Ricibob
Ricibob

Reputation: 7725

Run UnitTest several time - not just loop it

I have a VS UnitTest (MSTest) to cover some multiple threading code. With out the required locks the code fails the test 15 out 30 runs - i.e half the runs. I fix the code and the tests pass 30 times.

Now I want the test framework to run the test N time and not just once to be sure that it cant pass once out of luck. There doesn't seem to be any way to do that with an attribute on the test so I put a loop INSIDE the test to run the test code body N times.

I remove the fixes to the code (locks etc) and run the test (which loops N times) - and bam it passes. I run it again (loop N times) and it fails... Im back where I started - its still failing only half the time (even though its doing N loops through test body on each test).

What I really want is not to loop inside the test but really have the test framework load the test, run test, unload test etc N times (as I did by hand originally). How to do that? (Why isn't there just a test attribute for this - like [TestRepeatCount=5]?)

Upvotes: 0

Views: 2703

Answers (2)

Stanislav Berkov
Stanislav Berkov

Reputation: 6287

You can create own special attribute (derived from ITestDataSource) that runs your test N times, for more details https://stackoverflow.com/a/76311034/586609

Upvotes: 1

chaliasos
chaliasos

Reputation: 9793

What you really need is a Load Test. Add your unit test(s) and configure it how it will run.

You can set the number of total tests, the number of concurrent tests e.t.c.

Upvotes: 1

Related Questions