views:

30

answers:

1

I am writing unit tests for basic CRUD functionality using VS 2010.

My goal is to create new entities, read them back, update them, and then delete them.

One obvious approach is to have a unit test for each of those operations and run them in that order. Except that I can't directly specify the order the tests should run in.

I understand that I can create an Ordered Test. However, after doing so and selecting "Run all tests in solution", the Ordered Test runs in addition to the individual tests. This isn't really what I'm after, especially as there is no guarantee of the order for the individual tests... some of them may "randomly" fail depending on the order they happen to be executed in.

That seems to lead to the conclusion that I would have to individually execute my Ordered Tests if I wish to use that functionality.

Alternatively I could write one big test for C+R+U+D per entity, but that isn't as atomic as I would like.

What is best-practice (without resorting to xUnit) for this type of testing?

+1  A: 

You could create a Test List that contains only the Ordered Tests you want to run.

However, the best practice for unit testing is not to rely on test ordering at all. Ordered tests are Erratic Tests because they behave differently if executed in a different order (you already discovered that) or if executed as stand-alone tests instead of part of the ordered test suite.

Do yourself the favor and abandon the idea of ordered tests. It will only lead to more pain.

Mark Seemann
Are you suggesting that it's best practice to put all of the steps required to test each use case into a single test, even if that test becomes potentially large?
Eric J.
By the way I love that link!
Eric J.
No, I'm saying that each operation should have its own test - one that verifies that you can create an item, another (independent) test that verifies that you can update it, etc.
Mark Seemann