jeudi 30 avril 2015

Implementing Unhandled Exception Handler in C# Unit Tests

I've got some tests and they rely heavily on some shared code that I can't modify. This shared code throws an exception sometimes and I want to be able to handle all uncaught instances of this exception without wrapping every call to the shared code in a try catch (there are years of tests here).

I also want to be able to re-throw the exceptions that aren't of the type that I'm looking for.

I've tried

public void init() 
{
    AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    Logger.Info("Caught exception");
    throw (Exception)e.ExceptionObject;
}

But it appears that the stock unit test framework (Microsoft.VisualStudio.QualityTools.UnitTestsFramework) is doing something with the AppDomain and preventing me from overriding its UnhandledException handler.

Anyone have any suggestions?

Aucun commentaire:

Enregistrer un commentaire