How to Run Unit Tests (Mstest) in Parallel

How to run unit tests (MSTest) in parallel?

Most of the answers on this page forget to mention that MSTest parallelizes tests in separate assemblies. You have to split your unittests into multiple .dll's to paralelize it.

But! The recent version - MSTest V2 - now CAN parallelize "in-assembly" (yay!) you just need to install a couple of nuget packages in your test project - TestFramework and TestAdapter - like described here https://blogs.msdn.microsoft.com/devops/2018/01/30/mstest-v2-in-assembly-parallel-test-execution/

And then simply add this to your test project

[assembly: Parallelize(Workers = 4, Scope = ExecutionScope.ClassLevel)]

EDIT: You can also disable parallel execution for a specific test using [DoNotParallelize] on a test method.

How to run test sequences in new MSTests?

From this anwer here, you can see that you can also disable parallel execution for a specific test using [DoNotParallelize] on a test method.

In MSTest, how can I specify that certain test methods cannot be run in parallel with each other?

MsTest v2 has functionality as following

[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]
// Notice the assembly bracket, this can be compatible or incompatible with how your code is built

namespace UnitTestProject1
{
[TestClass]
public class TestClass1
{
[TestMethod]
[DoNotParallelize] // This test will not be run in parallel
public void TestPrice_5PercentTax() => //YourTestHere?;

[TestMethod]
[DoNotParallelize] // This test will not be run in parallel
public void TestPrice_10PercentTax() => //YourTestHere?;

[TestMethod]
[DoNotParallelize] // This test will not be run in parallel
public void TestPrice_NoTax() => //YourTestHere?;

[TestMethod]
public void TestInventory_Add10Items() => //YourTestHere?;

[TestMethod]
public void TestInventory_Remove10Items() => //YourTestHere?;
}
}

More detailed information can be found here MSTest v2 at meziantou.net

I strongly recommend atleast a quick read through of the link, as this will likely help you solve and understand the issue with the tests run in parallel or sequential.

Parameterize Tests to run Parallel or Single Thread in MSTEST

I actually found another way to do it. During the devops process, after checking out the code, I am doing a bit of powershell, which is below. Then only a singular tests run at a time.

task: PowerShell@2
inputs:
targetType: 'inline'
script: |
(Get-Content -Path '$(Build.Repository.LocalPath)\code\properties\AssemblyInfo.cs') |
ForEach-Object {$_ -replace 'Workers = 0','Workers = 1'} |
Out-File '$(Build.Repository.LocalPath)\code\properties\AssemblyInfo.cs'
errorActionPreference: 'continue'


Related Topics



Leave a reply



Submit