Search code examples
log4netmstestnullreferenceexception

TestMethod is crashing the Test Runner in MSTest


I'm running a set of tests in MSTest, and an exception is happening AFTER a specific test completes and runs TestCleanup, which causes the test runner to crash and not run any more tests. I have five tests that do this, and they look almost the same as some tests that run correctly.

This is the error in the Output Test window:

[11/16/2020 5:36:14 PM Informational] ------ Run test started ------
[11/16/2020 5:36:55 PM Error] An exception occurred while invoking executor 'executor://mstestadapter/v2': Object reference not set to an instance of an object.
[11/16/2020 5:36:59 PM Informational] ========== Run test finished: 0 run (0:00:45.5280936) ==========

This is what I see in the Output Debug window:

Exception thrown: 'System.NullReferenceException' in log4net.dll
Exception thrown: 'System.NullReferenceException' in Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.dll
Exception thrown: 'System.NullReferenceException' in Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.dll
'testhost.x86.exe' (CLR v4.0.30319: Domain 3): Unloaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'
…
The thread 0xdd68 has exited with code 0 (0x0).
Exception thrown: 'System.NullReferenceException' in Microsoft.TestPlatform.PlatformAbstractions.dll
The thread 0xdc80 has exited with code 0 (0x0).
The program '[57424] testhost.x86.exe' has exited with code 0 (0x0).

I can see that log4net is set to OFF in the app.config. What could be the problem that is causing the test runner to crash?


Solution

  • log4net was the culprit. The logger was initialized and needed to be properly cleaned up after the test.

      [TestCleanup]
      public virtual void TestCleanup()
      {
          //// To avoid this error caused by log4net v1.2.13: An exception occurred while invoking executor 'executor://mstestadapter/v1': Type is not resolved for member 'log4net.Util.PropertiesDictionary,log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a'.
          CallContext.FreeNamedDataSlot("log4net.Util.LogicalThreadContextProperties");
      }