Scenarios with examples

Adam Ralph edited this page Dec 31, 2018 · 18 revisions 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's [Theory] attribute for data driven testing.


[Example(1, 2, 3)]
[Example(2, 3, 5)]
public void Addition(int x, int y, int expectedAnswer, Calculator calculator, int answer)
    $"Given the number {x}"
        .x(() => { });

    $"And the number {y}"
        .x(() => { });

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

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

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

results in this output: 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's DataAttribute will also work, including's own [ClassData] and [MemberData].
  • Each [Example] effectively generates a new scenario.
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.