In the code snippet below, why not turn on the handler (from the AppDomain.CurrentDomain.UnhandledException event) when the exception is thrown in the unit test?
I am using NUnit 2.5.10 with TestDriven.NET 3.0 on VS2010.
[TestFixture] public class MyTests { private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine("Gotcha!"); } [Test] public void ExceptionTest1() { AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; throw new Exception("ExceptionInTest"); } }
Output: (no error)
------ Test started: Assembly: WcfQueue.Test.dll ------ Test 'xxxxx.Test.MyTests.ExceptionTest1' failed: System.Exception : ExceptionInTest ProgramTests.cs(83,0): at xxxxx.Test.MyTests.ExceptionTest1() 0 passed, 1 failed, 0 skipped, took 1.98 seconds (NUnit 2.5.5).
Update: The purpose of this question is NOT to test the .NET or NUnit environment. I just want to find out the reason why the handler does not work in unit test.
source share