how does MSTest determine the order in which to run test methods?

28,790

Solution 1

I believe that MSTest executes test methods ordering them by their 'ID' (seems to be their full namespace).

I created a TestProject1 which contains 4 unit tests (UnitTest1, ...2, ...A, ...B). Each unit test contains 5 test methods (TestMethodA, ...B, ...1, ...2, ...3). They were declared with random order inside their test classes. Now, every time I run MSTest, the tests are executed with the same order:

TestProject1.UnitTest1.TestMethod1
TestProject1.UnitTest1.TestMethod2
TestProject1.UnitTest1.TestMethod3
TestProject1.UnitTest1.TestMethodA
TestProject1.UnitTest1.TestMethodB
TestProject1.UnitTest2.TestMethod1
TestProject1.UnitTest2.TestMethod2
TestProject1.UnitTest2.TestMethod3
TestProject1.UnitTest2.TestMethodA
TestProject1.UnitTest2.TestMethodB
TestProject1.UnitTestA.TestMethod1
TestProject1.UnitTestA.TestMethod2
TestProject1.UnitTestA.TestMethod3
TestProject1.UnitTestA.TestMethodA
TestProject1.UnitTestA.TestMethodB
TestProject1.UnitTestB.TestMethod1
TestProject1.UnitTestB.TestMethod2
TestProject1.UnitTestB.TestMethod3
TestProject1.UnitTestB.TestMethodA
TestProject1.UnitTestB.TestMethodB

The only way to change that order is to rename one TestClass or a TestMethod. If for example I rename the TestMethodB, of the UnitTest1, to TestMethod4 it will be executed before TestMethodA.

To see the IDs of your test methods open the 'Test View' window from VS and then right click on a column header (e.g. Test Name) --> "Add/Remove Columns..." and add 'ID' column.

Solution 2

The MSDN says ;-)

How to: Create an Ordered Test

http://msdn.microsoft.com/en-us/library/ms182631.aspx

MSTest.exe command-line options

http://msdn.microsoft.com/en-us/library/ms182489(v=vs.120).aspx

Solution 3

As for VSTest execution order. Here is how it's organized in your TestProject:

  1. Sort cs-files in your project by their CREATION Time ASC
  2. Method Position in each file

For example, you have 3 cs files in project.

  • UnitTest1.cs - created 01/01/1970 with methods TestMethod05 and TestMethod03
  • UnitTest2.cs - created 05/01/1970 with method TestMethod02.
  • UnitTest3.cs - created 03/01/1970 with method TestMethod01.

Then order of executing test is this:

    TestProject1.UnitTest1.TestMethod05
    TestProject1.UnitTest1.TestMethod03
    TestProject1.UnitTest3.TestMethod01
    TestProject1.UnitTest2.TestMethod02

You can see the 'default order' using command:

vstest.console.exe TestProject1.dll /ListTests

Share:
28,790

Related videos on Youtube

gerryLowry
Author by

gerryLowry

currently creating videos for my Paradigm Mentors website, for example, there are several free videos about PayPal's REST API, plus notes, plus a free c# Code First, ASP.NET MVC5, EF6 demo application at paradigmmentors.com/paypal. Programmer since 1967. Worked for MS Canada PSS 1994 - 1995. Self employed 25++ years. Program mainframe, mini, PC. APL, assembler, BASIC, c, c++, c#, COBOL, FORTRAN, J, Java, Pascal, Perl, PL/I, RPG II/III, VB, VBA, et cetera. Taught Business Systems Analysis and programming courses at community college level. Clients include GM, IBM. Male. Age 74. Married. Two adult progeny. Hobbies include computer and natural languages, chess, mathematics, reading. Ethical Vegan. Programming style: TDD using xUnit.net framework by Jim Newkirk and Brad Wilson Twitter: @gerrylowry1947 → twitter.com/gerrylowry1947 résumé: gerrylowryprogrammer.com

Updated on July 09, 2022

Comments

  • gerryLowry
    gerryLowry about 2 years

    edit: note, question 288805 is similar, however, I specifically am asking how does MSTest choose the default test order. Please see the rest of this question. Thank you Eilon for the link.

    I was looking at a legacy MSTest project. The tests were always running in the same order. The order was not alphabetic and was bouncing between methods in two *.cs TestMethod files.

    I did not change the physical order of the legacy code. I did for my convenience append "MSTest01" to the method name of the first test, "MSTest02" to the method name of the second test, et cetera.

    To my surprise, the execution order of the TestMethod functions changed; #3 first, #6 second, #5 third, et cetera.

    When I removed the "MSTestnn" strings from the TestMethod function names, their execution order changed back to the previous ordering, i.e., one test from the first .cs file, two tests from the second .cs file, five tests from the first .cs file, et cetera.

    It seems that file location may not be a factor while TestMethod function name may be a factor.

    QUESTION: can anyone explain how MSTest decides on execution order of TestMethod functions?

    • Mathias
      Mathias over 14 years
      Typically you should not care in what order your unit tests run. That being said, it is an intriguing question.
    • gerryLowry
      gerryLowry over 14 years
      @ Mathias I agree. I like the way xUnit.net runs tests in random order. Random order is the best choice because it is likely to reveal sequencing dependencies that should not exist. See my comments to Troy.
    • Eilon
      Eilon over 14 years
    • gerryLowry
      gerryLowry over 14 years
      ? weird, my comments to Troy seem to have disappeared, along with his question.
    • gerryLowry
      gerryLowry over 14 years
      ademiller.com/blogs/tech/2007/11/… F.Y.I. "xUnit.net runs tests in random order", Ade Miller
  • Dejan Dakić
    Dejan Dakić almost 10 years
    This behaviour is not specified and it can change without notice.
  • Razkar
    Razkar over 6 years
    Is there a way to change that default order in vstest, other that try to mess with file creation time(delete, recreate)?
  • Ivan Tsyng
    Ivan Tsyng over 6 years
    You can try to Use Playlist. Right click on the Test method in Test Explorer => Add to playlist => New playlist
  • philreed
    philreed over 6 years
    How did you get MSTest to output the actual execution order? I'm am trying to achieve the same here: stackoverflow.com/questions/48379363/…