MSDN UK Team blog

Get all the latest Microsoft developer news, tools, downloads, tutorials and tips right here, every day.
Search Form
Unfiltered HTML
Developers code with microsoft

Featured Article: Pex and Visual Studio

Unfiltered HTML
Unfiltered HTML

Featured Article: Pex and Visual Studio

  • Comments 1

This week’s featured article comes from Mark Bloodworth, formally of BBC Worldwide and now working as an Architect Evangelist at Microsoft. Mark talks about PEX a framework for application testing.

Pex is a tool that allows you to explore code and understand its behaviour.  A good place to start is PexForFun – which runs Pex in the cloud.  It allows you to write code in a web browser in a variety of .NET languages and run Pex to compile, run and analyse your code.   There are instructive puzzles as well as coding duels (interactive puzzles) that allow you to test your coding skills.

The full version of Pex is an add-in for Visual Studio (2008 and 2010) (it can also be run from the command line) that allows you to do runtime analysis.  It can also generate automated tests.   The download information is here.  The download also includes Moles: a lightweight mocking framework for .NET that enables you to write test stubs and detours (including non-virtual/static methods in sealed types).   Once you have downloaded and installed Pex, there are tutorials and documentation here that will help you get started quickly.

To see how Pex can be used, consider the following sample code to calculate word frequency:

public Dictionary<string, int> WordCount(string text)

{

    string[] words = text.Split(' ');

    Dictionary<string, int> result = new Dictionary<string, int>();

    foreach (string word in words)

    {

        string strippedWord = StripPunctuation(word).ToLower();

        if (result.ContainsKey(strippedWord))

        {

            result[strippedWord]++;

        }

        else

        {

            result[strippedWord] = 1;

        }

    }

    return result;

 

}

 

private string StripPunctuation(string word)

{

    string result = word;

    if (char.IsPunctuation(result[0]))

    {

        result = result.TrimStart(result[0]);

    }

    if (char.IsPunctuation(result[result.Length - 1]))

    {

        result = result.TrimEnd(result[result.Length - 1]);

    }

    return result;

}

 

Running Pex against this code reveals a series of errors.  The first is that the WordCount method should not accept a null argument – Pex can be used to generate a precondition  - which will throw a NullReferenceException in this case.  The second is that the StripPunctuation method throws an exception if the word passed to it is a zero length string.  The StripPunctuation method can also be tripped up if the string passed to it contains a single punctuation character.  Correcting all these errors gives us the following code:

 

public Dictionary<string, int> WordCount(string text)

{

    // <pex>

    if (text == (string)null)

        throw new ArgumentNullException("text");

    // </pex>

    string[] words = text.Split(' ');

    Dictionary<string, int> result = new Dictionary<string, int>();

    foreach (string word in words)

    {

        string strippedWord = StripPunctuation(word).ToLower();

        if (strippedWord.Length > 0)

        {

            if (result.ContainsKey(strippedWord))

            {

                result[strippedWord]++;

            }

            else

            {

                result[strippedWord] = 1;

            }

        }

    }

    return result;

}

 

private string StripPunctuation(string word)

{

    string result = word;

    if (result.Length > 0)

    {

        if (char.IsPunctuation(result[0]))

        {

            result = result.TrimStart(result[0]);

        }

        if (result.Length > 0)

        {

            if (char.IsPunctuation(result[result.Length - 1]))

            {

                result = result.TrimEnd(result[result.Length - 1]);

            }

        }

    }

    return result;

}

 

Of course I would write unit tests against this code, but it’s possible that I would miss one of these error cases.  Pex works by exploring the code – it doesn’t generate random test inputs, but analyses every branch of code – and using a feedback loop to learn how your program works.  Having seen Pex in action, it’s worth going back to the tutorials to look at how it can be combined with Moles and Code Contracts to enhance the testing you are already doing.  There is also a session from PDC 2010 that goes into these tools that you can watch here.

  • Impressive ! I'll try it tomorrow.

Page 1 of 1 (1 items)
Leave a Comment
  • Please add 2 and 3 and type the answer here:
  • Post