The PEX Framework is a Visual Studio 2010 Powertool for automated white-box testing of .NET applications. Implemented as a Visual Studio add-in, PEX was developed by Microsoft Research and can be downloaded here. Once installed, PEX lets you automatically generate test suites with high code coverage. Unlike other unit testing tools, PEX suggests fixes to potential bugs it detects in your code. PEX is capable of performing an analysis on your application's code, searching for boundary conditions, generating test cases, searching for assertion failures and ultimately reducing maintenance costs. In this article, I examine the PEX Framework and show how you can integrate it with Visual Studio to create better unit tests with high code coverage.
To work with the PEX framework, you need the following installed on your system:
- .NET Framework 3.5 or higher
- Visual Studio 2008 or Visual Studio 2008 Team System
- Visual Studio 2010
Alternatively, if for some reason you don't have a version of Visual Studio compatible with PEX installed in your system, you can still use PEX from the command line.
Installing the PEX Framework
To install PEX framework in your system, follow these steps:
- Double click on the PEX setup file. The window in Figure 1 appears:
- Click Next
- Accept the License agreement
- Specify the drive and folder you would want PEX to be installed on
- Click Finish when PEX has been installed in your system

Figure 1: Executing the Microsoft Pex 2010 Setup
.
You can now start Visual Studio 2010 in your system and begin using PEX.
Exploring PEX Using Visual Studio 2010
To begin exploring PEX, I'll create a sample project in Visual Studio 2010 to build an application that displays the sum of two numbers in a MessageBox. To do this, open Visual Studio 2010 and create a WPF application. Open Mainwindow.xaml and drag-and-drop two TextBox controls, two Label controls and a Button control. Here's the markup code for the MainWindow.xaml file:
<Window x:Class="DDJ.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Height="205" Width="440">
<Button Content="Get Result" Height="24" HorizontalAlignment="Left" Margin="164,132,0,0" Name="btnResult" VerticalAlignment="Top" Width="80" Click="btnResult_Click" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="164,43,0,0" Name="txtFirstNumber" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="164,90,0,0" Name="txtSecondNumber" VerticalAlignment="Top" Width="120" />
<Label Content="Enter First Number : " Height="28" HorizontalAlignment="Left" Margin="31,41,0,0" Name="label1" VerticalAlignment="Top" Width="127" />
<Label Content="Enter Second Number : " Height="28" HorizontalAlignment="Left" Margin="31,88,0,0" Name="label2" VerticalAlignment="Top" Width="127" />
</Grid>
</Window>
Insert the following event handler for the Button control into the MainWindow.xaml.cs file:
private void btnResult_Click(object sender, RoutedEventArgs e)
{
Calculate.DisplayResult(txtFirstNumber.Text, txtSecondNumber.Text);
}
The DisplayResult() method is a static method that belongs to the Calculate class. This method would display the sum of two numbers entered by the user when the Button is clicked on. The result would be displayed in a MessageBox. Here is the complete MainWindow.xaml.cs code:
using System.Windows;
namespace DDJ
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnResult_Click(object sender, RoutedEventArgs e)
{
Calculate.DisplayResult(txtFirstNumber.Text, txtSecondNumber.Text);
}
}
}
And, here is the complete code of the Calculate class:
using System;
namespace DDJ
{
public class Calculate
{
public static void DisplayResult(String firstNumber, String secondNumber)
{
int sum = GetSum(Int32.Parse(firstNumber), Int32.Parse(secondNumber));
System.Windows.MessageBox.Show(sum.ToString(), "Result");
}
public static int GetSum(int firstNumber, int secondNumber)
{
return (firstNumber + secondNumber);
}
}
}


