Pex – first impressions

September 1, 2009 Waldemar Mękal

Pex is a Microsoft Research Project for Automated White box Testing for .NET. It finds input-output values of your methods which can be used in unit testing to provide high code coverage.

I heard about it some time ago and I finally decided to give it a try.

Installation

There are two releases – academic and commercial. To use commercial release you have to have Visual Studio 2008 Team System Edition. You are given standard .msi package, so installation is very easy and straightforward.

After restarting Visual Studio you might notice a new section ‘Pex’ on options dialog (Tools -> Options…).

Pex options

Pex options

Also, there are new entries in editor context menu.

Pex context menu

Pex context menu

First use

I made a new project and create a simple class Calculator with method Divide().

public class Calculator
{
  public int Divide(int a, int b)
  {
    return a / b;
  }
}

Then, I run Pex Exploration by choosing ‘Run Pex’ from editor context menu. It resulted with the following (If you want Pex to generate NUnit tests, you have to have reference to NUnit in your project).

Pex exploration result

Pex exploration result

So, I had three unit tests and two of them was failing. I could choose either ‘Add Precondition…’ or ‘Allow Exception…’.

Add Precondition or Allow Exception

Add Precondition or Allow Exception

I chose ‘Allow Exception…’ to see what would happen. I re-run the Pex, but nothing happened – two tests were still failing. I spent hour or two trying again and again, searching the web and nothing – they were still failing. Then, accidentally I made something that caused it worked. I slowly reconstructed my actions and I discovered that I had to re-run the Pex by clicking ‘Run Pex Exploration’ on PexAllowedException in generated unit test. What is funny, this happens only with the first ‘Allow Exception…’. When using it later, you can re-run Pex everywhere. Really intuititve…

Re-running pex after allowing exception

Re-running pex after allowing exception

The rest worked without problems. I could add precondition, generate test cases either for MS Test or NUnit (MbUnit and CsUnit are also supported), so that was nice. By the way, if you want Pex to generate unit tests for an unit test framework different than MS Test, you have to have a reference to it in the analyzed project.

Summary

Pex might be quite impressive and useful. As you read, for me was irritating and not intuitive for a while I was fighting with ‘Allow Exception…’.

Great about Pex is that it can find input values that you don’t even expect. So, it can really help you, but is it already a tool that can be used in real development? What are your impressions?

Last posts