问题
I'm getting an error (see bottom of post) when running NUnit against a production code assembly. The production code assembly has a reference to a third party framework that uses log4net internally (Specifically, this is SimplyAccounting's SDK). The version of log4net that it uses is 1.2.9.0. Outside of this third party framework which we use via the public api from the dll files, log4net is not part of our production code.
While troubleshooting, I eliminated the reference to the production code assembly from the unit test project and added a reference to the SimplyAccounting SDK. When I do this NUnit pukes out the following error. If the third party reference (or the production code reference) is not in the test project then Nunit runs fine. Any workarounds with a potential log4net conflict would be helpful. I'm using NUnit 2.5.2.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at log4net.Layout.LayoutSkeleton.get_Header()
at log4net.Appender.TextWriterAppender.WriteHeader()
at log4net.Appender.TextWriterAppender.set_Writer(TextWriter value)
--- End of inner exception stack trace ---
Server stack trace:
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
at NUnit.Core.Log4NetCapture.SetAppenderTextWriter(TextWriter writer)
at NUnit.Core.Log4NetCapture.StartCapture()
at NUnit.Core.TextCapture.set_Enabled(Boolean value)
at NUnit.Core.RemoteTestRunner.StartTextCapture(EventListener queue)
at NUnit.Core.RemoteTestRunner.BeginRun(EventListener listener, ITestFilter filter)
at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)
at System.Runtime.Remoting.Messaging.StackBuilderSink.PrivateProcessMessage(RuntimeMethodHandle md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)
at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at NUnit.Core.TestRunner.BeginRun(EventListener listener, ITestFilter filter)
at NUnit.Core.ProxyTestRunner.BeginRun(EventListener listener, ITestFilter filter)
at NUnit.Util.TestDomain.BeginRun(EventListener listener, ITestFilter filter)
at NUnit.Util.TestLoader.RunTests(ITestFilter filter)
at NUnit.UiKit.TestSuiteTreeView.RunTests(ITest[] tests, Boolean ignoreCategories)
at NUnit.UiKit.TestSuiteTreeView.RunSelectedTests()
at NUnit.UiKit.TestTree.RunSelectedTests()
at NUnit.Gui.NUnitForm.runButton_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
回答1:
One solution (although pretty disruptive and not solving the root issue) would be to have your production assembly talk to an interface that wraps the SimplyAccount SDK instead of talking directly to it. Then your production code doesn't need to reference the SDK dll and you could test it freely by passing in a fake imlementation of the interface you created.
This has the design benefit of being decoupled from that SDK, but like I said, it could be quite disruptive to your codebase.
来源:https://stackoverflow.com/questions/3796140/log4net-issues-with-nunit-tried-v2-5-7-2-5-2-2-4-8-2-4-7