The Test Attribute Classes

The unit tests you write are run by Visual Studio using the unit test execution host. This host has to examine your code and find the unit tests within it and run them accordingly. To do so, the host relies on attributes. Recall that an attribute is used to provide metadata about your code. Other code (such as the unit test host) can use reflection to determine various bits of information about your code.

As you’ve seen in the brief samples thus far, you signify unit tests by decorating your code with the attribute classes defined inside the unit testing namespace. For example, a test class has the TestClass attribute; a test method is indicated using the TestMethod attribute. Table 8.2 presents a list of the most common attribute classes found in the unit testing namespace.

Image
Image
Image

TABLE 8.2 Visual Studio Test Attribute Classes

You can see from Table 8.2 that a number of attribute classes give you control over your unit tests. Figure 8.8 shows our InvoiceTests unit test class with a few additions. We have created a TestContext instance. We have also stubbed out code for initializing the test class and cleaning up following the test run. The next step is to write code inside each of these test methods.

Image

FIGURE 8.8 The invoice test class with stubbed out methods for class initialize and cleanup.

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

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