Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / HPC / parallelization

Parallelization in Unit-Tests with MSTest v2

5.00/5 (2 votes)
2 Apr 2019CPOL1 min read 11.3K  
Parallel unit-tests execution with MSTest v2

Introduction

If you have unit-tests running on each checkin, or you have a lot of unit-tests and you want to improve the time of execution, this article is for you!

I will show you how to implement some changes in your current test project in order to improve execution time locally from Visual Studio, but also in a TFS build pipeline.

Tests Results Before

Before applying the changes, this build step time was taking about 10 minutes.

Image 1

Tests Results After

After applying the changes, the build step time decreases almost 5 minutes.

Image 2

Changes to be Applied

You will need to install from nugets two packages in your project MSTest.TestAdapter and MSTest.Framework.

Image 3

Image 4

Image 5

You will also need to remove from your project the default installed package "Microsoft.VisualStudio.QualityTools.UnitTestFramework".Image 6

Image 7

Now, you only need to add the attribute assembly in some class on your unit-test project, in my case, I add it in the BaseTest class.

Image 8

The variables Workers and Scope can be changed depending on your own customization:

  • Workers = 0 (use as many threads as possible based on CPU and core count)
  • Workers = X (X is number of threads to execute tests)
  • Scope = ClassLevel (each thread executes a TestClass you have in your project)
  • Scope = MethodLevel (each thread executes a TestMethod)

TFS Build Definition

In the build definition, you just need to update the field "Path to Custom Test Adapters" if your Agent server is using Visual Studio 2015, you need to specify in this field the MSTest.TestAdapter.dll added to the project previously.

Image 9

Image 10

To finalize, this change increased the speed in almost 50% of execution time.

Image 11

History

  • 2nd April, 2019: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)