I was under the impression that the test methods in a unit test class would be executed in the order that they appear in the class file. Apparently this is not true. It also doesn't appear to be purely based off of alphabetical order either. How does MSTEST decide execution order?
EDIT: I was able to track down the answer after digging a bit. See below.
Occasionally, you may want to have unit tests run in a specific order. Ideally, the order in which unit tests run should not matter, and it is best practice to avoid ordering unit tests. Regardless, there may be a need to do so. In that case, this article demonstrates how to order test runs.
The TestClass attribute denotes a class that contains unit tests. The TestMethod attribute indicates a method is a test method.
@After annotation is used on a method containing java code to run after each test case.
I was able to track down the answer.
According to Microsoft employee Guillermo Serrato:
MSTest executes all tests synchronously, the order is nondeterministic
Sorting in Test View, or test list editor causes them to appear like you have control -- it's just a by product of the implementation. We make no attempt to actually execute them in a specific order (We've gone back and forth on the "allow order", and "randomly order").
If you really need order, that is what ordered tests are for. These are available in all editions where Unit Test is available -- either use the Test/New Test menu, or right click the test project and create a "ordered test"
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