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

时间:2017-09-25 12:52:59

标签: c# nunit nunit-console

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

前两个我可以进入我的调试输出(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

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

1 个答案:

答案 0 :(得分:10)

您的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");
    }

输出以下内容,

=> nunit.v3.TestNameInSetup.ExampleOfConsoleOutput
TestContext.Progress.WriteLine In ExampleOfConsoleOutput
Console.WriteLine In ExampleOfConsoleOutput
TestContext.WriteLine In ExampleOfConsoleOutput

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

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

相关问题