jeudi 31 mars 2016

Why are messages sent to trace source missing from all but the first unit test?

I observe a weird behavior with Visual Studio Enterprise 2015 Update 1. When using logging through .NET Framework TraceSource inside the unit tests, only the first unit test contains the logging output.

This is the way to reproduce the issue:

An empty unit test project contains the UnitTest1 class which looks like this:

using System;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class UnitTest1
{
    private readonly TraceSource trace = new TraceSource("Demo", SourceLevels.All);

    [TestMethod]
    public void TestMethod1()
    {
        this.trace.TraceEvent(TraceEventType.Information, 0, "Test 1 (trace source)");
        Console.WriteLine("Test 1 (console)");
    }

    [TestMethod]
    public void TestMethod2()
    {
        this.trace.TraceEvent(TraceEventType.Information, 0, "Test 2 (trace source)");
        Console.WriteLine("Test 2 (console)");
    }
}

The App.config file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <trace autoflush="true" />
    <sources>
      <source name="Demo" switchValue="Critical, Error, Warning, ActivityTracing, Information, Verbose">
        <listeners>
          <add name="ConsoleTraceListener" />
          <add name="TextFileListener" />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="ConsoleTraceListener" type="System.Diagnostics.ConsoleTraceListener" />
      <add name="TextFileListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="tests.log" />
    </sharedListeners>
  </system.diagnostics>
</configuration>

When running the tests from Visual Studio:

  • The output of the first unit test is:

    Test Name:  TestMethod1
    Test Outcome:   Passed
    Result StandardOutput:  
    Demo Information: 0 : Test 1 (trace source)
    Test 1 (console)
    
    

    Both the messages from trace source and console are shown.

  • The output of the second unit test is:

    Test Name:  TestMethod2
    Test Outcome:   Passed
    Result StandardOutput:  Test 2 (console)
    
    

    While the output from Console.WriteLine works well, the one from TraceSource.TraceEvent is not shown.

Running the tests from the console by executing mstest /testcontainer:UnitTestProject1.dll reproduces the problem as well. The stdout reported for the first test contains two lines; the second test's stdout has one line only.

Why isn't logging traced for all but the very first test?

Aucun commentaire:

Enregistrer un commentaire