Pex It: Don't mix preconditions and assertions
Pex It: Don't mix preconditions and assertions
Submitted by Jonathan de Halleux on Wed, 13/02/2008 - 18:51.This is a general recommendation if you're planning to use a tool like Pex in the future: make sure that preconditions (i.e. parameter validation) fails in a different fashion that other assertions.
Here's a snippet that shows the problem:
// don't do this
void Clone(ICloneable o) {
Debug.Assert(o != null); // pre-condition
...
object clone = o.Clone();
Debug.Assert(clone); // assertion
}
Why is this bad?
A tool like Pex will explore your code and try to trigger every Debug.Assert it finds on its way. When the assertion is a precondition, it is likely expected and one would like to emit a negative test case (i.e. 'expected exception').
The problem in the snippet above is that both failure will yield to the same assertion exception and it will very difficult to *automatically* triage the failure as expected or not.
How do I fix this?
Make sure different classes of assertions can be differentiated automatically, through different exception types, tags in the message, etc...
This posting is provided "AS IS" with no warranties, and confers no rights.
