Logo

Maarten Balliauw {blog}

ASP.NET, ASP.NET MVC, Azure, PHP, OpenXML, VSTS, ...

About the author

Maarten Balliauw is an MVP ASP.NET and is currently employed as .NET Software Engineer at RealDolmen. His interests are mainly web applications developed in ASP.NET (C#) or PHP.
More about me More about me
Send mail E-mail me


Microsoft Most Valuable Professional - MVP - ASP.NET

Subscribe to my RSS feed Follow me on Twitter! View Maarten Balliauw's profile on LinkedIn RealDolmen - Rock-solid passion for ICT
I'm a speaker at TechDays Belgium and TechDays Finland

Search

Latest Twitter

    Follow me on Twitter...

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    © Copyright Maarten Balliauw 2010

    Verifying code and testing with Pex

    Pex, Automated White box testing for .NET

    Earlier this week, Katrien posted an update on the list of Belgian TechDays 2009 speakers. This post featured a summary on all sessions, of which one was titled “Pex – Automated White Box Testing for .NET”. Here’s the abstract:

    “Pex is an automated white box testing tool for .NET. Pex systematically tries to cover every reachable branch in a program by monitoring execution traces, and using a constraint solver to produce new test cases with different behavior. Pex can be applied to any existing .NET assembly without any pre-existing test suite. Pex will try to find counterexamples for all assertion statements in the code. Pex can be guided by hand-written parameterized unit tests, which are API usage scenarios with assertions. The result of the analysis is a test suite which can be persisted as unit tests in source code. The generated unit tests integrate with Visual Studio Team Test as well as other test frameworks. By construction, Pex produces small unit test suites with high code and assertion coverage, and reported failures always come with a test case that reproduces the issue. At Microsoft, this technique has proven highly effective in testing even an extremely well-tested component.”

    After reading the second sentence in this abstract, I was thinking: “SWEET! Let’s try!”. So here goes…

    Getting started

    First of all, download the academic release of Pex at http://research.microsoft.com/en-us/projects/Pex/. After installing this, Visual Studio 2008 (or 2010 if you are mr. or mrs. Cool), some context menus should be added. We will explore these later on in this post.

    What we will do next is analyzing a piece of code in a fictive library of string extension methods. The following method is intended to mimic VB6’s Left method.

    /// <summary>
    /// Return leftmost characters from string for a certain length
    /// </summary>
    /// <param name="current">Current string</param>
    /// <param name="length">Length to take</param>
    /// <returns>Leftmost characters from string</returns>
    public static string Left(this string current, int length)
    {
        if (length < 0)
        {
            throw new ArgumentOutOfRangeException("length", "Length should be >= 0");
        }

        return current.Substring(0, length);
    }

    Great coding! I even throw an ArgumentOutOfRangeException if I receive a faulty length parameter.

    Pexify this!

    Analyzing this with Pex can be done in 2 manners: by running Pex Explorations, which will open a new add-in in Visual Studio and show me some results, or by generating a unit test for this method. Since I know this is good code, unit tests are not needed. I’ll pick the first option: right-click the above method and pick “Run Pex Explorations”.

    Run Pex Explorations

    A new add-in window opens in Visual Studio, showing me the output of calling my method with 4 different parameter combinations:

    Pex Exploration Results

    Frustrated, I scream: “WHAT?!? I did write good code! Pex schmex!” According to Pex, I didn’t. And actually, it is right. Pex explored all code execution paths in my Left method, of which two paths are not returning the correct results. For example, calling Substring(0, 2) on an empty string will throw an uncaught ArgumentOutOfRangeException. Luckily, Pex is also there to help.

    When I right-click the first failing exploration, I can choose from some menu options. For example, I could assign this as a task to someone in Team Foundation Server.

    Pex Exploration Options In this case, I’ll just pick “Add precondition”. This will actually show me a window of code which might help avoiding this uncaught exception.

    Preview and Apply updates

    Nice! It actually avoids the uncaught exception and provides the user of my code with a new ArgumentException thrown at the right location and with the right reason. After doing this for both failing explorations, my code looks like this:

    /// <summary>
    /// Return leftmost characters from string for a certain length
    /// </summary>
    /// <param name="current">Current string</param>
    /// <param name="length">Length to take</param>
    /// <returns>Leftmost characters from string</returns>
    public static string Left(this string current, int length)
    {
        // <pex>
        if (current == (string)null)
            throw new ArgumentNullException("current");
        if (length < 0 || current.Length < length)
            throw new ArgumentException("length < 0 || current.Length < length");
        // </pex>

        return current.Substring(0, length);
    }

    Great! This should work for any input now, returning a clear exception message when someone does provide faulty parameters.

    Note that I could also run these explorations as a unit test. If someone introduces a new error, Pex will let me know.

    More information

    More information on Pex can be found on http://research.microsoft.com/en-us/projects/Pex/.

    kick it on DotNetKicks.com


    Categories: C# | Debugging | General | ICT | Internet | Pex | Quality code | Testing | TFS | VSTS

    Comments

    RealDolmen blogs | Reply

    Tuesday, January 06, 2009 12:53 PM

    trackback

    Trackback from RealDolmen blogs

    Verifying code and testing with Pex

    alvinashcraft.com | Reply

    Tuesday, January 06, 2009 3:28 PM

    pingback

    Pingback from alvinashcraft.com

    Dew Drop - January 6, 2009 | Alvin Ashcraft's Morning Dew

    Sven | Reply

    Tuesday, January 06, 2009 8:28 PM

    Sven

    Nice post - keep it up ;)

    maartenba Belgium | Reply

    Tuesday, January 06, 2009 8:58 PM

    maartenba

    I sure will!

    rowan Australia | Reply

    Wednesday, January 07, 2009 3:12 PM

    rowan

    looks like a handy tool, thanks for posting.

    Dave Van den Eynde Belgium | Reply

    Thursday, January 15, 2009 9:55 AM

    Dave Van den Eynde

    Does anyone else feel this violates the whole purpose of TDD?

    maartenba | Reply

    Thursday, January 15, 2009 10:25 AM

    maartenba

    I think this just adds an extra layer of verification on your code. In the example I gave, TDD would perhaps not provide me with a test that checks for a ceratin Exception but instead only tests the happy path.

    Matt Penner United States | Reply

    Friday, January 16, 2009 8:21 PM

    Matt Penner

    I'm definitely a TDD yourself type guy.  I don't like generated tests, but this looks like a great helper.  As Maarten said this gives you an extra layer.  I would definitely write the tests yourself, but run this to see if you missed anything.  Self created tests can only test what you think to test.

    However, as Maarten so cleverly displayed, you still need to look at the entire procedure as a whole.  Maarten, after you incorporated Pex's (can I say that?) additions your initial exception block at the end will never be used.  This can be removed.  ReSharper probably would have caught this (wow, tools are replacing all our intelligence huh? j/k).

    So, refactor your code after running Pex.

    mattpenner.info | Reply

    Friday, January 16, 2009 8:31 PM

    pingback

    Pingback from mattpenner.info

    Matt Penner  » Blog Archive   » Verifying code with Pex

    maartenba Belgium | Reply

    Saturday, January 17, 2009 3:50 PM

    maartenba

    Have removed that check, indeed looked over it. Had no Resharper at hand...

    Gary Evans United Kingdom | Reply

    Tuesday, January 27, 2009 10:51 PM

    Gary Evans

    One of the comments above was worried that this violates the whole idea of TDD - I've found it the exact opposite!
    Using Pex, you can structure your tests to be more general statements, and get the benefit of TDD but catch unexpected bugs.

    Check out my blog post on writing theories using Pex: taumuon-jabuka.blogspot.com/.../...t-using_11.html

    Cheers, Gary

    Add comment




      Country flag

    biuquote
    • Comment
    • Preview
    Loading