【问题标题】:Can not print from NUnit unit test无法从 NUnit 单元测试中打印
【发布时间】:2020-07-28 00:27:03
【问题描述】:

我有以下测试:

    [Test]
    public void RetrieveWrongURL()
    {
        Debug.WriteLine("In RetrieveWrongURL");
        Console.WriteLine("In RetrieveWrongURL");
        Assert.Throws<IncorrectUrlSuppliedException>(() => mRetriever.doSomething("https://someWrong.url"));
    }

当我运行测试时,在Output 窗口中我得到:

[05/01/2018 10:23:40 Informational] ------ Run test started ------
[05/01/2018 10:23:41 Informational] NUnit Adapter 3.9.0.0: Test execution started
[05/01/2018 10:23:41 Informational] Running selected tests in Z:\path\to\my\dll\MyDll.dll
[05/01/2018 10:23:41 Informational] NUnit3TestExecutor converted 3 of 3 NUnit test cases
[05/01/2018 10:23:42 Informational] NUnit Adapter 3.9.0.0: Test execution complete
[05/01/2018 10:23:42 Informational] ========== Run test finished: 1 run (0:00:02.0995689) ==========

如何在输出窗口中写入任何内容?

【问题讨论】:

标签: c# visual-studio


【解决方案1】:

尝试以下方法:

Nunit.Framework.TestContext.WriteLine("text");

注意:NUnit 控制台会在每次测试结束时显示测试的文本输出。测试运行结束时会生成一份总结报告。从 NUnit 3.0 开始就是这种情况。在此之前,输出会在生成后立即发送到控制台。

【讨论】:

    【解决方案2】:

    使用这个

    System.Diagnostics.Debug.WriteLine("something");
    

    【讨论】:

    • 或者您可以使用 Trace.WriteLine 并在测试运行器中获取输出,即使在 Release 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-25
    • 1970-01-01
    • 2021-10-16
    相关资源
    最近更新 更多