Creating Ordered Tests

Visual Studio enables you to group unit tests, set their sequence of execution, and treat the results as if a single test was run. This can be useful if you need to write unit tests that are dependent on one another. For example, you might insert a record in one test and rely on that record being there in a later test. Of course, this goes against a good practice for unit testing; each test should be able to execute independently. Thankfully, you can create an ordered test that groups the individual unit tests into a new, self-contained test.

You add an ordered test to your test project by right-clicking the unit test project and selecting Add, Ordered Test. You can also select the Ordered Test template from the Add New Test dialog box.

An ordered test is simply an XML file based on the OrderedTest schema. You do not, however, need to hand-edit the XML. Instead, Visual Studio gives you the ordered test designer to help you. Figure 8.15 shows an example of this designer.

Image

FIGURE 8.15 You add existing unit tests to an ordered test to create a new test that executes two or more tests in a specific order.

The left side of the dialog box is where you find all your tests in your solution. Tests are shown by test name. You select individual tests from the left side and use the arrow (>) in the middle to include the tests in your ordered test. You can use the up and down arrows on the right side to change the order in which your tests execute.

The ordered test becomes its own test within your test project. You can then run it by itself or as part of a larger group. When run, Visual Studio executes each unit test in the order you defined. If any fail, the entire ordered test fails unless you have checked the Continue After Failure check box (located on the bottom of the dialog in Figure 8.15). You can view the details of the failed ordered test to see which tests passed and which failed. Figure 8.16 shows the ordered test inside the Test Explorer being run. Notice the first item (compute for negative) failed, so the other two tests in the ordered test were not run.

Image

FIGURE 8.16 The ordered test results inside Test Explorer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset