The test context setup is done in the test class constructor, since the test setup is not applicable in xUnit. For every test, xUnit creates a new instance of the test class, which implies that the codes in the class constructor are run for each test.
Oftentimes, it is desirable for unit test classes to share a test context because it can be expensive to create and clean up test contexts. xUnit offers three approaches to achieve this:
- Constructor and dispose: Sharing setup or cleanup code without having to share the object instances
- Class fixtures: Sharing object instances across tests in a single class
- Collection fixtures: Sharing object instances across multiple test classes
You should use constructor and dispose when you want a fresh test context for every test in a test class. In the following code, the context object will be constructed and...