How To Order Unit Tests in Visual Studio 2008

Sometimes it is maybe necessary to bring some order into your unit tests in Visual Studio. Indeed, this is not really the idea of unit testing, however it might be necessary for a variety of reasons. Facing this issue, I had to search the MSDN documentation for a while.

I was desperately looking for some syntax like:

[TestMethod(Order = 1)]

However, the only way to bring some order in your tests is, by creating some ordered tests in addition to your unit tests.

To do so, you first create a set of unit tests. In my case, some of the tests should be only executed after other run successfully. Otherwise the result might look like:

Failed Unit Test

Now we create a new test project called Order Test:

Add New Ordered Test

When opening the project you actually can arrange the order of the previously written unit tests:

Arrange an Ordered Test

Running the test then is possible by selecting the test from the Test View window. Your tests will actually only appear as one entry in the list but you will see that all the selected tests run successfully – in the correct order.

Succesfull Ordered Test

2 Comments

  1. Paul

    Reply

    Alternatively you could create a wrapper TestMethod around the methods you want to execute in order.

    E.g.

    <TestMethods()> _
    Public Sub TestMethod1
    Method1()
    Method2()
    Method3()
    End Sub

    Private Sub Method1()
    ‘Do something
    End Sub

    Private Sub Method2()
    ‘Do something
    End Sub

    Private Sub Method3()
    ‘Do something
    End Sub

  2. GR

    Reply

    You could also use the Priority attribute to asign a priority to each unit test and then order your test list by Priority in order to achieve the desired test run.

    e.g.

    [TestMethod]
    [Priority(1)]
    public void Some_Test()
    {
    }

Leave Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.