Disable System.Diagnostics.Debug.Assert dialogs

While executing my integration tests today, I was greeted with the excellent Assertion.Failed dialog which is displayed when using System.Diagnostics.  This dialog caused a problem on the build server as it was waiting for someone to click Ignore – that someone never came!


If you haven’t saw the Debug.Assert code before, it basically looks like this:

public void DebugAssert()

When the Assert fails, the dialog is displayed.

The solution, add an App.Config file to your test suite.  Within the XML will clear the trace listeners, now when assert is called nothing happens because nothing is listening for it and the code continues. While executing unit tests this is fine – we just don’t want the dialog being displayed.

If you was interested in the debug.assert messages, you could have hooked up a different listener to write out the message to the console or a file.  This is what TestDriven.NET does, if an assertion is hit it writes the message to the output window.

Technorati Tags: ,

4 thoughts on “Disable System.Diagnostics.Debug.Assert dialogs”

  1. I hit exactly the same problem a while back… which duly “froze” the build for ages until I realised what was happening 🙂

    You can also set the flag programmatically, should you want to:

    public void Setup()
    if (Environment.CommandLine.Contains(“nunit-console”))
    ((DefaultTraceListener)Debug.Listeners[0]).AssertUiEnabled = false;

  2. In my first attempt I had very similar code in the TestFixtureSetUp.

    After 4 times of copying
    public void Setup () { CustomDebugAssert.Disable(); } I got bored and looked for alternatives 🙂

    MbUnit has a [AssemblySetUp] stage which would have been perfect, sadly NUnit doesn’t.

  3. If an Assert fails, why would one want to suppress that? That is, in effect, unit testing on-the-spot. I submit that all Asserts should pass always, if it is being used properly, then and only then should Nunit tests even be considered. No? If not that, then what would you do differently? The whole point of putting in an Assert is so that one does not miss it– so why turn it off?

Leave a Reply

Your email address will not be published. Required fields are marked *