简体   繁体   中英

NUnit grouping asserts in parallel testing

I want to run NUNit tests in parallel because I have heavy wait conditions that end up in massively long test runs in a sequential manner. This simplified structure outlined below suits all my needs however, it seems that NUnit is grouping the asserts when I call the TestParallel method

public class Tests
{
    [Test]
    public void Test1()
    {
        Assert.Fail("Test1 Failure");
    }

    [Test]
    public void Test2()
    {
        Thread.Sleep(1000);
        Assert.Fail("Test2 Failure");
    }

    [Test]
    public void TestParallel()
    {
        var toRun = new Task[]
        {
            Task.Run(() => Test1()),
            Task.Run(() => Test2())
        };
        Task.WaitAll(toRun);
    }
}

Both tests fail as expected however, the second failing test also displays the first failing tests assert exception like so:

NUnit.Framework.AssertionException: 'Multiple failures or warnings in test:
  1) Test1 Failure
  2) Test2 Failure
'

在此处输入图片说明

What options do I have to avoid this behavior?

It's NUnit's job to run your tests, not yours. :-)

In your code, you have given NUnit three tests to run, Test1, Test2 and TestParallel. It does so but TestParallel is also launching Test1 and Test2 a second time. Their assertions are considered part of TestParallel and are reported accordingly.

You could, of course, remove the [Test] attribute from Test1 and Test2, leaving yourself only one test which incorporates the other two. That's not an approach that scales very well.

The more natural approach would be to have NUnit run the tests in parallel rather than doing it yourself. To accomplish that, add [Parallelizable] to Test1 and Test2 and remove TestParallel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM