Metadata for Test Initialization and Cleanup

,

The UTF provides attributes that allow you to specify methods that should be run before and after test execution. You can provide methods that are executed once for each test assembly, once for each test class, and once for each test method. The order in which methods decorated with each particular attribute are run is presented in Figure 24.4.

Image

FIGURE 24.4 Test execution order is affected by initialization and cleanup metadata.

AssemblyInitialize Attribute

The AssemblyInitialize attribute identifies a method that contains code to be used before all tests in an assembly are run, and to allocate resources obtained by the assembly. A method decorated with the AssemblyInitialize attribute must be public and static, and have a void return type. The following demonstrates the use of the AssemblyInitialize attribute:

[AssemblyInitialize]
public static void AssemblyInitialize()
{
    /* Assembly initialization logic goes here. */
}


Note

The UTF runs a method that is marked with the AssemblyInitialize attribute only if that method is a member of a class that is marked with the TestClass attribute.


AssemblyCleanup Attribute

The AssemblyCleanup attribute is analogous to the AssemblyInitialize attribute but occurs at the end of the test run. As with the AssemblyInitialize attribute, a method decorated with this attribute should be located in a test class. The following shows an example of a method decorated with the AssemblyCleanup attribute:

[AssemblyCleanup]
public static void AssemblyCleanup()
{
    /* Assembly cleanup logic goes here. */
}

ClassInitialize Attribute

The ClassInitialize attribute provides the opportunity to run code before any of the tests in the test class have run, and to allocate resources to be used by the test class.

A method decorated with the ClassInitialize attribute must be public and static with a void return type. Only one method in a class may be decorated with this attribute.

The following shows an example of a method decorated with the ClassInitialize attribute:

[ClassInitialize]
public static void ClassInitialize()
{
    /* Class initialization logic goes here. */
}

ClassCleanup Attribute

The ClassCleanup attribute is analogous to the ClassInitialize attribute but occurs after all test methods have completed within the test class. The following shows an example of a method decorated with the ClassCleanup attribute:

[ClassCleanup]
public static void ClassCleanup()
{
    /* Class cleanup logic goes here. */
}

TestInitialize Attribute

The TestInitialize attribute is used to indicate that a method decorated with this attribute should be called before every test method within a test class. A method decorated with the TestInitialize attribute must be public and have a void return type. The following shows an example of a method decorated with the TestInitialize attribute:

[TestInitialize]
public void TestInitialize()
{
    /* Test initialization logic goes here. */
}


Tip

If more than one method is decorated with the TestInitialize attribute in a test class, it prevents the execution of all test methods. Furthermore, it does so silently. If you find that the debugger is failing to hit a break point in a test method, look for a duplication of the TestInitialize attribute.


TestCleanup Attribute

The TestCleanup attribute is useful for resetting the state of shared resources in between test methods, such as an object that is used by all tests within a test class.

[TestCleanup]
public void TestCleanup()
{
    /* Test cleanup logic goes here. */
}

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

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