views:

76

answers:

1

I'm trying to perform two methods marked [TestMethod] in an order

First: Login
Second: GetUser

But Test choses the Second to be First.

Is there a a way to set order of execution of methods marked [TestMethod]?

+3  A: 

Ordering your tests like this is bad design. Most unit testing frameworks won't let you order your tests for this very reason. It sounds like you should be organising your tests like this:

Test 1: Test that a user can log in.

Test 2: Fake a logged in user, make sure that GetUser returns that faked user.

You should make sure your system is designed to allow this kind of testing (faking/mocking out of parts for tests). Otherwise you will end up with unmaintainable tests that will all break when something core does.

Think of it this way: in your second test, you aren't testing the login process, so why should the test break if login is broken? It shouldn't, so you need to make sure you can remove the dependency on the real login process, and instead use some method of setting up a user as logged in that cannot fail.

Jamie Penney
This is very true. However, to answer the question the user was asking (instead of the one he should have asked :-), the Silverlight unit testing framework runs tests in alphabetical order. For instance, when I'm debugging a particular test, and want it to execute first so that I don't have to wait for all the others to finish, I change its name to "aaMyTestName". (There may be a better way to do this, but I haven't found it yet.)
Ken Smith
@Ken: please add answers as answers not as a comment.
AnthonyWJones
Jamie Penney
@Jamie - I very much agree. Your answer is better than mine, which is why I didn't add it as a separate answer, just an explanatory comment to yours. It's worth knowing that tests are executed in alphabetical order, but it's a very, very bad idea to depend on that when writing your tests.
Ken Smith