Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Under what circumstances are [ClassCleanup] and [TestCleanup] not run

I have a set of MSTest test cases that use methods with those attributes to delete all rows from tables in the test db created after the tests are started; every once in a while I end up with a row from a one of the unit tests still in the DB.

Since I'm saving the test start time and deleting all records with timestamps more recent than it, the only way I can see for the records surviving is if the cleanup code wasn't called.

like image 297
Dan Is Fiddling By Firelight Avatar asked Feb 04 '11 15:02

Dan Is Fiddling By Firelight


2 Answers

Well, first off, a "unit test" technically shouldn't touch the DB. But, I use NUnit for my integration tests too, so no biggie.

The only circumstances in which the cleanup methods wouldn't run in their entirety is if there were an error in the cleanup function, or an exception caused the runtime to shut down. The only ones that I know can do that are stack overflow and out-of-memory errors. Aborting a test from the debugger will also cause the cleanup phase to be skipped.

A more likely explanation, depending on your data access layer, is that you lost your DB connection and the cleanup function couldn't get a new one. That would generally indicate a need to make your DAL more robust, and also indicates that something you're not expecting is happening in the test to cause the DAL to break down.

like image 172
KeithS Avatar answered Oct 13 '22 04:10

KeithS


If you are debugging the tests and you stop the debugger before the test run completes, this will not be called.

like image 33
Mark Avenius Avatar answered Oct 13 '22 05:10

Mark Avenius