If you use Nunit and use standard System.Diagnostics.Debug.Assert in your code, you can get tired of the messagebox that is raised when a standard assertion fail. To avoid this, you can use app.config to completely remove all listener during the test.
1
2
3
4
5
6
7
| <system.diagnostics>
<trace autoflush="false" indentsize="4">
<listeners>
<clear/>
</listeners>
</trace>
</system.diagnostics>
|
This solves the problem, now you can launch nunit interface, run the test and get rid of the annoying messagebox that gets displayed when a standard assertion fail. If you do not want to use app.config, you can still create a base test class, and clear the listeners in the fixture setup.
1
2
3
4
5
6
7
8
9
10
11
12
| readonly List<TraceListener> oldListener = new List<TraceListener>();
[TestFixtureSetUp]
public void FixtureSetup() {
foreach (TraceListener listener in Trace.Listeners)
oldListener.Add(listener);
Trace.Listeners.Clear();
}
[TestFixtureTearDown]
public void FixtureTearDown()
{
Trace.Listeners.AddRange(oldListener.ToArray());
}
|
With this code you get the same effect, except that now you can disable listeners per test instead of disabling for entire test suite.
This technique has a drawback, in this way you completely disable standard assertions, but I like that my tests fail even if a standard assertion fails, and it is good to make possible for me to choose if in a test I want that standard assertion make my test fail or not. A quick and dirty solution is the following
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| public class NunitTraceListener : TraceListener
{
public override void Fail(string message)
{
NUnit.Framework.Assert.Fail("StandardAssertionFailed: " + message);
}
public override void Fail(string message, string detailMessage)
{
NUnit.Framework.Assert.Fail("StandardAssertionFailed: " + message);
}
public override void Write(string message)
{
Console.Write(message);
}
public override void WriteLine(string message) {
Console.WriteLine(message);
}
}
|
This class inherits from standard TraceListener, standard messages goes to the console (nunit is able to intercept them) and I override the two Fail() method that are called when some assert is failing. In my fail routines I simply make a standard fail assertion of nunit framework, this makes my test fail. The only thing you need to do is to set this listener in the trace.listeners collection
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| readonly List<TraceListener> oldListener = new List<TraceListener>();
[TestFixtureSetUp]
public void FixtureSetup()
{
foreach (TraceListener listener in Trace.Listeners)
oldListener.Add(listener);
Trace.Listeners.Clear();
Trace.Listeners.Add(new NunitTraceListener());
}
[TestFixtureTearDown]
public void FixtureTearDown()
{
Trace.Listeners.Clear();
Trace.Listeners.AddRange(oldListener.ToArray());
}
|
alk.
Tags: nunit TraceListener