VS2015:Test Explorer中的TraceSource输出?

时间:2015-11-05 02:38:12

标签: unit-testing visual-studio-2015 system.diagnostics tracelistener tracesource

我有一个我试图进行单元测试的图书馆。我使用TraceSource写出警告细节,因为我将单元测试设置为构建的一部分,我希望它们在测试运行时显示 - 特别是在失败时。

我已尝试使用以下代码注册跟踪侦听器,但我的WriteLine内容永远不会被调用,尽管调用了构造函数。请参见底部的代码[1]。

如何让我的痕迹显示为测试跑步者的一部分?

[1]我的testcontexttracelistener的代码:

[TestClass]
public class TestContextTraceListener : TraceListener
{
    TestContext testContext;

    public TestContextTraceListener(TestContext testContext)
    {
        this.testContext = testContext;
    }

    public override void Write(string message)
    {
        // EP TODO: This is likely going to create newlines where they shouldn't be, but testContext doesn't have a .Write, and I'm too lazy to buffer.
        this.WriteLine(message); 
    }

    public override void WriteLine(string message)
    {
        this.testContext.WriteLine(message);
    }
}

[TestClass]
public class TestAssemblyInitializeAndCleanup
{
    static TraceListener traceListener;

    [AssemblyInitialize]
    public static void OnInitialize(TestContext testContext)
    {
        if (traceListener == null)
        {
            // This code is reached, but the listener never gets triggered.
            traceListener = new TestContextTraceListener(testContext);
            Trace.Listeners.Add(traceListener);
        }
    }

    [AssemblyCleanup]
    public static void OnCleanup()
    {
        Cleanup();
    }

    static void Cleanup()
    {
        if (traceListener != null)
        {
            traceListener.Flush();
            Trace.Listeners.Remove(traceListener);
            traceListener.Dispose();
        }
    }
}

0 个答案:

没有答案