NUnit Secondary Thread Exception

I am testing code that starts a secondary thread. And this thread sometimes throws an exception. I would like to write a test that fails if this exception is not handled properly.

I prepared this test, and what I see in NUnit:

LegacyImportWrapperTests.Import_ExceptionInImport_Ok : PassedSystem.ArgumentException: aaaaaaaaaa
at Import.Legacy.Tests.Stub.ImportStub.Import() in ImportStub.cs: line 51...

But the test is marked as GREEN. So, NUnit knows about this exception, but why does it mark the test as Passed?

+3
source share
1 answer

Just because you can see the details of an exception in the output does not necessarily mean that NUnit knows about the exception.

AppDomain.UnhandledException , (, , ):

bool exceptionWasThrown = false;
UnhandledExceptionEventHandler unhandledExceptionHandler = (s, e) =>
{
    if (!exceptionWasThrown)
    {
        exceptionWasThrown = true;
    }
};

AppDomain.CurrentDomain.UnhandledException += unhandledExceptionHandler;

// perform the test here, using whatever synchronization mechanisms needed
// to wait for threads to finish

// ...and detach the event handler
AppDomain.CurrentDomain.UnhandledException -= unhandledExceptionHandler;

// make assertions
Assert.IsFalse(exceptionWasThrown, "There was at least one unhandled exception");

, :

UnhandledExceptionEventHandler unhandledExceptionHandler = (s, e) =>
{
    if (!exceptionWasThrown)
    {
        exceptionWasThrown = e.ExceptionObject.GetType() == 
                                 typeof(PassedSystem.ArgumentException);
    }
};
+4

Source: https://habr.com/ru/post/1760712/


All Articles