Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net - LogicalThreadContext - and unit test cases

I am starting to write a unit test (MS Test, with Resharper as the test runner). When I set the LogicalThreadContext (see below), my test cases get 'aborted'. Anybody know why? Is this related to the unit test being on a different thread? How do I resolve this?

[TestClass] public class ContextInfoTest {     private ILog _log;       [TestInitialize]     public void TestInitialize()     {         // logging configured in assembly.info         _log = LogManager.GetLogger(this.GetType());      }       [TestMethod]     public void FigureOutWhyAborting()     {         string input = "blah";         LogicalThreadContext.Properties["mypropertyname"] = input;          string output = LogicalThreadContext.Properties["mypropertyname"] as string;         Assert.AreEqual(input, output);     }       [TestMethod]     public void ThisWorks()     {         string input = "blah";         CallContext.LogicalSetData("mypropertyname", input);          string output = CallContext.LogicalGetData("mypropertyname") as string;         Assert.AreEqual(input, output);     } 

The weird thing is that if I were to debug and step through the code, the Assert.AreEqual does get called and passes, so something is happening after that line of code... which is why I think it might have something to do with the test thread, etc.

Thanks!

UPDATE: So I ran this test in MSTest and got this exception (Resharper didn't show it)

Unit Test Adapter threw exception: Type is not resolved for member 'log4net.Util.PropertiesDictionary,log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a'..

I'm using log4net v1.2.13, on VS2013, .Net 4.5.

This link seems to suggest it is a referenced assemblies problem, but there is no resolution. Any additional ideas would be greatly welcome, GAC'ing log4net is not an option. https://issues.apache.org/jira/browse/LOG4NET-398

like image 974
Raymond Avatar asked May 14 '14 17:05

Raymond


People also ask

How do you come up with unit test cases?

Follow Arrange, Act, Assert The AAA is a general approach to writing more readable unit tests. In the first step, you arrange things up for testing. It's where you set variables, instantiate objects, and do the rest of the required setup for the test to run. During this step, you also define the expected result.

When unit test cases should be written?

Unit Testing is done during the development (coding phase) of an application by the developers. Unit Tests isolate a section of code and verify its correctness. A unit may be an individual function, method, procedure, module, or object.

What is unit testing test cases?

A unit test is a way of testing a unit - the smallest piece of code that can be logically isolated in a system. In most programming languages, that is a function, a subroutine, a method or property. The isolated part of the definition is important.

What is unit testing stackoverflow?

Unit testing is the process of writing code to test the behavior and functionality of your system. Obviously tests improve the quality of your code, but that's just a superficial benefit of unit testing.


2 Answers

I ended up doing this to get it working:

put this in the TestCleanup() method:

CallContext.FreeNamedDataSlot("log4net.Util.LogicalThreadContextProperties"); 
like image 133
Raymond Avatar answered Sep 18 '22 04:09

Raymond


So, I can't thank you enough for figuring this out to call FreeNamedDataSlot. This turned me on to my answer that worked for me. Instead of passing in the full namespace of the class, I just had to use the class name:

This was used somewhere deep in my Data Access Layer:

MySession session  = (MySession)System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("MySession");  [TestCleanup] public void Cleanup() {     CallContext.FreeNamedDataSlot("MySession"); } 

This worked perfect for me! Hopefully this helps someone else when using Visual Studio's Test environment.

like image 30
Ben Humphrey Avatar answered Sep 19 '22 04:09

Ben Humphrey