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

How do I get nunit3-console to output my debug to screen (on a Windows box)?

我已经使用 NUnit 运行 我在 Visual Studio 2013 中使用 NUnit GUI 和 NUnit 控制台成功进行了 C# 单元测试。

对于前两个,我可以获得调试输出 (Console.Write(...))。在 Visual Studio 2013 中,在测试后单击“输出”link 可以查看,在 GUI 中,调试显示在“输出”window。

当我 运行 使用 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");,我想看到它出现在屏幕上(标准输出)。

您的 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 尽管默认值(没有命令行选项)显示上述输出。