I dont see anyone mentioning the ClassInitialize
attribute method. The attributes are pretty straight forward.
Create methods that are marked with either the [ClassInitialize()]
or [TestInitialize()]
attribute to prepare aspects of the environment in which your unit test will run. The purpose of this is to establish a known state for running your unit test. For example, you may use the [ClassInitialize()]
or the [TestInitialize()]
method to copy, alter, or create certain data files that your test will use.
Create methods that are marked with either the [ClassCleanup()]
or [TestCleanUp{}]
attribute to return the environment to a known state after a test has run. This might mean the deletion of files in folders or the return of a database to a known state. An example of this is to reset an inventory database to an initial state after testing a method that is used in an order-entry application.
[ClassInitialize()]
Use ClassInitialize
to run code before you
run the first test in the class.
[ClassCleanUp()]
Use ClassCleanup
to run code after all tests in
a class have run.
[TestInitialize()]
Use TestInitialize
to run code before you run
each test.
[TestCleanUp()]
Use TestCleanup
to run code after each test has
run.