Personally, I am not keen on embedding tracing in unit tests (using either method you mention). If a unit test requires this, it is most likely a sign that your unit test is too complex. If you need to trace logic through the unit test, you should use asserts throughout the test to programmatically check that the expected behaviour is occurring, removing the need for the textual tracing output.
However, you need to be pragmatic - it is useful to do this sometimes. Using either method (or something else like Debug.WriteLine
) is fine, but which one you use does give you some flexibility.
If you have a lot of tests that output tracing, you can get a lot of tracing output when you run all of your tests in a single run. Within NUnit, you can filter this in the options page:
The four options do the following ref:
- Standard Output: Captures all output written to Console.Error.
- Error Output: Captures all output written to Console.Error.
- Trace Output: Captures all output written to Trace or Debug.
- Log Output: Captures output written to a log4net log. NUnit captures all output at the Error level or above unless another level is specified for the
DefaultLogThreshold
setting in the configuration file for the test assembly or project.
By turning off these options, you can individually disable the output of tracing sent to the four different logging methods, enabling you to filter your test tracing.
I am not aware of any similar setting in ReSharper's test runner.
One thing also worth considering is that the text output can have side effects. I recently encountered NUnit crashing because some output contained characters that were illegal in an XML file - NUnit produces one as part of our autobuild.
EDIT:
@Bronumski: The only real difference I can see of using one method over another is the way the output is consumed.
Certain tools will pick up Debug
tracing (eg. DebugView) but not Console
output. Also, you can disable Trace
output at runtime via configuration (in app.config), but not Console
output. This will only matter though if you have to decorate real (ie. not test) code with tracing for your tests - logging lots of text at runtime can be costly and so is beneficial if it can be turned off unless you really need it to diagnose something.
Additionally, when using NUnit, you can selectively turn them off independently of each other if you have too much logging to wade through.