gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 15:10:50 26 4
gpt4 key购买 nike

我已经使用 NUnit 在 Visual Studio 2013 中成功运行我的 C# 单元测试,使用 NUnit GUI 和 NUnit 控制台。
对于前两个,我可以访问我的调试输出(Console.Write(...))。在 Visual Studio 2013 中,可以通过在测试后单击“输出”链接进行查看,并且在 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");在其中,我想看到它出现在屏幕上(标准输出)。

最佳答案

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

关于c# - 如何让 nunit3-console 将我的调试输出到屏幕(在 Windows 机器上)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46405753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com