如何让nunit3-console将我的调试输出到屏幕(在Windows框中)?

cod*_*der 0 c# nunit nunit-console

我已经使用nunit在MSVS2013中成功运行我的C#单元测试,使用nunit GUI和nunit控制台.

对于前两个,我可以进入我的调试输出(Console.Write(...)).在MSVS2013中,可以通过在测试后单击"输出"链接查看,在GUI中,调试显示在"输出"窗口中.

当我使用nunit3-console.exe运行时,我只是得到摘要报告.我试着查看XML输出中的内容(在TestResults.xml中),但它只包含更多相同的摘要报告.

如何让我的调试也出现在屏幕上?

我的命令行如下所示:

nunit3-console.exe "c:\path\to\my\assebly.dll" --trace=Verbose --test=Regression.Tests.HelloWorld
Run Code Online (Sandbox Code Playgroud)

测试HelloWorld中有一行Console.Write("Hello World\r\n");,我想看到它出现在屏幕上(标准输出).

Rob*_*use 12

Console.WriteLine(...)应该出现在输出中,但在NUnit 3中,您应该TestContext.WriteLine(...)在测试中使用.由于NUnit 3能够并行运行测试,因此它会捕获该输出,然后在测试完成后将其输出到控制台.这样,输出与测试匹配,而不与其他测试交错.

如果您希望输出在写入时立即熄灭,请使用TestContext.Progress.WriteLine(...).例如,以下测试,

    [Test]
    public void ExampleOfConsoleOutput()
    {
        Console.WriteLine("Console.WriteLine In ExampleOfConsoleOutput");
        TestContext.WriteLine("TestContext.WriteLine In ExampleOfConsoleOutput");
        TestContext.Progress.WriteLine("TestContext.Progress.WriteLine In ExampleOfConsoleOutput");
    }
Run Code Online (Sandbox Code Playgroud)

输出以下内容,

=> nunit.v3.TestNameInSetup.ExampleOfConsoleOutput
TestContext.Progress.WriteLine In ExampleOfConsoleOutput
Console.WriteLine In ExampleOfConsoleOutput
TestContext.WriteLine In ExampleOfConsoleOutput
Run Code Online (Sandbox Code Playgroud)

请注意,Progress消息是在其他输出之前输出的,即使它在测试中是最后一个.

您也不需要--trace命令行选项,即NUnit内部跟踪.控制输出的命令行选项--labels虽然默认值(无命令行选项)显示上述输出.