BDD with .NET: SpecFlow - Almantask/CleanCodeSeries.Workshop GitHub Wiki
Specflow Flow
-
Install SpecFlow extension to visual studio.
-
Create a test project and add the needed nugets:
- SpecFlow
- SpecFlow.Tools.MSBuild.Generation
- SpecRun.SpecFlow
- NUnit
- Create new feature
Feature- just a description of your tests. First half of key to BDD
Scenario- test steps-is the second half of key for BDD
- Given- setup input
- When- method call
- Then- assert
Feature: Greeter
In order to do a demo
I need to greet people first
So that we avoid any rude reactions
@mytag
Scenario: Greet Person
Given I have selected a language EN,
When I greet a person with a name "Tom",
Then the result should be "Hello, Tom!".
- Generate template skeleton for your test
Inside the opened feature file, right click Generate Step Definitions. Select what you want and hit ok.
- Fill the skeleton with your test implementation
[Binding]
public class GreeterSteps
{
private string _result;
private readonly Greeter _greeter = new Greeter();
[Given(@"I have selected a language EN,")]
public void GivenIHaveSelectedALanguageEN()
{
_greeter.Language = Language.EN;
}
[When(@"I greet a person with a name ""(.*)"",")]
public void WhenIGreetAPersonWithAName(string p0)
{
_result = _greeter.Greet(p0);
}
[Then(@"the result should be ""(.*)""\.")]
public void ThenTheResultShouldBe_(string p0)
{
Assert.Equal(_result, p0);
}
}
-
Rebuild test project
-
Run tests
-
Enjoy the results :)