By default when you run a unit test from Visual Studio's Test Explorer, it only runs once. Is there a way to run it multiple times, like 100 times or something ? And then after running it multiple times, I get a report stating how many times the test passed/failed and what caused all the failures (if any) ?
Note: I looked at some other similar Stack Overflow threads but didn't see a solution that's applicable in my case.
In VS2022 there is a feature (since 17.4). In Test Explorer you can choose "Run Until Failure"

It will run 1000 times by default, it can be configured in VS Options under Test / General > Group: Test Run

EDIT: This answer has been superceded by the answer given by @Malior.
===========
I think this is a perfectly legitimate question, because sometimes tests can be flaky due to use of AutoFixture or other intentionally random elements to populate test models. This randomness is valuable as it can discover unforeseen flaws in testing and bugs in production code.
I know of no professional plugin that gives Visual Studio this feature, but there is always the coding approach:
public class TestClass
{
private Random _random;
public TestClass()
{
_random = new Random();
}
[Fact]
public void FlakyTest()
{
var number = _random.Next(10);
Assert.True(number < 7);
}
[Fact]
public void MyFlakyTestWorks100Times()
{
for (var i = 0; i < 100; i++)
{
var tests = new TestClass();
tests.FlakyTest();
}
}
}
Edit: Obviously you would want to seriously consider removing that looping test before you submit your work to your build pipeline!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With