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

The Evil Greebo picture The Evil Greebo · Oct 24, 2012 · Viewed 32.1k times · Source

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?

Answer

Randy Larson picture Randy Larson · Jan 27, 2016

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.