Pex is one of the Visual Studio add-in that provides a runtime code analysis tool for .NET Framework code. The output of the Pex helps you understand the behavior of the code, and it also builds automated tests with high code coverage.
Pex
Pex is one of the Visual Studio add-in that provides a runtime code analysis tool for .NET Framework code. The output of the Pex helps you understand the behavior of the code, and it also builds automated tests with high code coverage. Along with Pex, we have Moles, which is used for mocking the objects in Unit test. In this article, we will discuss about Pex and how we can create Unit tests using Pex.
You can find the details of Pex and how to install the same from http://research.microsoft.com/en-us/projects/pex/downloads.aspx
Run Pex
For our sample, I have a Utility class with two methods, Sum and Greater. For finding various values that cover all the code paths of our code, right click on the method and select “Run Pex” from the context menu.

This will open the Pex Exploration Results window with all possible combination of input values.
For our sample Greater method, Pex added 6 test cases. We can save these unit test cases into a file using the Save Test option.

Also, Pex gives the code coverage information. Click on the hypherlink appeared on top of the Test cases in Pex explorer to get the code coverage report.

Parameterized Unit Test
We can create Parameterized unit tests using Pex. If our method is expecting any custom types, Pex may not be able to generate the expected values. In this case, we will create parameterized unit tests and pass the values using either Moles or Factory. We will discuss about Moles and factories latter.
For creating Parameterized Unit test, right click on the class and select the Pex -> Create Parameterized Unit Tests option from the context menu.

This will open the Create Parameterized Unit Tests window, where we can specify the Selection filters and the test Framework. As you can see, our selection includes the namespace filter and type name filter indicating the class. For this demo, I am using the Visual Studio Unit Test framework as the Test framework.

As we are going to add a new test project, it will open the Add new Test project window. Here, we can specify the Test project location and name.

Pex generated unit test for our sample class consist of one parameterized unit tests for each of our methods, Sum and Greater.
namespace SampleApp
{
/// <summary>This class contains parameterized unit tests for Utility</summary>
[PexClass(typeof(Utility))]
[PexAllowedExceptionFromTypeUnderTest(typeof(InvalidOperationException))]
[PexAllowedExceptionFromTypeUnderTest(typeof(ArgumentException), AcceptExceptionSubtypes = true)]
[TestClass]
public partial class UtilityTest
{
/// <summary>Test stub for Greater(Int32, Int32)</summary>
[PexMethod]
public int Greater(
[PexAssumeUnderTest]Utility target,
int n1,
int n2
)
{
int result = target.Greater(n1, n2);
return result;
// TODO: add assertions to method UtilityTest.Greater(Utility, Int32, Int32)
}
/// <summary>Test stub for Sum(Int32, Int32)</summary>
[PexMethod]
public int Sum(
[PexAssumeUnderTest]Utility target,
int n1,
int n2
)
{
int result = target.Sum(n1, n2);
return result;
// TODO: add assertions to method UtilityTest.Sum(Utility, Int32, Int32)
}
}
}
Now, let us add the Unit tests with expected input values. Run the Pex Exploration against the Unit test class.

This will add the Unit tests with Pex generated values. For our sample, Pex explorer added separate classes for each of the methods.

Conclusion
Pex makes it very easy for a developer to create unit tests with high code coverage. Pex generate different parameter values to cover all code paths. We will look deeper into Pex and Moles latter.