As Henk already noted, suppressing the UI is useless, because you want your that code to fail. When you don't want to change your code, you can write a custom trace listener that throws an exception, as follows:
public class ProductionTraceListener : DefaultTraceListener
{
public override void Fail(string message, string detailMessage)
{
string failMessage = message;
if (detailMessage != null)
{
failMessage += " " + detailMessage;
}
throw new AssertionFailedException(failMessage);
}
}
[Serializable]
public class AssertionFailedException : Exception
{
public AssertionFailedException() { }
public AssertionFailedException(string message) : base(message) { }
public AssertionFailedException(string message, Exception inner)
: base(message, inner) { }
protected AssertionFailedException(SerializationInfo info,
StreamingContext context) : base(info, context) { }
}
And you can register it as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace>
<listeners>
<clear />
<add name="Default"
type="[Namespace].ProductionTraceListener, [Assembly]" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
As you already might expect from the trace listener's name ProductionTraceListener
, I use that thing in my production environment (web applications), and not in my unit tests. While you can use this trick in your unit tests, I advice you to change your code. IMO you should only use asserts for code paths that should never run, and if they do, the test should fail. In your situation you want to have a succeeding test when a assert fails, which is counterintuitive.
My advice is to change the code and use normal if (x) throw ArgumentException()
checks for preconditions (or use CuttingEdge.Conditions) and use those asserts only for code paths that should never run. Also try using Trace.Assert
instead of Debug.Assert
, because you also want those asserts to be checked in your production environment. When you've done that you can use the ProductionTraceListener
in your production environment, and this UnitTestTraceListener
in your unit tests.
public class UnitTestTraceListener : DefaultTraceListener
{
public override void Fail(string message, string detailMessage)
{
string failMessage = message;
if (detailMessage != null)
{
failMessage += " " + detailMessage;
}
// Call to Assert method of used unit testing framework.
Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Fail(
failMessage);
}
}
Good luck.