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.


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

c#, If an exception is uncaught in TestInitialize, TestCleanup won't be called. 1 Answer 1. active oldest votes. 6. From the call stack you posted, I see that the exception occurs in a seperate thread. It seems that only exceptions thrown in the calling thread of the test method cause the test to fail.


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

TestMethod failures are masked by TestCleanup exceptions in the , Steps to reproduce Create an MSTest V2 based unit test project. Throw an exception in the TestMethod and the TestCleanup. This happens only when the TestMethod fails AND the TestCleanup method fails. load failures in TestInitialize() are not getting reported When there is failure in TestCleanup() . Create an MSTest V2 based unit test project. Throw an exception in the TestMethod and the TestCleanup. Run the test. Expected behavior. An indication of the test method exception and stack trace should be shown in the outcome. Actual behavior. Only the TestCleanup stack is shown.


But you can directly call CleanUp method from the catch block

        [TestCleanup]
        public void Clenup()
        {
               ..............
        }


        [TestMethod]
        public void Test1()
        {
            try
            {...................}
            catch (Exception e)
            {
                 Cleanup();
                 throw new Exception();
            }
         }

TestCleanup should not be called if TestInitialize failed · Issue #250 , Description TestCleanup method is called even if TestInitialize failed. This is a wrong behavior for 2 reasons: It breaks compatibility with MSTest V1 (see more [TestInitialize] public void Init() { throw new Exception("Boom! The TestCleanup method should not be called (the message box in the example should not appear) Actual behavior. TestCleanup does get called (the message box do appear) Environment. OS: Windows 8.1 VS2015 Update 3 MSTest.Framework and MSTest.Adapter: 1.1.8. Notes about the real problem I'm facing: I'm trying to port TestAutomationEssentials.MSTest to MSTestV2. This project implement a generic TestBase class.


That was fixed in MsTest v2.

https://github.com/Microsoft/testfx/issues/250

Extract from the above link:

This was a conscious compat break to give unit test writers a choice to cleanup partially initialized methods.

NotImplementedException Class (System), The exception that is thrown when a requested method or operation is not implemented. You can include a call to the member when it is implemented in the  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.


Exceptions and Exception Handling, The C# language's exception handling features help you deal with any an exception may be thrown not by a method that your code has called directly, would terminate with a DivideByZeroException was unhandled error. C# Using TestCleanup in MSTest 0 If like me you put simplicity as the key priority factor when developing, your likely not a fan of unit test mocking frameworks as they add additional and for the majority of the time unnecessary complexity to your applications.


User Andre Miras, 11 MsTest TestCleanup method not called when an unhandled exception is thrown · 9 What does cifs_mount failed w/return code = -22 indicate · View more  Format ("Expected exception of type {0} but no exception was thrown.", typeof (T)));}} The key part is the first parameter of Action . Using this will accept any Action (method) but will also act as a wrapper so we can pass a Func (function) without having to provide an overloaded Throws() method signature for passing functions with n number of


Q & A Home, Getting Null Reference Exception when Running MSTests on the build · 1. replies System.NullReferenceException: Object reference not set to an instance of an object. TestCleanup method Tests.Cleanup threw exception. System. An unhandled exception occurred while executing this action: System. Microsoft has been informally calling the MSTest V2. The most exciting part of this is the fact that we’ve finally have Assert.ThrowsException and its async counterpart Assert.ThrowsExceptionAsync as part of the framework. Unfortunately, MS didn’t completely abandon ExpectedException,