There’s often no discernable, consistent pattern that dictates the execution order of automated tests (Coded UI in this example, but the same applies to Unit Tests).  Some argue that it may be a good thing that there isn’t an inherent pattern for the execution of CodedUI tests (and unit tests, for that matter), as a seemingly more random pattern can uncover test dependencies which reduce the overall effective coverage of tests.  And I agree with that to an extent, but there are always cases in which control over execution order is needed.

An easy way to accomplish test order is to use an Ordered Test. This will provide you explicit control over the execution order of your tests.

For this example, I have a Coded UI Test class called CodedUITest1 (for more on Coded UI Tests, see the Anatomy of a Coded UI Test).  In it, I have two CodedUI Test methods:

  • CodedUITestRunFirst()
  • CodedUITestRunSecond()

I want to order them such that they execute like:

  • CodedUITestRunSecond()
  • CodedUITestRunFirst()

1. Add a new Ordered Test. Go to Test->New Test, and select Ordered Test.

 New test window

2. The ordered test will open. I can move the available tests from the left list into my ordered test list on the right. I can then move the tests up/down to create the desired order.

 Ordered Test dialog

It’s not shown in this screenshot, but there is a checkbox to allow the ordered test to continue upon a failure.

3. Save the ordered test. I can now see the ordered test in my Test View window.

Test View window showing new ordered test

4. When ready, I select to run my ordered test. It will appear in the Test Results window as a single test.

Test Results window showing ordered test

When finished, I can double-click on the test result to see that both tests did actually run, their individual results, and their order.

Detailed results of ordered test

It’s a surprisingly easy yet elegant solution.  I can put pretty much any automated test into an ordered test (except for load tests).  If you have a lot of tests, coupling the use of ordered tests with other test lists can really help visually organize your test project.