Behavior-Driven Development (BDD) helps you write tests in plain language, focused on how users expect your app to behave.
📝 What is BDD?
- Tests are written as scenarios in plain English.
- Encourages collaboration between developers, testers, and stakeholders.
🛠️ SpecFlow for .NET
- SpecFlow is a popular BDD tool for .NET.
1. Install SpecFlow NuGet packages
1
2
dotnet add package SpecFlow
dotnet add package SpecFlow.xUnit
2. Write a Feature
Create a .feature
file:
1
2
3
4
5
6
Feature: Calculator
Scenario: Add two numbers
Given the calculator is cleared
When I add 2 and 3
Then the result should be 5
3. Implement Step Definitions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Binding]
public class CalculatorSteps
{
Calculator calc = new Calculator();
int result;
[Given(@"the calculator is cleared")]
public void GivenTheCalculatorIsCleared() => calc.Clear();
[When(@"I add (.*) and (.*)")]
public void WhenIAddNumbers(int a, int b) => result = calc.Add(a, b);
[Then(@"the result should be (.*)")]
public void ThenTheResultShouldBe(int expected) => Assert.Equal(expected, result);
}
💡 Benefits
- Improves communication
- Documents expected behavior
Next: Automated integration testing!