7

Here is what I want to achieve with xUnit:

  1. Run initialization code.
  2. Run tests in parallel.
  3. Perform teardown.

I have tried [CollectionDefinition]/[Collection]/ICollectionFixture approach described here but it has disabled the parallel execution, which is critical for me.

Are there any way to run tests in parallel and be able to write global setup/tear-down code in xUnit?

If it is not possible with xUnit, does NUnit or MSUnit support this scenario?

IT Hit WebDAV
  • 5,652
  • 12
  • 61
  • 98

2 Answers2

0

NUnit supports this scenario. For global setup, create a class in one of your root namespaces and add the [SetupFixture] attribute to it. Then add a [OneTimeSetUp] method to that class. This method will get run once for all tests in that namespace and in child namespaces. This allows you to have additional namespace specific onetime setups.

[SetUpFixture]
public class MySetUpClass
{
  [OneTimeSetUp]
  public void RunBeforeAnyTests()
  {
    // ...
  }

  [OneTimeTearDown]
  public void RunAfterAnyTests()
  {
    // ...
  }
}

Then to run your tests in parallel, add the [Parallelizable] attribute at the assembly level with the ParallelScope.All. If you have tests that should not be run in parallel with others, you can use the NonParallelizable attribute at lower levels.

[assembly: Parallelizable(ParallelScope.All)]

Running test methods in parallel in NUnit is supported in NUnit 3.7 and later. Prior to that, it only supported running test classes in parallel. I would recommend starting any project with the most recent version of NUnit to take advantages of bug fixes, new features and improvements.

Rob Prouse
  • 22,161
  • 4
  • 69
  • 89
  • Thant you, Rob! Is it possible to configure setup method for a group of test classes (collections in terms of xUnit)? Like ICollectionFixture is doing it for xUnit, but tests within collection must run in parallel. – IT Hit WebDAV Feb 01 '18 at 18:46
  • 1
    Yes, the setup fixture in the code example is for a namespace, so you should group tests that need the same setup in the same namespace (or in child namespaces) and create setup fixtures for each namespace that needs them. – Rob Prouse Feb 01 '18 at 20:10
  • I just realized that parallel execution is not supported in NUnit on .NET Core. https://stackoverflow.com/questions/46526999/nunit-net-core-parallel-tests – IT Hit WebDAV Feb 02 '18 at 00:54
  • 1
    Parallel execution for. NET Core will be in the next release. Watch this space – Rob Prouse Feb 02 '18 at 13:48
-2

A somewhat basic solution would be static class with a static constructor and subscribing to the AppDomain.CurrentDomain.ProcessExit event.

public static class StaticFixture
{
    static StaticFixture()
    {
        AppDomain.CurrentDomain.ProcessExit += (o, e) => Dispose();

        // Initialization code here
    }

    private static void Dispose()
    {
        // Teardown code here
    }
}

There's no guarantee when the static constructor gets called though, other than at or before first use.

Niklas Holm
  • 949
  • 9
  • 14