NUnit和AppDomain的UnhandledExceptionEventHandler

时间:2013-03-18 14:37:22

标签: nunit backgroundworker appdomain unhandled-exception

我在NUnit中编写单元测试有一些问题,它检查后台工作者在其RunWorkerCompleted事件中抛出自定义异常(ProcessException) - 为了参数,我们假设我们无法更改该代码。

我尝试做的是在我的单元测试中连接到AppDomain的UnhandledExceptionHandler,如下所示:

        AppDomain currentDomain = AppDomain.CurrentDomain;
        UnhandledExceptionEventHandler handler = null;

        handler = (s, e) =>
        {
            // Need to make it clear that an exception is expected in the output window
            Console.WriteLine("==== Unhandled exception has been caught ====");
            processExceptionWasThrownByBackgroundWorker = true;
            currentDomain.UnhandledException -= handler;
            Assert.AreEqual(typeof(ProcessException), e.GetType());
        };

        currentDomain.UnhandledException += handler;

        CallMyBackgroundWorkerThatThrows();

        Assert.AreEqual(true, processExceptionWasThrownByBackgroundWorker);

此单元测试运行良好,并在使用TestDriven.Net从visual studio中运行时通过。但是,当它在构建机器上运行时,显示,NUnit控制台也捕获未处理的异常[1]并返回NUnit进程的失败,同时报告测试全部通过。事实上,它试图反序列化自定义异常 - ProcessException - 并失败,这就是我认为这里所描述的[2]

[1]:http://www.mail-archive.com/nunit-core@lists.launchpad.net/msg00326.html

[2]:http://groups.google.com/forum/?fromgroups#!topic/nunit-discuss/7LF_X-yBWJ8

构建机器的输出:

[exec] Tests run: 147, Errors: 0, Failures: 0, Inconclusive: 0, Time: 27.6745774 seconds
[exec] Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0
[exec] Unhandled exceptions:
[exec] 1) Blah.ShelledProcessBackgroundRunnerTests.A_background_process_will_be_killed_if_the_timeout_is_exceeded : System.Runtime.Serialization.SerializationException: Unable to find assembly 'Blah, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
[exec] at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
[exec] at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
[exec] at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
[exec] at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
[exec] at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
[exec] at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
[exec] at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
[exec] at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
[exec] at System.Runtime.Remoting.Channels.CrossAppDomainSerializer.DeserializeObject(MemoryStream stm)
[exec] at System.AppDomain.Deserialize(Byte[] blob)
[exec] at System.AppDomain.UnmarshalObject(Byte[] blob)
[exec] c:\build_work\ourcibuild(229,26): External Program Failed: C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\nunit-console-x86.exe (return code was -100)

这是否准确反映了可能发生的事情? 我是否可以更改NUnit的行为以忽略未处理的异常,因为我在单元测试中处理它?<​​/ p>

0 个答案:

没有答案