Skip to content

Instantly share code, notes, and snippets.

@gasparnagy
Last active February 23, 2016 07:11
Show Gist options
  • Save gasparnagy/54b3fe009bcb4a63f68c to your computer and use it in GitHub Desktop.
Save gasparnagy/54b3fe009bcb4a63f68c to your computer and use it in GitHub Desktop.
Examples for blog post "Running SpecFlow scenarios parallel with xUnit 2"
Feature: Addition
Scenario Outline: Add two numbers
Given I have entered <a> into the calculator
And I have entered <b> into the calculator
When I press add
Then the result should be <result> on the screen
Examples:
| case | a | b | result |
| classic | 50 | 70 | 120 |
| commutativity | 70 | 50 | 120 |
| zero | 0 | 42 | 42 |
<specFlow>
<unitTestProvider name="xUnit" />
</specFlow>
public class Calculator
{
private readonly Stack<int> operands = new Stack<int>();
public int Result
{
get { return operands.Peek(); }
}
public void Enter(int operand)
{
operands.Push(operand);
}
public void Add()
{
operands.Push(operands.Pop() + operands.Pop());
}
public void Multiply()
{
operands.Push(operands.Pop() * operands.Pop());
}
}
[Binding]
public class CalculatorSteps
{
private readonly Calculator calculator = new Calculator();
[Given(@"I have entered (.*) into the calculator")]
public void GivenIHaveEnteredIntoTheCalculator(int operand)
{
calculator.Enter(operand);
}
[When(@"I press add")]
public void WhenIPressAdd()
{
calculator.Add();
}
//...
[Then(@"the result should be (.*) on the screen")]
public void ThenTheResultShouldBeOnTheScreen(int expectedResult)
{
Assert.Equal(expectedResult, calculator.Result);
}
}
Feature: Multiplication
Scenario Outline: Add two numbers
Given I have entered <a> into the calculator
And I have entered <b> into the calculator
When I press multiply
Then the result should be <result> on the screen
Examples:
| case | a | b | result |
| classic | 50 | 70 | 3500 |
| commutativity | 70 | 50 | 3500 |
| zero | 0 | 42 | 0 |
| one | 1 | 42 | 42 |
[Given(@"I have entered (.*) into the calculator")]
public void GivenIHaveEnteredIntoTheCalculator(int operand)
{
((Calculator)ScenarioContext.Current["calc"]).Enter(operand);
}
[Binding]
public class CalculatorSteps
{
private readonly ScenarioContext scenarioContext;
public CalculatorSteps(ScenarioContext scenarioContext)
{
this.scenarioContext = scenarioContext;
}
[Given(@"I have entered (.*) into the calculator")]
public void GivenIHaveEnteredIntoTheCalculator(int operand)
{
((Calculator)scenarioContext["calc"]).Enter(operand);
}
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment