lundi 21 septembre 2015

How to make xUnit to run a Theory parallel?

I've got a test (Theory) which is slow and a bunch of test cases for it. So I want them to run simultaneously.

I've created a simple example:

[Theory]
[MyTestData]
public void MyTheory(int num, int sleep)
{
    Console.WriteLine("{0:HH:mm:ss.ffff} - Starting {1} - Sleeping {2}", DateTime.Now, num, sleep);
    Thread.Sleep(sleep);
    Console.WriteLine("{0:HH:mm:ss.ffff} - Finished {1} - Sleeping {2}", DateTime.Now, num, sleep);
}

[AttributeUsage(AttributeTargets.Method)]
public class MyTestDataAttribute : DataAttribute
{
    public override IEnumerable<object[]> GetData(MethodInfo testMethod)
    {
        yield return new object[2] { 1, 5000 };
        yield return new object[2] { 2, 2000 };
        yield return new object[2] { 3, 4000 };
    }
}

The command line to run the test:

"\packages\xunit.runner.console.2.0.0\tools\xunit.console" "\Projects\xUnitTest\xUnitTest\bin\Debug\xUnitTest.dll" -parallel all > xUnitResult.txt

And the result:

xUnit.net console test runner(64-bit.NET 4.0.30319.42000)
Copyright(C) 2015 Outercurve Foundation.

Discovering: xUnitTest
Discovered:  xUnitTest
Starting:    xUnitTest
21:55:39.0449 - Starting 2 - Sleeping 2000
21:55:41.0627 - Finished 2 - Sleeping 2000
21:55:41.0783 - Starting 1 - Sleeping 5000
21:55:46.0892 - Finished 1 - Sleeping 5000
21:55:46.0892 - Starting 3 - Sleeping 4000
21:55:50.0989 - Finished 3 - Sleeping 4000
Finished:    xUnitTest

=== TEST EXECUTION SUMMARY ===
   xUnitTest Total: 3, Errors: 0, Failed: 0, Skipped: 0, Time: 11,137s

Which is pretty serial. I'm sure it is possible to make it parallel.

Aucun commentaire:

Enregistrer un commentaire