Quick introduction to Pex

PexPex and Moles is new testing and code analysis technology that is created by Microsoft Research. Pex is powerful tool that helps you analyze your code, detect error situations and generate parameterized unit tests. In this posting I will give you quick and illustrated overview of Pex.

For business use Pex requires MSDN Library subscription. For academic use it is free right now. You can download Pex and Moles from Pex and Moles homepage.

Pex generates unit tests to test your code on the fly. It analyzes paths in your code and checks for different constraints and tries different parameters with tests to break your code. Parameter values are not randomly generated but meaningfully selected. This means that Pex is not about flooding and finding bugs randomly. After code analysis and testing Pex shows you results in Visual Studio IDE.

Now let’s try to do something simple that illustrates how Pex works.

Primitive test class

Let’s create new Class Library project using Visual Studio and let’s create there class called calculate. Here is my class.

public class Calculate
{
   
public static decimal Speed(decimal s, decimal
t)
    {
       
return s / t;
    }
}

If you look at this class you can see that method Speed() is easy to crash – just give zero as value to argument t. Now let’s see what Pex thinks about our class.

Analyzing code with Pex

To run Pex on our code we have to right click on project and select Run Pex from context menu.

Run Pex

Now you are asked for test framework.

Select testing framework

I selected nUnit but you can find also other frameworks there like Visual Studio tests and xUnit.

After clicking OK Pex starts analyzing and testing your code. It takes a little time so we have to wait a little bit.

Results

Pex shows us analysis results in two windows: Pex Explorer and Pex Exploration Results. Pex Explorer is good if we have more classes and it we want to navigate between them and test results. Pex Explorer is very similar to windows we have seen when running tests on our code.

Pex Explorer

Pex Exploration Results window shows us Pex results in more detail.

Pex exploration results

If we click on some line the details pane is opened that gives us better overview about what Pex did in this point.

Pex Exploration Results

In details windows we can also see test that Pex generated if we open details section.

As we can see then Pex discovered one more problematic situation – our code is not safe against overflows. In similar manner Pex is able to analyze more complex methods and gives us very good hints about weak sides of our code.

Conclusion

