Pex - Automated Whitebox Testing for .NET : assumptions and assertions

Assumptions and Assertions


Users can use assumptions and assertions to express preconditions (assumptions) and postconditions (assertions) about their tests. When Pex generates a set of parameter values and 'explores' the code, it might violate an assumption of the test. When that happens, Pex will not generate a test for that path but silently ignore it.

Assertions are a well known concept in regular unit test framework, so Pex already 'understands' the built-in Assert classes provided by each supported test framework. However, most framework do not provide an Assume class. In that case, Pex provides the PexAssume class. If you do not want to use an existing test framework, Pex also has the PexAssert class.

[PexMethod]
public void Test1(object o) {
    // precondition: o should not be null
    PexAssume.IsNotNull(o);
 
    ...
}

In particular, the non-nullness assumption can be encoded as custom attribute:

[PexMethod]
public void Test2([PexAssumeNotNull] object o)
// precondition: o should not be null
{
   ...
}

See Also

PexClassAttribute, PexMethodAttribute, PexAssumeNotNullAttribute
(c) Microsoft Corporation. All rights reserved. pex Wiki Documentation 0.93.50813.0