Click here to Skip to main content
Click here to Skip to main content

A TraceListener For Tests

By , 15 Apr 2010
Rate this:
Please Sign up or sign in to vote.

In my code, I make extensive use of debug assertions (see System.Diagnostics.Debug.Assert). These assertions are very helpful while debugging because you don't need to step into every line of code to see if all pre-conditions are met. As soon as a pre-condition fails, an assertion failed window will pop up and will allow us to either abort, ignore or go to the assertion instruction (retry).

Imagine you have this code:

private void IKnowForSureThatANullStringWillNeverBePassed(string text)
{
    System.Diagnostics.Debug.Assert(string != null, "text is null.");

    // ...
}
free hit counters

Because this method is private, I have full control of what is passed to the text parameter, therefore I'm asserting that it will never be null. Because it might not be obvious that text will never be null, the assertion also acts as documentation.

I usually run my unit tests and integration test on debug compilations and these assertions would be helpful by making the test fail on an assertion fail instead of running through the method and failing with an NullReferenceException. That’s why I (and more people out there) wrote this simple TraceListener:

public class TraceListener : global::System.Diagnostics.TraceListener
{
    public static readonly TraceListener Default = new TraceListener();

    protected TraceListener()
    {
        this.Name = "Testing Trace Listener";    
    }

    protected TraceListener(string name)
        : base(name)
    {
    }

    public override void Write(string message)
    {
    }

    public override void WriteLine(string message)
    {
    }

    public override void Fail(string message, string detailMessage)
    {
        var builder = new global::System.Text.StringBuilder();

        builder.Append(message);

        if (detailMessage != null)
        {
            builder.Append(" ");
            builder.Append(detailMessage);
        }

        throw new global::
	Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException
	(builder.ToString());
    }
}

This trace listener won't write anything to anywhere. It will just throw an AssertFailedException if the Fail method is called, which is what happens when an assertion fails.

Because an assertion window is not desired when running the tests (specially if they are automated tests run as part of a build process), it’s better to disable the assert UI on the configuration file of the test project.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.diagnostics>
        <assert assertuienabled="false"/>
        <trace>
            <listeners>
                <add name="TestTraceListener"
                	type="PauloMorgado.TestTools.VisualStudio.UnitTesting.
		Diagnostics.TraceListener, PauloMorgado.TestTools.VisualStudio" />
            </listeners>
        </trace>
    </system.diagnostics>
</configuration>

You can find this and other tools on the PauloMorgado.TestTools on CodePlex.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Paulo Morgado
Software Developer (Senior) Paulo Morgado
Portugal Portugal

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 15 Apr 2010
Article Copyright 2010 by Paulo Morgado
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid