Skip to content
This repository has been archived by the owner on Jun 12, 2021. It is now read-only.

Scenarios with examples

Adam Ralph edited this page Aug 5, 2015 · 18 revisions

xBehave.net allows the passing of example values for the parameters in a scenario method.

This is equivalent to Cucumber's Scenario Outlines and works in a similar manner to xUnit.net's [Theory] attribute for data driven testing.

E.g.

[Scenario]
[Example(1, 2, 3)]
[Example(2, 3, 5)]
public void Addition(int x, int y, int expectedAnswer, Calculator calculator, int answer)
{
    "Given the number {0}"    // or in C# 6 or later, $"Given the number {x}"
        .f(() => { });

    "And the number {1}"
        .f(() => { });

    "And a calculator"
        .f(() => calculator = new Calculator());

    "When I add the numbers together"
        .f(() => answer = calculator.Add(x, y));

    "Then the answer is {2}"
        .f(() => Assert.Equal(expectedAnswer, answer));
}

results in this output: xUnit.net console examples output

There are few things to note here:

  • Each parameter which does not have a corresponding example value (based purely on number of values/parameters) continues to have its default value passed (null for reference types and zero values for value types).
  • You are not limited to using only the [Example] attribute for providing values. Any attribute which inherits from the xUnit.net [InlineData] attribute will also work, including xUnit.net's own [ClassData], [OleDbData], [SqlServerData], [ExcelData] and [PropertyData].
  • Each [Example] effectively generates a new scenario
  • The example values can be injected into the step descriptions using the format {n} where n is the ordinal number of the example value. This feature is largely redundant when using C# 6 or later since string interpolation can be used instead (see the comment in the above example).
Clone this wiki locally