The TestContext Class

The unit test framework contains a class that is used to store information pertaining to executing tests. This class is called TestContext. You use the properties of this class to get information about your running tests, including the path to the test directory, a URL to the executing test (in the case of ASP.NET unit tests), and data-binding information such as the data connection or the current row of data for the executing test class. There are also a couple useful methods of this class for things like writing trace messages. The test context information is stored inside properties of this class. The key properties and methods are defined inside Table 8.1.

Image

TABLE 8.1 TestContext Key Properties and Methods

TestContext is not accessible to your code by default. You access TestContext by first defining a field and a property named TestContext. The unit test framework automatically creates an instance of a TestContext object when it runs your tests. It then looks for a TestContext property in your source code. If it finds it, the framework assigns an instance of TestContext to your property. You can then use your property to access information about the executing test context. The following code shows how you might define the TestContext property in a C# unit test.

private TestContext testContextInstance;

public TestContext TestContext {
  get
  {
    return testContextInstance;
  }
  set {
    testContextInstance = value;
  }
}


Note

Some attribute classes require that you define a parameter of type TestContext to your decorated method. This is true for ClassInitialize (as discussed later). In this case, the unit test framework automatically passes an instance of a TestContext object to your method when it executes.


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

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