Pex is interesting tool that helps developers on analyzing code and generating parameterized unit tests. Pex is not about replacing tests or making them less important. Pex is about providing additional value to developers who are interested in their code quality and unit tests. Tests will never go away and Pex is here to help us find more tests we need to have to improve our code quality. Also you may consider Pex as helping tool that helps us on generating tests we want to add to our test libraries.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    51 thoughts on “Quick introduction to Pex

    • November 29, 2010 at 2:07 pm
      Permalink

      Cool feature, but I like other unit tests platforms like NUnit, MSBuild

    • November 29, 2010 at 2:14 pm
      Permalink

      Thanks for comment, Slava! I have to be more specific and say that Pex is not unit testing framework but complementary tool that helps you create unit tests for what ever unit test framework you are using.

      Generating tests with Pex is optional. You can use like here – just to see if there are problems in your code that must be handled. You can write tests manually that check if these problems are there or not. Pex is just advising you.

    • June 18, 2025 at 5:39 am
      Permalink

      763020 752617This internet internet site is my breathing in, genuinely very good layout and perfect content material . 951012

    • July 12, 2025 at 9:01 pm
      Permalink

      82740 423220Ill create a hyperlink to the internet page about my individual weblog. 128184

    • July 14, 2025 at 1:04 am
      Permalink

      622036 871198Just wanna remark that you have a extremely good web site , I like the layout it truly stands out. 244585

    • July 14, 2025 at 1:20 am
      Permalink

      683366 852769You got a very exceptional web site, Glad I noticed it through yahoo. 256448

    • July 17, 2025 at 12:36 am
      Permalink

      17558 852320You can surely see your skills in the function you write. The world hopes for far more passionate writers like you who arent afraid to say how they believe. At all times follow your heart 865645

    • July 18, 2025 at 1:37 pm
      Permalink

      660325 526675Wow, cool post. Id like to write like this too – taking time and real effort to make a good article but I procrastinate too a lot and never appear to get started. Thanks though. 169450

    • July 23, 2025 at 12:22 am
      Permalink

      397394 459950You made some decent points there. I looked on the internet for the dilemma and discovered most individuals will go coupled with along along with your internet site. 471660

    • July 24, 2025 at 5:40 am
      Permalink

      49716 896637There is noticeably a bundle comprehend this. I suppose you produced specific nice points in functions also. 447049

    • July 24, 2025 at 10:38 am
      Permalink

      681792 150115This sort of considering develop change in an individuals llife, building our Chicago Pounds reduction going on a diet model are a wide actions toward making the fact goal in mind. lose weight 264688

    • July 28, 2025 at 8:02 pm
      Permalink

      58637 316808This really is a exceptional blog, would you be involved in performing an interview about just how you created it? If so e-mail me! 8150

    • July 29, 2025 at 8:22 pm
      Permalink

      723703 576570There is noticeably plenty of cash to realize about this. I suppose you produced specific nice points in functions also. 252585

    • July 30, 2025 at 5:26 pm
      Permalink

      777711 443017BTW, and I hope we do not drag this too long, but care to remind us just what kind of weapons were being used on Kurds by Saddams army? To the tune of hundreds of thousands of dead Speak about re-written history 724915

    • August 5, 2025 at 12:25 am
      Permalink

      32290 342590Having read this I thought it was quite informative. I appreciate you taking the time and effort to put this post together. I once again find myself spending way to significantly time both reading and commenting. But so what, it was nonetheless worth it! 190296

    • August 6, 2025 at 12:36 am
      Permalink

      167482 570898I feel 1 of your advertisements triggered my internet browser to resize, you may want to put that on your blacklist. 603521

    • August 8, 2025 at 3:24 am
      Permalink

      586073 862343I was just seeking this information for a while. After 6 hours of continuous Googleing, at last I got it in your web site. 964255

    • August 10, 2025 at 12:30 am
      Permalink

      18115 177043Disgrace on the seek Google for not positioning this submit upper! Come on over and consult with my web site. 776251

    • August 10, 2025 at 3:20 am
      Permalink

      839350 945513Glad to be one of several visitants on this incredible internet web site : D. 29673

    • August 13, 2025 at 7:31 am
      Permalink

      288440 97921Was koche ich heute – diese Frage stellen sich tag fuer tag viele Menschen. Und wir haben tag fuer tag die perfeckte Antwort darauf! Besuchen Sie uns auf unserer Webseite und lassen Sie sich von uns beraten . Wir freuen uns auf Sie! 956195

    • August 13, 2025 at 7:51 am
      Permalink

      748903 903868Amaze! Thank you! I constantly wished to produce in my internet web site a thing like that. Can I take element with the publish to my weblog? 911362

    • August 19, 2025 at 3:19 am
      Permalink

      514055 937052Your talent is actually appreciated!! Thank you. You saved me a great deal of frustration. I switched from Joomla to Drupal to the WordPress platform and Ive fully embraced WordPress. Its so much easier and easier to tweak. Anyway, thanks once more. Awesome domain! 970340

    • August 20, 2025 at 12:20 pm
      Permalink

      514150 883899Hi, you used to write outstanding posts, but the last several posts have been kinda boring I miss your fantastic posts. Past couple of posts are just slightly bit out of track! 678926

    • August 21, 2025 at 12:22 am
      Permalink

      3301 453312I agree with most of your points, but several want to be discussed further, I will hold a small speak with my partners and possibly I will appear for you some suggestion soon. 104333

    • August 26, 2025 at 12:27 am
      Permalink

      538665 841049An intriguing discussion will probably be worth comment. I believe which you merely write considerably a lot more about this subject, it may become a taboo subject but normally consumers are inadequate to communicate in on such topics. To an additional. Cheers 93343

    • August 29, 2025 at 12:25 am
      Permalink

      306276 145118Ich kenne einige Leute, die aus Kanadakommen. Eines Tages werde ich auch dorthin reisen Lg Daniela 599329

    • September 1, 2025 at 12:14 am
      Permalink

      136953 344444Trop excitant de mater des femmes lesbiennes en train de se doigter la chatte pour se faire jouir. En plus sur cette bonne petite vid o porno hard de lesb X les deux jeunes lesbienne sont trop excitantes et super sexy. Des pures beaut de la nature avec des courbes parfaites, les filles c est quand v 439487

    • September 3, 2025 at 12:42 am
      Permalink

      815598 373638I like you blog (dsol, je suis francais, je parle mal anglais) 981188

    • September 6, 2025 at 12:55 am
      Permalink

      984942 605729Some genuinely rattling function on behalf of the owner of this internet site , utterly wonderful content material . 809984

    • September 11, 2025 at 12:29 am
      Permalink

      318409 640920You appear to be quite skilled in the way you write.::~ 947027

    • September 12, 2025 at 2:56 am
      Permalink

      217711 646530This really is the proper weblog for anybody who hopes to learn about this topic. You know a complete lot its almost tough to argue along (not that I actually would wantHaHa). You undoubtedly put a entire new spin for a topic thats been written about for years. Excellent stuff, just wonderful! 542647

    • September 16, 2025 at 12:16 am
      Permalink

      324080 620015Wahhhh,!! I dnt believe its food thats creating her tummy groww!!|tiitaBoo| 71815

    • September 19, 2025 at 12:50 am
      Permalink

      605242 649495Nice read, I just passed this onto a colleague who was performing slightly research on that. And he just bought me lunch since I identified it for him smile So let me rephrase that: Thank you for lunch! 582056

    • September 19, 2025 at 5:51 am
      Permalink

      19349 487213Good post. I be taught 1 thing far more challenging on totally different blogs everyday. It will all the time be stimulating to learn content from other writers and apply slightly one thing from their store. Id desire to use some with the content material on my weblog whether you dont mind. Natually Ill give you a hyperlink on your net blog. Thanks for sharing. 27014

    • September 20, 2025 at 1:13 am
      Permalink

      607453 228085Basically wanna input that you have a extremely good internet site , I enjoy the pattern it truly stands out. 787067

    • September 26, 2025 at 1:49 am
      Permalink

      77658 274715I saw two other comparable posts although yours was the most beneficial so a good deal 969736

    • September 26, 2025 at 2:26 am
      Permalink

      277860 95776Fantastic post, I conceive blog owners ought to acquire a great deal from this web weblog its real user pleasant. 215728

    Leave a Reply

    Your email address will not be published. Required fields are marked *