yW0K5o
yW0K5o

Reputation: 943

How could unit tests be rearranged?

I have VS 2012. All tests are executed in alphabetical order. How could I enforce my own execution order of tests? I tried searching StackOverflow and google on the topic but received no results.

Upvotes: 1

Views: 80

Answers (4)

Dnyanesh
Dnyanesh

Reputation: 2343

Basic principle of the Unit test is each test Unit is independent. And as long as you are following Arrange, Act and Assert will make your test complete. On top of that it is advised to use something called as TestIntialize in order to keep your tests clean and independent. Considering all this what your are looking for will not make your tests UNIT tests.

Upvotes: 0

Babak Naffas
Babak Naffas

Reputation: 12581

I agree with the others that the order of your tests shouldn't matter. That being said, Visual Studio doe support ordered tests. See How to: Create an Ordered Test

Upvotes: 1

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236328

There is nice FIRST principles of writing effective unit tests:

  • Fast - tests should run very quickly, so that you can run them very often. If you don't run tests often, then you have kind of latency in feedback which your code gives you. Duration of that latency shows how many code you can write until you understand you have broken something.
  • Isolated - this is what you are trying to violate - tests should not depend on each other, and they should have single reason to fail. If test fails, you should be able to say exact reason of failure, without debugging it and investigating context, environment, dependencies or other conditions.
  • Repeatable - tests should produce same results no matter when you run them. Otherwise tests results are uncertain, and such test have arguable value.
  • Self-validating - results of tests should be clear - you should not go and investigate some test output. If something should be checked, it should be done by test itself.
  • Timely - write them before you write your code, i.e. use TDD.

So, if your tests depend on order they run, then your test have more than one reason to fail - they can fail if implementation does not meat requirements, OR it can fail if implementation is correct, but test has been executed in wrong order. Which makes test results uncertain.

Upvotes: 2

LakshmiNarayanan
LakshmiNarayanan

Reputation: 1188

As suggested by @Matt and @Pete, the unit tests should be developed in a way that they are independent of each other.

However, to answer your question, at the time of posting this answer, there isn't a way to select the sequence. Refer Visual Studio Feature Requests. It is however in their backlog.

Upvotes: 1

Related Questions