我在 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 的行为以忽略未处理的异常吗?