Run test multiple times in Visual Studio?

Viewed 1804

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.

2 Answers

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!

This question is towards the wrong direction.

A unit test must be consistent, it should always pass or fail. If your test pass sometimes but some other times fails this is problematic. A unit test is supposed to test minimum functionality in your code, like a method.

In a unit test you are not testing external dependecies. When you call an external service (as mentioned in the comments) you are not testing your code, but someone else's code. That is why you always mock your dependencies and run tests on your mocked services.

Test explorer does not have the functionality you are asking for because unit tests are not supposed to be used this way.

Related