First, for a unit test to hit an assert
(or an ASSERT
or _ASSERT
or _ASSERTE
on Windows builds), the unit test would need to run the code under test with the debug build.
I guess this can easily happen on a developer's machine. For our nightly builds, we only run the unit tests in the release configuration, so there's no worries about asserts there.
Second, one can take the normative approach with asserts --
Asserts are meant to ensure that certain conditions / invariants are
always valid during the lifetime of the program. Or to be more
precise, to ensure that if such a condition gets broken, we get to
know about it ASAP, as close to the root cause of the problem as
possible.
In this case, no unit test should raise an assertion, because calling the code in a way that an assertion is raised should not be possible.
or one can take the "pragmatic" approach with assertions:
Let developers sprinkle ASSERT all over the place for "don't do this" and "not implemented" scenarios. (And we can argue all day whether that's wrong™ or right™, but that won't get the features delivered.)
If you take the pragmatic approach, then a unit test hitting an assertion means the unit test called the code in a way that is not quite supported by the code. It just may mean that the code "does nothing" in a release build or it may mean that the code crashes in a release build or it may mean that the code does "something interesting".
Here's the options that I have been known to use:
- If the assert is accompanied by an additional check to make the call "harmless", make the unit test test for the assertion (in debug) and for the "harmless" condition in release.
- For crashes or "something interesting", either there's no unit test that makes sense, or you can make a "debug only" unit test that tests that you really get an assertion (though I'm not so sure that's helpful).