Sometimes, you have the need to rerun failed tests of your application. For example, if you have a large number of System or UI tests that tend to be more fragile.

In my work, we use MSTest as primary unit/integration/UI tests framework. However, MSTest doesn’t come with any built-in feature for rerunning failed tests through its console runner.

Rerun Failed Tests Approaches

One way to accomplish the task is to extend the MSTest Execution Flow. However, there is very little information about the matter.

If you read these blog posts, you will find out that it is a difficult job to extend the MSTest Execution Flow and in my opinion is not the best solution.

I came up with a different answer to the problem- a console application that wraps mstest.exe and adds additional command line arguments like “\retriesCount:” and “\threshold:“. Here I will share with you the code of the application.

How To Rerun Failed Tests

1. Download the application.

2. Extract the files

3. Open MSTest.Console.Extended.exe.config

For the value set the correct path to the mstest.exe on the machine on which you are going to run your tests.

4. Open log4net.config

Here set the default location for the application’s log. Later, there you will find detailed information about the executed actions.

5. Set correct command line arguments

If you want to start test execution via mstest.exe, you can use the following line:

If you want to use MSTest.Console.Extended.exe to be able to rerun failed tests, you can use the following command:

There are four new arguments added.

/retriesCount:3 – tells the application to retry the failed tests three times

/deleteOldResultFiles:true – if set to true and the specified test result files exist, they will be deleted if possible

/newResultsFile:”…” – the original test results file will not be modified. A new updated version will be created where all passed tests from the new test runs will be set.

/threshold:5 – If the percentage of the failed tests is bigger than 5% no next reruns will be performed.

Below you can find an overview of the actions performed in the application.



First the specified console arguments will be parsed. The extended arguments will be removed. If you set deleteOldTestResults to true, the old test result files will be deleted if they exist. Next the mstest.exe will be started with the specified arguments. The program will wait for mstest.exe to exit. Next will deserialize the trx test results file to C# objects and will get all failed tests. If there are any failed tests, their percentage is smaller than the specified threshold and the retriestCount is larger than 1, failed tests will be rerun. A new mstest.exe run will be started. After that, the initial testRun’s results will be updated. If there are still any failed tests and the previously mentioned conditions are again met, a new rerun will be conducted. If you have specified newTestResultsFile, the newly updated testRun results will be serialized in it. Otherwise, the original trx file will be updated. If there are any failed tests, the application will exit with code one else with code 0.

MSTest.Console.Extended Code Organization

The code follows Onion Pattern, which allows it to be 100% unit testable.



The first project contains the core logic for the rerun of failed tests; the second one holds all unit tests.

As you can see from the Main method of the MSTest.Console.Extended project, the main business logic is located in the TestExecutionService class. Through Inversion of Control, it uses the different Provider classes located under the Infrastructure folder.

Below you can find the main part of the TestExecutionService class that follows the logic described in the above diagram.

As you can see, it only implements the desired algorithm but all of the particular logic, like argument parsing, file system operations, test run operations, is located in the Provider classes.

ConsoleArugmentsProvider Class



The ConsoleArgumentsProvider class contains the main logic for parsing the specified console arguments. Also, it implements the IConsoleArgumentsProvider interface, which helps us to mock it easily. It uses the below regex expressions to extract the different arguments and their values.

FileSystemProvider Class

The below class holds the main logic for serialization and deserialization of the mstest’s test result trx files into C# objects. It implements the IFileSystemProvider interface.



You can find all classes related to the mstest test runs under the Data folder.



MsTestTestRunProvider Class

We use the below class to perform all actions related to the Test Run objects like get all failed/passed tests, updated the test result summary and so on. It implements the IMsTestTestRunProvider interface.



ProcessExecutionProvider Class

The ProcessExecutionProvider is used to start the mstest.exe with specific parameters. It implements the IProcessExecutionProvider interface.

