Pex - Automated Whitebox Testing for .NET : Instantiating Existing Classes

Instantiating Existing Classes

What is the problem?
Pex monitors the executed instructions when it runs a test and the program-under-test. In particular, it monitors all field accesses. It then uses a constraint solver to determine new test inputs -- including objects and their field values -- such that the test and the program-under-test will behave in other interesting ways.

Thus, Pex needs to create objects of certain types set their field values.
If the class is visible and has a visible default constructor, Pex can create an instance of the class.
If all the fields of the class are visible, Pex can set the fields automatically.

If the type is not visible, or the fields are not visible, Pex needs help to create objects and bring them into interesting states to achieve maximal code coverage. (Pex could use reflection to create and initialize instances in arbitrary ways, but this is usually not desirable, since it might bring the object into a state which can never occur during a regular program execution. Instead, Pex relies on hints from the user to proceed.)

What can the user do to help Pex?
  • Explorables: You can use an attribute derived from PexExplorableAttributeBase.
    • Advantage: This attribute lets Pex create new objects by exploring its constructor or factory methods, which may validate arguments.
    • Disadvantage: This attribute does not yet support the creation of structs.
    • Disadvantage: Pex must explore more methods to create tests.
  • Creatables: If the object simply represents data, you can an attribute derived from PexCreatableAttributeBase to tell Pex how to create instances.
    • Advanced feature, it is not easy to configure creatable types correctly.
    • Disadvantage: There must be no restrictions on what data can be stored in the fields of the instance.
    • Advantage: When correctly configured, this attribute is very effective.
    • Advantage: This attribute can be used to create instances of structs as well as classes.

When Pex generates test inputs, it imposes certain bounds on the size of the inputs. Pex only considers a set of types when trying to build values which are assignable to the test parameters.
(c) Microsoft Corporation. All rights reserved. pex Wiki Documentation 0.93.50813.0