0

I'm using C# with NUnit.

Let's say you have a class

public class MyClass {
    private int classMember;

    [Test]
    public void Test1() {
        classMember = 1;
        Assert.That(classMember == 1);
    }
    
    [Test]
    public void Test2() {
        classMember = 2;
        Assert.That(classMember == 2);
    }
}

I'm wondering if it's possible for Test1() and Test2() to run concurrently and interfere with each other. For example, if Test1 and Test2 run at the same time, maybe Test1's assertion would fail because Test2 would set classMember to 2 before Test1 asserts that classMember = 1. Is this an issue, and if so, what is the best practice for handling it?

Thanks!

3
  • NUnit tests run concurrently if you explicitly annotate either the test (Or its class or assembly) with [Parallelizable]. docs.nunit.org/articles/nunit/writing-tests/attributes/… Commented Mar 31, 2021 at 17:27
  • 1
    So I guess that means I don't have to worry about that situation then unless I annotate the test. Thanks @BenCottrell! Commented Mar 31, 2021 at 17:28
  • The annotation also works at class scope and assembly scope too (So a test could inherit that from its class or assembly without being annotated), but yes it's an 'opt-in', with the default being no parallel execution of tests. Commented Mar 31, 2021 at 17:31

3 Answers 3

0

By default NUnit creates a single instance for all of your test cases within the same test class. Since NUnit 3.13, this behavior can be modified to create a new instance per test case and/or test method.

Creating a single instance per test case is really useful when running your tests on parallel within the same class, as it reduces the total duration of the test class execution. We use it when we run our end-to-end tests.

See more here https://docs.nunit.org/articles/nunit/writing-tests/attributes/fixturelifecycle.html

6

All test frameworks I know create a new separate instance of the test class for every single test.

Not just because of concurrency, but also to make sure every test starts with the same state instead of having the object in whatever state a previous test left it in.

Concurrency problems typically arise from static fields or singletons of some sort.

1
  • I believe NUnit is the one version of xUnit where this is not true. At least that is what I remember reading from "xUnit Test Patterns: Refactoring Test Code". I could be remember wrong or the framework has changed since its as written. Commented Apr 5, 2021 at 20:52
0

It's possible to run NUnit tests in parallel. Tests also use the same instance of a TestClass. This second statement can easily be proven by following tests:

public class MyClass {
    private int classMember = 0;

    [Test]
    public void Test1() {
        classMember += 1;
        Assert.That(classMember == 1);
    }
    
    [Test]
    public void Test2() {
        classMember += 1;
        Assert.That(classMember == 1);
    }
}

One of these will always fail. So yes it's possible that Test1 and Test2 interfere with each other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.