views:

56

answers:

3

Why my NUnit tests execute in different order than they are listed?

And upon what the execution order depends?

+3  A: 

It depends on how the runner arranges them after reflecting on your test assembly. Each runner may do this differently.

For instance, Reshaper's runner runs alphabetically.

But the salient issue here is that NUnit tests should not have sequential stipulations or dependencies.

If you need to have tests depend on other tests, use MbUnit. This is an example of fixture dependency using the DependsOnAttribute.

Sky Sanders
I actually wanted to understand the execution order. I am not asking this question after facing any problem.
JMSA
@JMSA - so the answer is "It depends on how the runner arranges them after reflecting on your test assembly. Each runner may do this differently". - updated answer to be clear
Sky Sanders
+2  A: 

Any unit test runner will probably reflect over the test fixture class and search for all methods with a certain attribute applied (or which matches some naming convention). The order these methods are returned does not depend on the order they are specified in the source file, so they cannot be executed in the order they are defined. This shouldn't be a problem since unit tests should run independently from each other.

Lee
I actually wanted to understand the execution order. I am not asking this question after facing any problem. And, by the way, I have verified @Sky Sanders's answer. It is true. It really executes the tests alphabetically.
JMSA
+2  A: 

They are run alphabetically as mentioned. If you open a test dll in the GUI NUnit runner you'll see the ordering.

Despite this, the order in which unit tests are run should not matter. A unit test should be isolated and independent, thus order should be no issue.

Finglas