Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between TestContext.Out and TestContext.Progress in NUnit 3?

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();

methods to log errors and test progress. Some effort was made to use the NUnit

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

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 */
}

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.

like image 561
Chris Avatar asked May 22 '17 16:05

Chris


People also ask

What is TestContext?

TestContext encapsulates the context in which a test is executed, agnostic of the actual testing framework in use.


1 Answers

The answer is indeed on the linked documentation page (https://github.com/nunit/docs/wiki/TestContext), but it's been a while since this question was asked so perhaps the documentation page has been improved since then.

At least now it says:

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.)

The keyword here is "immediate display" - when something is sent to Error and Progress the intention is that the test runner should display it immediately, while text sent to Out will not be visible until each test case has terminated.

This is because NUnit 3 will catch Console.Out and not output it until the test case finished. In NUnit 2, the regular Console.Out and Error was visible immediately.

like image 147
Oskar Berggren Avatar answered Sep 20 '22 13:09

Oskar Berggren