I’m creating some unit tests in Visual Studio, and I want to create a base class that creates a specific set of test data in my application, and then when the last unit test in the test run has completed, automatically cleans up the test data. I can handle the creation of the data just fine, I just don’t know if it is possible to tell that the test run has completed.
In other words:
- UnitTest1 and UnitTest2 inherit UnitTestBaseCreateData (conversely, they could have a call to a static method in the constructor of both UnitTest1 and UnitTest2, that creates the test data)
- UnitTest3 and UnitTest4 do not require the data to be created.
- If I select and run UnitTests 1, 2, 3, and 4, the test data will be created on demand for test 1, and automatically cleaned up after test 4 has completed
- If I select and run UnitTests 3, and 4, the test data will not be created and the cleanup code will not run.
- If I select and run only unit test 1, the test data will be created on demand, and then cleaned up after test 4 has completed.
I just don’t know if there is some [TestRunCleanup()] (similar to TestCleanup and ClassCleanup attributes) to a method attribute available that I can use as a hook to see if I have test data I need to cleanup…
You can use the AssemblyCleanupAttribute. When a method is decorated with the attribute, MS Test will execute the method when ALL tests (of that assembly) in the current test run are completed.