gpt4 book ai didi

c# - NUnit 3 中的 TestContext.Out 和 TestContext.Progress 有什么区别?

转载 作者:太空宇宙 更新时间:2023-11-03 12:27:30 24 4
gpt4 key购买 nike

我正在重构一个使用 NUnit 3.6 的测试应用程序。大多数应用程序是由非开发人员编写的,需要重构/重组。有很多使用标准的遗留助手类

Console.Out.WriteLine();
Console.Error.WriteLine();

记录错误和测试进度的方法。为使用 NUnit 做出了一些努力

TestContext.Progress.WriteLine();
TestContext.Out.WriteLine();
TestContext.Error.WriteLine();

在测试运行时提供“实时输出”的方法,但并不一致。展望 future ,我希望所有帮助程序类/方法都使用 Console.Out/Error 方法,这样它们就可以在没有 NUnit 依赖项的情况下分离,然后在每个测试的 [SetUp] 方法中重定向输出。

[SetUp]
public void BaseTestSetupMethod(){
Console.SetError(TestContext.Error);
/* Set Console.Out to TestContext.Out or TestContext.Progress */
}

TestContext.Out 和 TestContext.Progress 有什么区别?

我应该将 Console.Out 重定向到哪一个以获取一般消息,例如打印当前正在运行的测试的名称及其正在执行的操作?

我通读了 NUnit 3 的文档,包括 https://github.com/nunit/docs/wiki/TestContext页。虽然它列出了 TestContext.OutTestContext.Progress 之间的区别,但它们并不是真正的描述性的,也没有示例说明为什么您会在实践中使用一个而不是另一个。

最佳答案

答案确实在链接的文档页面 ( https://github.com/nunit/docs/wiki/TestContext ) 上,但是自从提出这个问题以来已经有一段时间了,所以也许从那时起文档页面已经得到了改进。

至少现在它说:

Out

Gets a TextWriter used for sending output to the current test result.

Error

Gets a TextWriter used for sending error output intended for immediate display.

Progress

Gets a TextWriter used for sending normal (non-error) output intended for immediate display.)

这里的关键词是“立即显示”——当某些东西被发送到 Error 和 Progress 时,意图是测试运行器应该立即显示它,而发送到 Out 的文本在每个测试用例终止之前是不可见的。

这是因为 NUnit 3 将捕获 Console.Out 并且在测试用例完成之前不会输出它。在 NUnit 2 中,常规的 Console.Out 和 Error 立即可见。

关于c# - NUnit 3 中的 TestContext.Out 和 TestContext.Progress 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44118127/

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