Unit Tests and Debug.Assert don't come usually work well together, specially if Debug.Assert pops out a dialog window that stops the execution of tests. MbUnit features a new test decorator that "digests" Debug.Assert calls and "translate" them for MbUnit. Simply tag the test that could trigger Debug.Assert with ExpectedDebugAssertAttribute.
Debug.Assert
ExpectedDebugAssertAttribute
[TestFixture] public class ExpectedDebugAssertionAttributeTest { [Test] [ExpectedDebugAssertion] public void ThrowAssertion() { Debug.Assert(false,"Testing assertion redirection"); } }
In the background, the decorator stores the listener in a temporary collection, emptis the Debug.Listeners collection and adds a simple listener that will throw in case of failure. When finishes, Debug.Listeners is rolledback.
The test method ThrowAssertion will fail if we do not trap the exception with ExpectedExceptionAttribute. In fact, in MbUnit, you can chain decorators:
[TestFixture] public class ExpectedDebugAssertionAttributeTest { [Test] [ExpectedException(typeof(AssertionException))] // this will trap the exceptoin [ExpectedDebugAssertion] // this will filter Debug.Assert to AssertionException public void ThrowAssertion() { Debug.Assert(false,"Testing assertion redirection"); } }
Attention, the order of declaration of decorators is important!
Page rendered at Friday, August 29, 2008 12:25:09 AM UTC
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.