/specflow-demo

BDD testing with Specflow and Gherkin

Primary LanguageC#

SpecFlow

SpecFlow is a popular open-source testing framework for .NET that facilitates behavior-driven development (BDD).

BDD is an agile software development process that encourages collaboration between developers, and non-technical stakeholders by using natural language descriptions to define the behavior of a software system.

SpecFlow allows you to write specifications and acceptance criteria in plain text using a Gherkin syntax, which is a structured language for describing software behaviors. Here's an example of a Gherkin scenario:

Feature: Login
  In order to access the application
  As a registered user
  I want to log in with my username and password

Scenario: Successful login
  Given the user is on the login page
  When they enter valid credentials
  Then they should be logged in

SpecFlow then maps these plain text scenarios to executable code in .NET languages like C#. This allows you to automate your acceptance tests and validate that your software behaves according to the specified requirements.

Here's an example of how you might set up a step definition file for your Gherkin scenario:

using TechTalk.SpecFlow;

[Binding]
public class LoginSteps
{
    private LoginPage loginPage;

    [Given("the user is on the login page")]
    public void GivenTheUserIsOnTheLoginPage()
    {
        // Implementation code to navigate to the login page
    }

    [When("they enter valid credentials")]
    public void WhenTheyEnterValidCredentials()
    {
        // Implementation code to enter valid credentials
    }

    [Then("they should be logged in")]
    public void ThenTheyShouldBeLoggedIn()
    {
        // Implementation code to verify that the user is logged in
    }
}

SpecFlow allows you to configure hooks, which are a powerful feature for setting up and tearing down the environment for your tests. These hooks enable you to perform necessary setup work before tests run and execute finishing work after tests have completed. This is particularly useful for managing resources, initializing data, or performing any other actions required to ensure a clean and consistent testing environment.

BeforeScenario: This hook runs before each scenario in your feature file. You can use it to set up any preconditions specific to the scenario.

AfterScenario: This hook runs after each scenario, allowing you to perform cleanup or post-scenario actions.

BeforeFeature: This hook is executed once before any scenario within a feature file runs. It's typically used for feature-level setup.

AfterFeature: This hook runs once after all scenarios within a feature have executed, and it's useful for feature-level teardown or cleanup.

BeforeTestRun: This hook is executed once before any scenarios run. You can use it for global setup that applies to all the scenarios.

AfterTestRun: This hook runs once after all scenarios have completed. It's suitable for global teardown or cleanup that applies to the entire test run.

Here's an example of defining hooks in SpecFlow:

using TechTalk.SpecFlow;
using TechTalk.SpecFlow.Infrastructure;

[Binding]
public class Hook
{

    [BeforeScenario]
    public void BeforeScenario()
    {
        // Run before scenario
    }

    [AfterScenario]
    public void AfterScenario()
    {
        // Run after scenario
    }

    [BeforeFeature]
    public static void BeforeFeature()
    {
        // Run before feature
    }

    [AfterFeature]
    public static void AfterFeature()
    {
        // Run after feature
    }

}

Resources: