When we write unit tests we try to cover all edge cases, not just run through the cases that work. That means that in many cases the product code will assert before throwing an exception.
We have to separate cases here
1. We expected the assert and want to ignore it
2. We did not expect an assert, and in this case we don’t want to get the unit test stalled, but rather simply fail.
In this blog I’m suggesting a rather simple approach for dealing with this issue that will only require changes to the unit tests for case 1 above. The approach is relevant for MsTest based unit tests, although it can apply to other frameworks as well with some minor modifications.
When we call Debug.Assert, Debug.Fail, Trace.WriteLine or similar methods, the resultant messages are routed to the static list of trace listeners (System.Diagnostics.Trace.Listeners). One of these listeners is the DefaultTraceListener that among other things pops up the Assert Dialog in debug mode.
For more information:
Obviously when we run unit tests we don’t want this dialog to come up, because it will stall the overall run. If we are running hundreds of unit tests and a few of them pop asserts, we will be sitting for a long time waiting for results to come back (or interactively keep clicking ignore).
Thankfully we can modify this static list on run time to suit our needs.
A Basic solution
In this solution we simply iterate through the list of listeners (Debug.Listeners) and find the DefaultTraceListener and remove it.
Note that I’m not assuming it’s the first listener, since I don’t want to make assumptions that this list was never manipulated.
This code can be added before encountering an assert in the body of the test, but it poses a few limitations
1. It will remove the listener for the tests to follow
2. You have to put it in any test that fires asserts because there is no guarantee in what order or even if all tests are run
Fortunately we can make this a more global approach
In this approach I create another test class with the sole purpose of initializing the complete test assembly. It will now run for every test regardless if that test is run on it’s own or as part of a group run.
Note that the Initialize method is decorated with [AssemblyInitialize] and is within a [TestClass]
Still with this approach asserts will now be ignored, and the only way to see that they got fired is the Trace section of the test result, which will typically be ignored if the test passed.
Fail on assert (well sometimes…)
Most of the time we want asserts to fail the tests but not always, sometimes when testing edge cases we do expect the product to assert, and we don’t want the test to fail in these cases.
So the following FailOnAssert class got created, and we add it to the DebugListerner list (this line goes below line 26 in the above code snippet)
Fail on assert is a TraceListener, in order to do that I derived it from a TraceListener. And overridden the Fail, Write and WriteLine methods. Since the calls to Debug.Assert and Debug.Fail ultimately turn into calls to Fail, they call Assert.Fail and stop the test.
Note that Write and WriteLine are abstract in the TraceListener base class and have to be overridden although they don’t serve any purpose in this case.
The Disable property is used to temporarily allow asserts to fire without failing the tests, the _disable member is decorated with the [ThreadStatic] attribute since we don't want a test on one thread inadvertently changing this behavior for another thread.
The messages logged by any product trace calls are logged by the unit test framework listeners and hence I don't need to implement any logging in my code.
Using the Disabled Property:
Unexpected asserts should not be ignored or stall the tests, and should trigger a test failure.
By simply adding this code to any existing unit tests project we where able to catch all asserts being fired without having to edit file by file, the code was generic enough to simply drop two files into each assembly and be mostly done.