When MSTest fails in TestInitialize, why doesn't TestCleanup get executed?

c#, mstest, visual-studio-2012

Solution

This is a known issue. It looks like it has been reported to Microsoft connect.

http://connect.microsoft.com/VisualStudio/feedback/details/694337/testcleanup-method-does-not-run-when-it-should

Problem

I have a base class that has a `TestInitialize` and a `TestCleanup` in it. I then have other test classes which inherit from this class. The `TestCleanup` is supposed to perform certain actions when a test from the derived class fails. The `TestCleanup` gets executed when the test fails in the actual test. However, when the test fails in the `TestInitialize`, the `TestCleanup` doesn't even get executed. Why does this happen and is there a way to get the `TestCleanup` to get called anyway? I don't think that the fact that the `TestInitialize` and `TestCleanup` are in a base class is at all causing this issue, I just wanted to give more information just in case it was.

Original source