Reputation: 388
I have a DLL that's a bit on the complicated side that I'm writing tests against. Most of the classes under test have their own TraceSource
object that are used to output tracing information.
namespace MyDll
{
public class Class1
{
static TraceSource tracing = new TraceSource(nameof(Class1));
//Instance members and functions...
}
public class Class2
{
static TraceSource tracing = new TraceSource(nameof(Class2));
//Instance members and functions...
}
public class Class3
{
static TraceSource tracing = new TraceSource(nameof(Class3));
//Instance members and functions...
}
}
Inside of the MyDll
project is an App.config
file that adds a common ConsoleTraceListener
to all of the TraceSource
, like so:
<configuration>
<system.diagnostics>
<sources>
<source name="Class1"
switchName="sourceSwitch"
switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add name="console"/>
</listeners>
</source>
<source name="Class2"
switchName="sourceSwitch"
switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add name="console"/>
</listeners>
</source>
<source name="Class3"
switchName="sourceSwitch"
switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add name="console"/>
</listeners>
</source>
</sources>
<switches>
<add name="sourceSwitch" value="Verbose"/>
</switches>
<sharedListeners>
<add name="console"
type="System.Diagnostics.ConsoleTraceListener">
<filter type="System.Diagnostics.EventTypeFilter"
initializeData="Verbose"/>
</add>
</sharedListeners>
</system.diagnostics>
</configuration>
App.config
is configured to always copy to the output directory. I'm not sure if that's relevant.
Here's the problem: If I run 1 test, I get all of the tracing information in the Output from the test. However, if I run all of the tests, only the first test run will have the tracing from the code under test. All of the other tests do not have any output.
How can I get MSTest to output all tracing information from all TraceSource
for all tests?
Upvotes: 0
Views: 581
Reputation: 388
The problem lies with MSTest. Static objects in the test class will get picked up in the first test, but not in the following tests. For example,
public class SomeDependency
{
private static TraceSource tracing = new TraceSource("SomeDependency");
public void FunctionUsed()
{
tracing.TraceEvent(TraceEventType.Informational, 1, "This function is being used");
}
}
[TestClass]
public class TestClass
{
static SomeDependency dependency;
[ClassInitialize]
public static void Init(TestContext context)
{
dependency = new SomeDependecy();
}
[TestMethod]
public void Test1()
{
dependency.FunctionUsed();
}
[TestMethod]
public void Test2()
{
dependency.FunctionUsed();
}
}
Test1
will show the output from the TraceSource
in SomeDependency
, but Test2
will not. This is a limitation with the MSTest runner.
Upvotes: 0