Parallel test execution within the context of MSTestv2, NUnit3 and VSTest.Console
Tags: c, testing
Published: 2018-10-15 18:12:31

---
title: Parallel test execution within the context of MSTestv2, NUnit3 and VSTest.Console
published: true
description: 
tags: c#,testing 
---

Keeping automated tests within a reasonable performance range is no trivial task and I'm always looking for ways to reduce the total duration of automated tests.  Recently, I did some reading on the capabilities of a few different tools with regards to parallel execution of tests. These tools included [MSTestv2](https://github.com/Microsoft/testfx) and [NUnit3](https://github.com/nunit/nunit) when run with [vstest.console](https://github.com/Microsoft/vstest).

While building a test case to compare the two I built two projects.  One using MSTestv2 (v1.3.2) `[DataTestMethod]`s, designed to take advantage of [DynamicData](https://github.com/Microsoft/testfx-docs/blob/master/RFCs/006-DynamicData-Attribute.md) and another using NUnit3 (v3.10.1). Below is my MSTestv2 test.  
  

```csharp
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Threading;

[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)] //0 means use as many workers as possible

namespace mstest_parallel
{
    [TestClass]
    public class UnitTest1
    {
        private static IEnumerable<object[]> MyTestData()
        {
            for (int i = 0; i < 10; i++)
            {
                yield return new object[] { i * 1000 };
            }
        }

        [DataTestMethod]
        [DynamicData(nameof(MyTestData), DynamicDataSourceType.Method)]
        public void TestMethod1(int testVal)
        {
            Thread.Sleep(testVal);
        }
    }
}

```

Now, MSTestv2 has the [ability to parallelize tests](https://blogs.msdn.microsoft.com/devops/2018/01/30/mstest-v2-in-assembly-parallel-test-execution/) to some degree. Here are the parallelization options it offers at a high level:

- **Class level** - each thread executes a `[TestClass]` worth of tests. Within the `[TestClass]`, the test methods execute serially
- **Method level** - each thread executes a `[TestMethod]`
- **Custom** - users can provide a plugin implementing the required execution semantics (Not yet supported).

These capabilities can be referred to as **fine-grained parallelization** features and the number of worker threads can be configured via assembly attribute `[assembly: Parallelize(Workers = n, Scope = Execution.ClassLevel)]` or a `.runsettings` file. [More details here](https://blogs.msdn.microsoft.com/devops/2018/01/30/mstest-v2-in-assembly-parallel-test-execution/)

Keep in mind these are different than [MSTest v1's parallel options](https://blogs.msdn.microsoft.com/vstsqualitytools/2009/12/01/executing-unit-tests-in-parallel-on-a-multi-cpucore-machine/) and **also** different from vstest.console's parallel options.

Vstest.console's [parallel options](https://blogs.msdn.microsoft.com/devops/2016/10/10/parallel-test-execution/) focus on what its authors refer to as **coarse-grained parallelization**. This means one can parallelize the running of multiple test containers at once. So if you are running vstest.console from the command line and specify several test containers i.e., `mytestlib1.dll mytestlib2.dll` vstest.console will run each test container simultaneously up to the maximum possible on the machine or the maximum configured.

Why do we care? I want to squeeze as much performance out of the chosen test framework and runner as possible - ideally without having to write any threading code myself. **Unfortunately, MSTestv2 (and v1) lacks a very specific and important parallelization option; The ability to run tests in parallel which are configured with `[DynamicData]` and/or `[DataRow]`**. Evidence for this not being supported exists [here](https://github.com/Microsoft/testfx/issues/405), [here](https://github.com/Microsoft/testfx/issues/452) and [here](https://github.com/Microsoft/testfx/issues/142#issuecomment-394283354). For our situation, this means our test method that utilizes `[DynamicData]` to generate unique cases will have all of the test cases per `[TestMethod]` run in serial rather than in parallel, thus making the majority of our parallelization efforts moot.

With parallelization turned on at the method level for the assembly our proof of concept MSTestv2 project showed the following results when using vstest.console to execute:

**Total tests: 11.  Passed: 11. Failed: 0. Skipped: 0.**  
**Test Run Successful.**  
**Test execution time: 47.4547 Seconds**

Not that great. The majority of these tests are being run one by one.

**NOTE**: vstest apparently reports the 'base' test method as a test case when running MSTestv2 tests which is why the test count is 11 instead of 10.  My understanding is that this does not actually run, but contains the overall results for all the tests associated with this `[TestMethod]`.

Let's check out NUnit3. This framework offers the [feature we want](https://github.com/nunit/nunit/issues/2471#issuecomment-340290377), which is running parameterized tests that have their cases generated at runtime in parallel.

The fundamental difference between the frameworks here is that MSTestv2 considers test cases via `[DynamicData]` and `[DataRow]` to all be [under a single [TestMethod]](https://github.com/Microsoft/testfx/issues/450#issuecomment-400966410) whereas NUnit3 considers [each test case its own separate [Test]](https://github.com/nunit/docs/wiki/Parameterized-Tests).

Here's our test method adjusted to be used with NUnit3:  

  

```csharp
using NUnit.Framework;
using System.Collections.Generic;
using System.Threading;

namespace nunit_parallel
{
    [TestFixture]
    [Parallelizable(ParallelScope.Children)] //parameterized tests are considered child tests
    public class UnitTest1
    {
        private static IEnumerable<object[]> MyTestData()
        {
            for (int i = 0; i < 10; i++)
            {
                yield return new object[] { i * 1000 };
            }
        }

        [Test]
        [TestCaseSource(nameof(MyTestData))]
        public void TestMethod1(int testVal)
        {
            Thread.Sleep(testVal);
        }
    }
}
```

The attribute specified at the top of the class [`[Parallelizable(ParallelScope.Children)]`](https://github.com/nunit/docs/wiki/Framework-Parallel-Test-Execution) tells NUnit to parallelize every test method in the class and it considers each of our generated test cases a unique test method. Additionally, I am letting NUnit decide my `[LevelOfParallism]`.  By [default](https://github.com/nunit/docs/wiki/LevelOfParallelism-Attribute), NUnit3 uses the processor count or 2, whichever is greater.

Now, running with the same tests using vstest.console and same number of tests cases per `[Test]`:

**Total tests: 10.  Passed: 10. Failed: 0. Skipped: 0.**  
**Test Run Successful.**  
**Test execution time: 17.5729 Seconds**

Predictably, the time to run the suite of tests is drastically shorter.

One important part of this setup is how these tests are run. Vstest.console handles running tests from each test framework for us as long as we can [specify the necessary test adapter](https://github.com/Microsoft/vstest-docs/blob/master/RFCs/0004-Adapter-Extensibility.md#specifying-an-adapter). For both NUnit3 and MSTestv2, the adapters are available as NuGet packages. Adding these NuGet packages to your test project will cause the necessary libraries to be copied out upon build and vstest.console will find them for you so long as they are in the same directory as the test containers you specify.

I hope this helps explain some of the options regarding parallelizing tests, especially parameterized ones. I learned a ton while studying this stuff and am I sure this will continue to be the case while writing these types of test suites. If you believe anything I have written above needs to be corrected please reach out to me.