Search code examples
pex

How to define a set of input parameters in Pex?


Say I have MyClass with 100s of fields.

If I use an object of MyClass as an input param, Pex would simply choke trying to generate all possible combinations (mine runs into 1000s of paths even on a simple test)

[PexMethod] void MytestMethod(MyClass param){...}

How can I tell Pex to use only a set of predefined objects of MyClass rather than having it trying to be smart and generate all possible combinations to test?

In other words I want to manually specify a list of possible states for param in the code above and tell Pex to use it

Cheers


Solution

  • If you find that Pex is generating large amounts of irrelevant, redundant, or otherwise unhelpful inputs, you can shape the values that it generates for your parametrized unit tests' input using PexAssume, which will ensure that all generated inputs meet a set of criteria that you provide.

    If you were wanting to ensure that arguments came from a predefined collection of values, for instance, you could do something like this:

    public void TestSomething(Object a) {
        PexAssume.IsTrue(someCollection.Contains(a));
    }
    

    PexAssume has other helper methods as well for more general input pruning, such as IsNotNull, AreNotEqual, etc. What little documentation is out there suggests that there is some collection-specific functionality as well, though if those methods exist, I'm not familiar with them.

    Check out the Pex manual for a bit more information.