Search code examples
testingxunitxunit.net

XUnit 2.4 running tests in sequence


We are using XUNit 2.4 and have the following tests setup, These tests are all in the same class, so Xunit should run them in sequence. Does the existance of the Theory and InlineData attributes cause these tests to run in parrellel. I.E the test runner will run all InlineData attributes for 1 test in parrellel?

[Theory]
[Trait("Category", "Acceptance")]
[InlineData(1, 0.0000)]
[InlineData(1, 0.9900)]
[InlineData(1, 1.9900)]
[InlineData(3, 0.9900)]
[InlineData(4, 29.9900)]
[InlineData(5, 2)]
public async Task ShouldReturnNewPrice(int packagePriceQuantity, decimal retailPrice)
{
}


[Theory] 
[Trait("Category", "Acceptance")]
[InlineData(1, 0.0000)]
[InlineData(1, 0.9900)]
[InlineData(1, 1.9900)]
[InlineData(3, 0.9900)]
[InlineData(4, 29.9900)]
[InlineData(5, 2)]
public async Task ShouldReturnDiscount(int 

packagePriceQuantity, decimal retailPrice) {}


Solution

  • The above tests will never run in parallel; only one test case runs per test class at any moment. (There are Parallel running facilities at Test Class and Assembly levels; the settings allow you to opt in or out of that - for this question, the important factor is that as long as you have the two Test Methods on the same Test Class, they will not and cannot be parallelized)

    For individual cases in a single theory test, the order is not randomized; they run in sequence. The ordering is defined by the order that the reflection APIs yield out the InlineData attributes. That frequently aligns with the order in the source, but that's only a coincidence if it does work (there is no strict ordering to attributes applied to something in code at the IL/metadata level). (If you want/need the cases to be run in random order, you'd need to have a MemberData serving the data in shuffled order)

    The individual Test Methods (ShouldReturnNewPrice, ShouldReturnDiscount ) will run in random order if you request the full test class to be run. (There is a facility for overriding this behavior, but the randomization is an important intentional default)