Some of my mstest unit tests help detect multi-threading race conditions, and as such they are most useful when run many times in a row, but I only want to do this for specific test runs — not all the time.
Is there a way to configure mstest (in the Test List Editor preferably) to run a test multiple times?
Best Answer
I needed to do something similar, so I came up with a solution to this.
It's not simple, but once everything is setup you can reuse it across projects. I also have a download of this code on GitHub (https://github.com/johnkoerner/MSTestLooper), but in case that goes away at some point, here is how I did it.
First we create an attribute that we will apply to our class to tell it run all the tests multiple times. Do all of this in a separate assembly, because the DLL needs to live in a special location.
Next we have to create a custom test class execution class:
Finally we add a custom invoker, which is where we perform the looping:
The HelperTestResults class just builds up strings for output, you can handle this how you want and I don't want to include that code because it will just make this post that much longer.
Compile this into a DLL and then you need to copy it to
You also have to create a registry entry for the class:
Now that you have all of that done, you can finally use the class:
Notice that our test method accepts a parameter, which comes from the test looper. I also show this using a data driven test, to show you can combine the two together to generate large permutations across your data sets.