Posted by: Cirilo Meggiolaro | 12/8/2008

Tip of the day #55 – Unit testing with auto generated input parameters

Microsoft Research group has worked in a tool called Pex, an engine to automate unit testing for .NET applications.

The idea behind the Pex is to analyze and run your application methods with input parameters generated automatically based on smart possibilities for each data type.

It’s is not a random choice of input parameters and it does not generate all possibilities but something smart like null, “:”, “a”, “A”, “0” for a string input parameter covering lower and upper cases, special characters, numbers, null, string empty and so on.

Pex offers the possibility to save a test project with all unit test classes so you may run as many times as you want.

A nice feature is the possibility to allow Pex to give an option to fix failed tests based on preconditions.

Let’s check the steps to use Pex.

How to…

1. Install Pex;

2. Open Visual Studio;

3. Create a class library project;

4. Open the default class and add the code below. For this example, I am using a single method that loops through characters from a string and populates an array of chars without any kind of check. It’s useless since the string object offers a method to get an array of chars but the purpose is to show the Pex in action;

public void MyMethod(string someText)
{
    char[] letters = new char[someText.Length];

    for (int i = 0; i < someText.Length; i++)
    {
        letters[i] = Convert.ToChar(someText.Substring(i, 1));
    }
}

5. Right-click over the new method and on the context menu, click Run Pex Explorations:

Picture 1 - Pex options on context menu

Picture 1 - Pex options on context menu

6. The engine will run your application code several times and will display a result window with a list of parameters inputs for that method and the output of each run;

Picture 2 - The Pex results window

Picture 2 - The Pex results window

7. Select a row and you will se more details on the right side of the results window;

Picture 3 - The details window appears when you select a row on the results window

Picture 3 - The details window appears when you select a row on the results window

8. If you want Pex to give you a suggestion to fix your code, click the Add Precondition button on detail window and a detail form will be displayed with the information about the fix;

Picture 4 - Add precondition details form

Picture 4 - Add precondition details form

9. Click Apply and your method will be changed to reflect the suggested code. The following code displays the final code:

public void MyMethod(string someText)
{
    // <pex>
    if (someText == (string)null)
     throw new ArgumentNullException(“someText”);
    // </pex>

    char[] letters = new char[someText.Length];

    for (int i = 0; i < someText.Length; i++)
    {
        letters[i] = Convert.ToChar(someText.Substring(i, 1));
    }
}

10. Run Pex again to check how the precondition affects the results. You may notice that the following picture displays a different result now because the code has a check for null values now. All tests have passed;

Picture 5 - Results window after apply precondition

Picture 5 - Results window after apply precondition

11. To save your tests click the Save Test button on details window. A form with details about the new project is displayed;

Picture 6 - Save test details.

Picture 6 - Save test details.

12. Clicking on apply button a new project will be created and the class and test cases, saved;

Picture 7 - New test project added to the solution

Picture 7 - New test project added to the solution

13. Do you want to debug during the Pex exploration? Now you can. Just add a breakpoint and click the Debug button on the Pex details window to start running the test cases with debug support.

Picture 8 - Debug button.

Picture 8 - Debug button.

Releases

Right now there are two releases available: One for Visual Studio Team System 2010 CTP and an academic version for Visual Studio Professional 2008 that you can download from the Pex downloads page.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: