views:

1889

answers:

7

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.

+1  A: 

It shouldn't matter what order they run in ... if your tests depend on another test running first, your tests are flawed :-)

Joel Martinez
I figured that was coming.
Daniel Auger
Agreed - but what about the scenario when you are trying to track which test is not cleaning up after itself (in a data driven test) - knowing the execution order would help in this case
Delaney
I know this isn't always possible, I have a few test harnesses that are data driven in this way. But you should avoid data driven tests like this at all costs. Unit tests should only test units. Data driven tests should be integration tests, and be run using some other tool (ie. not mstest or nunit) where you can control execution order for exactly the reason you're describing :-)
Joel Martinez
+1  A: 

There are many ways to order the tests in VS. Use the test view and the add extra columns and order. I use VSMDI files and this runs them in order specified therein.

Preet Sangha
+6  A: 

I was able to track down the answer.

According to Microsoft employee Guillermo Serrato:

MSTest executes all tests synchronously, the order is nondeterministic

Daniel Auger
+1  A: 

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"

dhopton
+1  A: 

It's very simple. I am using this

C:>mstest /testcontainer:C:\MyTest.dll /resultsfile:C:\MyTestresults.xml

The logic is here that by default mstest gives you a .trx file. So in the command against [/resultfile:] option write the name of file by giveing .xml extension. So I used /resultsfile:C:\MyTestresults.xml instead of /resultsfile:C:\MyTestresults.trx

This works fine for me. Let me know if it works for you.

A: 

Lets say I have a database that is loaded in ClassInitialize method, and I want all tests in that class to work against that loaded database. If it is not possible to order the tests in a nice way, some other ClassInitialize method might run between my tests, replacing the database and causing my tests to fail.

I dont like the ordered test files because the give no good view of what is happening. I want to be able to order my tests in the list and run them in that order. How hard could it be to implement? Not harder than figuring out a random mechanism...

Ragnar Osterlund
+1  A: 

In my experiences (I was little hard) I tried a lot of thinks about the change order of the method execution orders in the MStest project. I'm going to try describe the steps.

  1. You should ensure delete the default .testrunconfig and .vsmdi files. These files createdby Visual Studio when the creating test project.

  2. Close the Visual Studio instance and reopen it.

  3. When the Visual Studio opened you should open the Test List Edior then Create your own list under the Lists of tests section. After you create a new test list it should be empty.

  4. Go to All Loaded tests section then drag any test onto newly created test section with your choised order.

Then ensure your last created test section checked and you can press the run test and voala it's running with your defined order.

I don't know this might bug or some thing like that, but it magically working. We tested this method a couple of time with my colleague.

Good lock

fyasar