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 t)
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.
Now you are asked for test 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 Exploration Results window shows us Pex results in more detail.
If we click on some line the details pane is opened that gives us better overview about what Pex did in this point.
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.
View Comments (2)
Cool feature, but I like other unit tests platforms like NUnit, MSBuild
Thanks for comment, Slava! I have to be more specific and say that Pex is not unit testing framework but complementary tool that helps you create unit tests for what ever unit test framework you are using.
Generating tests with Pex is optional. You can use like here - just to see if there are problems in your code that must be handled. You can write tests manually that check if these problems are there or not. Pex is just advising you.