Unit testing Debug.Assert/Trace.Assert System.Diagnostics statements

I was giving some thought to how to test the good old Debug.Assert and Trace.Assert and found a decent way to do it.
 

using System;

using

System.Diagnostics;

using

NUnit.Framework;

namespace UnitTestingDebugStatements {

    public class Foo {

        public Foo(string name) {

            Debug.Assert(!String.IsNullOrEmpty(name), "name cannot be null or empty");

        }

    }

 

    public delegate void TraceListenerHandler(object sender, EventArgs args);

 

    public class MyTraceListener : TraceListener {

        EventHandler handler;

        public MyTraceListener(EventHandler handler) {

            this.handler = handler;

        }

        public override void Write(string message) {

            handler(this, new EventArgs());

        }

        public override void WriteLine(string message) {

            handler(this, new EventArgs());

        }

    }

 

    [TestFixture]

    public class FooTestFixture {

        Foo foo;

        bool assertSuccessful;

        MyTraceListener listener;

 

        public FooTestFixture() { }

 

        [SetUp]

        public void SetUp() {

            assertSuccessful = true;

            listener = new MyTraceListener(

                delegate(object sender, EventArgs args) { assertSuccessful = false; });

            Debug.Listeners.Add(listener);

        }

 

        [TearDown]

        public void TearDown() {

            Debug.Listeners.Remove(listener);

        }

 

        [Test]

        public void DebugAssertCausesValidationFailure() {

            Assert.IsTrue(assertSuccessful);

            try {

                foo = new Foo(null);

            } catch { }

            Assert.IsFalse(assertSuccessful);

        }

    }

}

 

The drawback is two-fold. First, when run in nunit-console, a message pops up that I haven’t been able to figure out how to supress. Second, when run through fxcop, it states you still haven’t validated the parameter passed in (in this case, the ‘name’ parameter of the constructor in the Foo class). That leave you with the only option, to change the Assert statement to evaluate and throw an exception and test for the expected exceptions in two separate NUnit tests.

 
e.g.

    public class Foo {

        public Foo(string name) {

            if (name == null) throw new ArgumentNullException("name");

            if (name.Length == 0) throw new

                    ArgumentException("name must be at least 1 character in length");

        }

    }

 

 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s