Search code examples
c#unit-testingvisual-studio-2017

How do I fix "The active test run was aborted" in Visual Studio's Test Explorer?


"Run All" from "Test Explorer" does not complete (VS2017 Enterprise) anymore. It stalls with Passed (411), Not Run (309). The counts vary a little, usually roughly half and half.

The output window (Visual Studio | Output tab | Show output from: Tests) contains the following error message:

"The active test run was aborted. Reason: Unhandled Exception: System.AppDomainUnloadedException: Attempted to access an unloaded AppDomain."

The tests continue to run fine in ReSharper (720 of 720 pass). R# is where I usually run my tests. I jump over to Microsoft's "Test Explorer" when I am trying to Analyze Code Coverage (though the tests stall with or without code coverage). It (Analyze Code Coverage) worked as recently as 5/15/2018 (and at least a half dozen to a dozen times before that). enter image description here


Solution

  • The Microsoft test runner was being tripped up by a single unit test class that happened to have Task.Run() calls such as the following:

        var task = Task.Run(async () =>
        {
            <various code>
        });
    

    These tests were missing calls to task.Wait() to wait for each task to finish before exiting the test.

    (This appears to trip up the Microsoft test runner but not the ReSharper test runner. Specifically, the Microsoft Test Runner aborts the sln test run and skips 300+ tests. ReSharper was able to run all tests without incident.)

    Aside: The reason for the varied behavior on Windows 7 versus Windows 10 is because the test class was for a Windows 10 sensitive 3rd party control/library.