Skip to content

Debugging Scenarios with examples

Michael W. Powell edited this page Jun 20, 2024 · 7 revisions

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

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

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

    $"Given a number {y}".x(() => { });

    $"Given an expected result {expected}".x(() => { });

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

    "When x plus y".x(() => actual = calculator.AssertNotNull().Add(x, y));

    "Then actual equal to expected".x(() => actual.AssertEqual(expected));
}

YMMV; all scenarios are committed to source and may be verified independently, including output to show for it. We ran the following command from the output directory:

dotnet ..\..\..\..\packages\xunit.runner.console\2.4.1\tools\netcoreapp2.0\xunit.console.dll Test.Xwellbehaved.Samples.dll -class "Xwellbehaved.DebuggingScenariosWithExamples" -verbose -html Test.Xwellbehaved.Samples.dll.WithExamples.html

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 derives from the xUnit.net DataAttribute will also work, including xUnit.net's own [ClassData] and [MemberData].
  • Each [Example] decoration effectively generates a new scenario.
Clone this wiki locally