DrifterApps.Seeds.FluentScenario
0.1.2
dotnet add package DrifterApps.Seeds.FluentScenario --version 0.1.2
NuGet\Install-Package DrifterApps.Seeds.FluentScenario -Version 0.1.2
<PackageReference Include="DrifterApps.Seeds.FluentScenario" Version="0.1.2" />
paket add DrifterApps.Seeds.FluentScenario --version 0.1.2
#r "nuget: DrifterApps.Seeds.FluentScenario, 0.1.2"
// Install DrifterApps.Seeds.FluentScenario as a Cake Addin #addin nuget:?package=DrifterApps.Seeds.FluentScenario&version=0.1.2 // Install DrifterApps.Seeds.FluentScenario as a Cake Tool #tool nuget:?package=DrifterApps.Seeds.FluentScenario&version=0.1.2
Fluent Scenario
FluentScenario is a C# library that provides a fluent BDD scenario testing framework.
Table of Contents
Installation
To install FluentScenario, you can use the NuGet package manager:
dotnet add package DrifterApps.Seeds.FluentScenario
If you use FluentAssertions to assert your tests. You can use the NuGet package manager:
dotnet add package DrifterApps.Seeds.FluentScenario.FluentAssertions
Usage
Scenario basics
Here is a basic test scenario ScenarioBasicsTests:
[Fact]
public async Task SimpleScenario()
{
int temperatureInside = 0;
int temperatureOutside = 0;
await ScenarioRunner.Create("when the weather is too cold", _scenarioOutput)
.Given("I want to go play outside", () => temperatureInside = 19)
.When("the temperature is below 0C", () => temperatureOutside = -5)
.Then("I stay inside if the temperature difference is greater than 20C", () => (temperatureInside - temperatureOutside).Should().BeGreaterThan(20))
.PlayAsync();
}
This will produce this output:
✓ SCENARIO for when the weather is too cold
✓ GIVEN I want to go play outside
✓ WHEN the temperature is below 0C
✓ THEN I stay inside if the temperature difference is greater than 20C
Scenario with execution methods
For scenarios where some steps could be shared between multiple scenarions, you can create a set of execution methods for those common steps ScenarioWithExecutionMethodsTests:
[Fact]
public async Task ScenarioWithExecutionMethods()
{
await ScenarioRunner.Create("when the weather is too cold", _scenarioOutput)
.Given(PlayOutsideMethod)
.When(TemperatureBelow0CMethod)
.Then(StayInsideMethod)
.PlayAsync();
}
private static void PlayOutsideMethod(IStepRunner runner)
{
runner.Execute("I want to go play outside", () =>
{
runner.SetContextData("temperatureInside", 19);
});
}
private static void TemperatureBelow0CMethod(IStepRunner runner)
{
runner.Execute("the temperature is below 0C", () =>
{
runner.SetContextData("temperatureOutside", -5);
});
}
private static void StayInsideMethod(IStepRunner runner)
{
runner.Execute("I stay inside if the temperature difference is greater than 20C", () =>
{
var temperatureInside = runner.GetContextData<int>("temperatureInside");
var temperatureOutside = runner.GetContextData<int>("temperatureOutside");
(temperatureInside - temperatureOutside).Should().BeGreaterThan(20);
});
}
This will produce this output:
✓ SCENARIO for when the weather is too cold
✓ GIVEN I want to go play outside
✓ WHEN the temperature is below 0C
✓ THEN I stay inside if the temperature difference is greater than 20C
Scenario with well named methods
Some of us like to properly name our methods so they are clear to the reader what the intent of the method is.
In this case, you can forgo a description and the ScenarioRunner will use the name of your calling methods to create the description ScenarioWithWellNamedMethodsTests:
[Fact]
public async Task WhenTheWeatherIsTooCold()
{
await ScenarioRunner.Create(_scenarioOutput)
.Given(IWantToGoPlayOutside)
.When(TheTemperatureIsBelow0C)
.Then(IStayInsideIfTheTemperatureDifferenceIsGreaterThan20C)
.PlayAsync();
}
private static void IWantToGoPlayOutside(IStepRunner runner)
{
runner.Execute(() =>
{
runner.SetContextData("temperatureInside", 19);
});
}
private static void TheTemperatureIsBelow0C(IStepRunner runner)
{
runner.Execute(() =>
{
runner.SetContextData("temperatureOutside", -5);
});
}
private static void IStayInsideIfTheTemperatureDifferenceIsGreaterThan20C(IStepRunner runner)
{
runner.Execute(() =>
{
var temperatureInside = runner.GetContextData<int>("temperatureInside");
var temperatureOutside = runner.GetContextData<int>("temperatureOutside");
(temperatureInside - temperatureOutside).Should().BeGreaterThan(20);
});
}
This will produce this output:
✓ SCENARIO for When The Weather Is Too Cold
✓ GIVEN I Want To Go Play Outside
✓ WHEN The Temperature Is Below0 C
✓ THEN I Stay Inside If The Temperature Difference Is Greater Than20 C
Scenario with parameters and results
To start a scenario, you can pass values in the Create method. And you can also pass values between steps.
Each values passed as a parameter is a Enzure<> value object. This value object is there to help ensuring that the value received is a valid value.
Here's an example ScenarioWithParametersAndResultsTests:
[Theory]
[InlineData(19)]
public async Task ScenarioWithParameters(int inside)
{
await ScenarioRunner.Create("when the weather is too cold", inside, _scenarioOutput)
.Given("I want to go play outside", (Ensure<int> temperatureInside) =>
{
temperatureInside.Should().BeValid(); // ensure the temperature is valid
return (temperatureInside.Value, -5);
})
.When("the temperature difference is great", (Ensure<(int inside, int outside)> temperatures) =>
{
temperatures.Should().BeValid(); // ensure the temperatures are valid
return temperatures.Value.inside - temperatures.Value.outside;
})
.Then("I stay inside if the temperature difference is greater than 20C",
(Ensure<int> temperatureDifference) =>
{
temperatureDifference.Should().BeValid().And.Subject.Value.Should().BeGreaterThan(20);
})
.PlayAsync();
}
This will produce this output:
✓ SCENARIO for when the weather is too cold
✓ GIVEN I want to go play outside
✓ WHEN the temperature difference is great
✓ THEN I stay inside if the temperature difference is greater than 20C
Contributing
Contributions are welcome! Please read the contributing guidelines for more information.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. |
-
net7.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on DrifterApps.Seeds.FluentScenario:
Package | Downloads |
---|---|
DrifterApps.Seeds.FluentScenario.FluentAssertions
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.