samedi 26 mars 2016

VS 2013 Unit test doesnt share TraceSource between tests

I have few unit tests (VS 2013 Test Project). they tests classes which all uses a single instance of a class called Logger:

TraceSource trace;    
public void Trace(TraceEventType level, string format, params object[] args)
{
    trace.TraceEvent(level, id, format, args);
}

The test class has this:

[TestInitialize]
public void Init()
{
    var traceSource = new TraceSource("traceSource");
    x.Log = new Logging(traceSource);
}

where Log is a static property somewhere.

If I debug one test method from Test Explorer - it works fine. If I select several methods and debug all - it works for the first method and then it stops.

Each Trace throws ObjectDisposedException. If you dig into the exception it says something about not able to write into a closed TextWriter.

The app.config is like this:

  <source name="traceSource" switchValue="Verbose">
    <listeners>
      <add name="consoleListener" type="System.Diagnostics.ConsoleTraceListener"/>
    </listeners>
  </source>

So it appears that it somehow cleanups between test runs.. but it runs Init once only.

What is the life cycle of the MS unit tests?

EDIT: I changed [TestInitialize] to [ClassInitialize].. Apparently I misunderstood what TestInitialize does... But the problem is still the same.

Aucun commentaire:

Enregistrer un commentaire