Test Cleanup Errors

I spent some time refactoring some unit tests in a VS2010 project.  One of the changes I made was to move some variables to class-level and initialize them in the class startup in a couple integration tests.

 

[ClassInitialize()] public static void MyClassInitialize(TestContext testContext) { CreateTestObjects(); } private static void CreateTestObjects() { SwimGroup swimGroup = SwimGroupFactory.GetSwimGroup(testSwimGroupId); Practice practice = PracticeFactory.CreatePractice(DateTime.Parse(testCarpoolStartDate), false, DateTime.Parse(testCarpoolStartTime), DateTime.Parse(testCarpoolEndTime), swimGroup); PracticeFactory.InsertPractice(practice); testPracticeId = practice.Id; Driver driver = DriverFactory.GetDriver(testDriverId); Swimmer swimmer = SwimmerFactory.GetSwimmer(testSwimmerId); Carpool carpool = CarpoolFactory.CreateCarpool(practice, driver); carpool.Swimmers.Add(swimmer); CarpoolFactory.InsertCarpool(carpool); testCarpoolId = carpool.Id; }

 

Note that this is a regression test – I actually want to test the database connection – so I am not using a fake.

I also run a clean-up method to remove the test data that I injected into the dev database.

private static void DestroyTestObjects() { Carpool carpool = CarpoolFactory.GetCarpool(testCarpoolId); CarpoolFactory.DeleteCarpool(carpool); Practice practice = PracticeFactory.GetPractice(testPracticeId); PracticeFactory.DeletePractice(practice); }

 

I noticed two important things:

1) The Cleanup runs even if there is an error thrown in a test

 

image

 

2) If the Cleanup throws an exception, all of the tests still pass.

 

So the tests run in separate thread than the test controller.  Very cool – but makes me realize that unit testing multi-threaded apps must be a bear…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: