Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


Working with Microsoft PEX Framework

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:

  1. Double click on the PEX setup file. The window in Figure 1 appears:

  2. Figure 1: Executing the Microsoft Pex 2010 Setup

  3. Click Next
  4. Accept the License agreement
  5. Specify the drive and folder you would want PEX to be installed on
  6. Click Finish when PEX has been installed in your system

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"
        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" />

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()
        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);

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.