How to execute in parallel C++ tests written with the Microsoft Unit Testing Framework ?

Julianis 20 Reputation points
2025-01-15T10:58:01.3766667+00:00

We are writing C++ tests with the help of Microsoft Unit Testing Framework (https://learn.microsoft.com/en-us/visualstudio/test/how-to-use-microsoft-test-framework-for-cpp?view=vs-2022#run-the-tests)

Tests are running fine, it works within Visual Studio and on our CI/CD servers.
And we are happy with the suite.

But we start to have a lot of tests and we want to parallelize the tests.

When I activate the "Run tests in parallel" option in the test menu, or if I use the /Parallelize option of vstest.console.exe, the C++ tests are still run in sequential order, and there is no improvement to the duration of the tests.

It seems to work for C# tests but not for C++ written tests.

One simple repro, debug version compiled with Visual Studio 2022 (17.12.3):

vstest.console.exe mytests.dll
...
Total execution time = 5.1s

vstest.console.exe mytests.dll /Parallel
...
Total execution time = 5.1s

Is there a way to run parallel tests in C++ ?
Some hidden attributes ?

The C++ tests are quite simple:


namespace MyTests

{
TEST_CLASS(LengthyTests)
{
static int DoSomeLengthyCalculations(int loops)
{
int s = 0;
for (int i = 0; i<loops; i++)
{
for (int j = 0; j<1000; j++) s += 3;
s += 1;
for (int j = 0; j<1000; j++) s -= 3;
}

return s;
}

TEST_METHOD(First)
{ Assert::AreEqual(500'000, DoSomeLengthyCalculations(500'000)); }

TEST_METHOD(Second)
{ Assert::AreEqual(800'000, DoSomeLengthyCalculations(800'000)); }

}; }

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,368 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,830 questions
Visual Studio Testing
Visual Studio Testing
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Testing: The act or process of applying tests as a means of analysis or diagnosis.
353 questions
{count} votes

Accepted answer
  1. Viorel 119.2K Reputation points
    2025-01-15T11:40:20.0833333+00:00

    I think that you should use this:

    vstest.console.exe mytests1.dll mytests2.dll mytests3.dll /Parallel
    

    I.e. create several different test projects and distribute the tests through these projects. The “/Parallel” option probably means running test files in parallel.

    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.