Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add log messages to an NUnit test that will appear in a nant build execution?

With NUnit, if you add Debug.Print statements in your tests they appear in the test output. (At least they do in the ReSharper unit test window.)

When using a NAnt build file that executes the same tests, those Debug.Print statements do not appear.

How can I add messages to my unit tests that will appear both in the NUnit output and the build log output from NAnt?

like image 529
The Evil Greebo Avatar asked Oct 24 '12 12:10

The Evil Greebo


People also ask

Which attribute is used to mark the test methods for unit testing in NUnit?

The Test attribute is one way of marking a method inside a TestFixture class as a test.

What is NUnit test runner?

The nunit.exe program is a graphical runner. It shows the tests in an explorer-like browser window and provides a visual indication of the success or failure of the tests. It allows you to selectively run single tests or suites and reloads automatically as you modify and re-compile your code.

How does NUnit decide what order to run tests in?

There is no facility in NUnit to order tests globally. Tests with an OrderAttribute argument are started before any tests without the attribute. Ordered tests are started in ascending order of the order argument. Among tests with the same order value or without the attribute, execution order is indeterminate.


2 Answers

NUnit 3's way of logging during a test is via the TestContext class. It has a raft of static Write variants. Each emits general content to the test result.

TestContext.Out yields a TextWriter that can also be used to emit logging information into test results.

like image 143
Randy Larson Avatar answered Oct 19 '22 22:10

Randy Larson


Console.WriteLine() should be preserved (it is for my version of NUnit at least).

That said, consider - each time you want to add some text output - how you could turn it into an assertion with a message. Your tests will get much better.

like image 23
skolima Avatar answered Oct 19 '22 23:10

skolima