Channels ▼
RSS

Tools

Working with Microsoft PEX Framework


Using PEX

To execute PEX on your application's code, follow these steps:

  1. In Visual Studio 2010, open the source file on which you intend to run PEX. In our example, it is the Calculate.cs file.
  2. Select the class or method on which PEX should be executed. Note that this class or method should be public. We will select the DisplayResult() method in this example.
  3. Next, right click on the class or method and choose "Run Pex". We will run PEX on the DisplayResult() method as in Figure 2.
  4. [Click image to view at full size]

    Figure 2: Executing PEX on your application's code.

  5. PEX would now start exploring your method or class. This process is known as "PEX Exploration" and the result of this exploration is displayed in a window called "Pex Explorer". After exploration of the DisplayResult() method, the output looks like Figure 3:
  6. [Click image to view at full size]

    Figure 3: The PEX Explorer Window.

Note that if you place your cursor inside a particular method and then run PEX, PEX would consider that method only for its explorations. However, if the cursor is placed outside of the method and PEX is executed, PEX exploration would work on the entire class.

Understanding the PEX Explorer Window

The PEX Explorer is a window that displays the results of a PEX Exploration process. There are four symbols or icons that display the exploration details on the method or class on which PEX has been executed. These symbols include a:

  • Clock symbol that signifies that a PEX exploration is still in progress
  • Red symbol that denotes failed test cases
  • Green symbol used to denote test cases that have passed
  • Blue symbol that implies that PEX has failed to generate test cases for that part of the program's code

Fixing the Errors

You now need to fix the test errors that have been displayed in the PEX Explorer on execution of PEX on the DisplayResult() method. To do this, you need to add proper boundary conditions in your code. Here is the optimized version of the DisplayResult() method:


public static void DisplayResult(String firstNumber, String secondNumber)
        {
            int x, y;
            try
            {
                x = Int32.Parse(firstNumber);                
            }
            catch
            {
                x = 0;
            }
            try
            {
                y = Int32.Parse(secondNumber);
            }
            catch
            {
                y = 0;
            }
            int sum = GetSum(x, y);
            System.Windows.MessageBox.Show(sum.ToString(), "Result");
        }

When you run PEX again on this method, you'll see that all the test cases have passed:

[Click image to view at full size]

Figure 4: The PEX Explorer Window shows the test cases have passed.

When you run PEX on the Calculate class, you can take a look at the PEX Exploration results for each of the methods of the class. You just need to select the method from the DropDownList control as in Figure 5:

[Click image to view at full size]

Figure 5: Selecting the method or code block of your choice inside the Pex Explorer Window.

Summary

The PEX Framework from Microsoft is a great tool in detecting and fixing bugs in your code. In this article, I've presented the core features of PEX and described how you can make use of this framework to build high-performance, robust applications.


Joydip Kanjilal is a Microsoft Most Valuable Professional in ASP.NET and author of numerous books on Windows programming. He can be contacted at joydipkanjilal@yahoo.com.


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.
 

Video