Quick introduction to Pex

PexPex and Moles is new testing and code analysis technology that is created by Microsoft Research. Pex is powerful tool that helps you analyze your code, detect error situations and generate parameterized unit tests. In this posting I will give you quick and illustrated overview of Pex.

For business use Pex requires MSDN Library subscription. For academic use it is free right now. You can download Pex and Moles from Pex and Moles homepage.

Pex generates unit tests to test your code on the fly. It analyzes paths in your code and checks for different constraints and tries different parameters with tests to break your code. Parameter values are not randomly generated but meaningfully selected. This means that Pex is not about flooding and finding bugs randomly. After code analysis and testing Pex shows you results in Visual Studio IDE.

Now let’s try to do something simple that illustrates how Pex works.

Primitive test class

Let’s create new Class Library project using Visual Studio and let’s create there class called calculate. Here is my class.

public class Calculate
public static decimal Speed(decimal s, decimal
return s / t;

If you look at this class you can see that method Speed() is easy to crash – just give zero as value to argument t. Now let’s see what Pex thinks about our class.

Analyzing code with Pex

To run Pex on our code we have to right click on project and select Run Pex from context menu.

Run Pex

Now you are asked for test framework.

Select testing framework

I selected nUnit but you can find also other frameworks there like Visual Studio tests and xUnit.

After clicking OK Pex starts analyzing and testing your code. It takes a little time so we have to wait a little bit.


Pex shows us analysis results in two windows: Pex Explorer and Pex Exploration Results. Pex Explorer is good if we have more classes and it we want to navigate between them and test results. Pex Explorer is very similar to windows we have seen when running tests on our code.

Pex Explorer

Pex Exploration Results window shows us Pex results in more detail.

Pex exploration results

If we click on some line the details pane is opened that gives us better overview about what Pex did in this point.

Pex Exploration Results

In details windows we can also see test that Pex generated if we open details section.

As we can see then Pex discovered one more problematic situation – our code is not safe against overflows. In similar manner Pex is able to analyze more complex methods and gives us very good hints about weak sides of our code.


Pex is interesting tool that helps developers on analyzing code and generating parameterized unit tests. Pex is not about replacing tests or making them less important. Pex is about providing additional value to developers who are interested in their code quality and unit tests. Tests will never go away and Pex is here to help us find more tests we need to have to improve our code quality. Also you may consider Pex as helping tool that helps us on generating tests we want to add to our test libraries.

See also

Leave a Reply

Your email address will not be published. Required fields are marked *