BDD with .NET: SpecFlow - Almantask/CleanCodeSeries.Workshop GitHub Wiki

Specflow Flow

  1. Install SpecFlow extension to visual studio.

  2. Create a test project and add the needed nugets:

  1. SpecFlow
  2. SpecFlow.Tools.MSBuild.Generation
  3. SpecRun.SpecFlow
  4. NUnit

  1. 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!".
  1. Generate template skeleton for your test

Inside the opened feature file, right click Generate Step Definitions. Select what you want and hit ok.

  1. 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);
        }
    }
  1. Rebuild test project

  2. Run tests

  3. Enjoy the results :)