Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MsTest TestCleanup method not called when an unhandled exception is thrown

I have a test which uses an external assembly to access UI features in the application we're testing. This assembly throws an exception of its own custom type if the UI is not in the appropriate state.

I've set up a TestCleanup method that kills the application's process (while a TestInitialize starts it) so that after a test run has been completed, the UI is restarted with a clean state.

This work well under regular conditions, however, whenever an exception from the referenced assembly is thrown, it never gets to the cleanup method and jumps straight ahead to the next test. This doesn't happen with exceptions thrown from the test itself, like AssertFailedException. I even tried throwing a basic Exception from the test, and it got to the cleanup method.

like image 492
Tal Bronfer Avatar asked Aug 14 '13 08:08

Tal Bronfer


2 Answers

If an exception is uncaught in TestInitialize, TestCleanup won't be called.

  1. http://web.archive.org/web/20140310065725/http://connect.microsoft.com/VisualStudio/feedback/details/694337/testcleanup-method-does-not-run-when-it-should
  2. When MSTest fails in TestInitialize, why doesn't TestCleanup get executed?
  3. Under what circumstances are [ClassCleanup] and [TestCleanup] not run
like image 131
Andre Miras Avatar answered Nov 13 '22 11:11

Andre Miras


This unfortunately diverges from the way C# handles exceptions in constructors : when this happens, the finalizer is called.

like image 29
Johan Boulé Avatar answered Nov 13 '22 11:11

Johan Boulé