Chr*_*ris 6 c# console nunit unit-testing nunit-3.0
I'm working on refactoring a testing application that uses NUnit 3.6. Most of the application was written by non-developers and is in need of refactoring/re-organization. There are a lot of legacy helper classes that use the standard
Console.Out.WriteLine();
Console.Error.WriteLine();
Run Code Online (Sandbox Code Playgroud)
methods to log errors and test progress. Some effort was made to use the NUnit
TestContext.Progress.WriteLine();
TestContext.Out.WriteLine();
TestContext.Error.WriteLine();
Run Code Online (Sandbox Code Playgroud)
methods to provide "live output" as test run, but it's not consistent. Moving forward, I want all helper classes/methods to use the Console.Out/Error methods so that they can be detached without the NUnit dependency and then redirect the output in the [SetUp] methods for each test.
[SetUp]
public void BaseTestSetupMethod(){
Console.SetError(TestContext.Error);
/* Set Console.Out to TestContext.Out or TestContext.Progress */
}
Run Code Online (Sandbox Code Playgroud)
What is the difference between TestContext.Out and TestContext.Progress?
Which one should I redirect Console.Out to for general messages like printing the name of the current running test and the operations that it's doing?
I read through the documentation for NUnit 3, including the https://github.com/nunit/docs/wiki/TestContext page. While it lists the difference between TestContext.Out and TestContext.Progress, they are not really descriptive and have no examples of why you would use one over the other in practice.
答案确实在链接的文档页面(https://github.com/nunit/docs/wiki/TestContext)上,但自从提出这个问题以来已经有一段时间了,所以也许文档页面从那时起已经得到了改进。
至少现在它说:
出去
获取用于将输出发送到当前测试结果的 TextWriter。
错误
获取一个 TextWriter,用于发送旨在立即显示的错误输出。
进步
获取一个 TextWriter,用于发送用于立即显示的正常(非错误)输出。)
这里的关键字是“立即显示” - 当某些内容发送到 Error 和 Progress 时,目的是测试运行程序应立即显示它,而发送到 Out 的文本在每个测试用例终止之前才可见。
这是因为 NUnit 3 将捕获 Console.Out 并且在测试用例完成之前不会输出它。在 NUnit 2 中,常规 Console.Out 和 Error 立即可见。
归档时间: |
|
查看次数: |
3935 次 |
最近记录: |