John Smith's Blog

Keeping the fun machine running one nickel at a time


News

My Stats

  • Posts - 21
  • Comments - 29
  • Trackbacks - 0

Twitter












Recent Comments


Recent Posts


Article Categories


Archives


Post Categories


Image Galleries



Pex has had two updates, one since I first used it, and one after I finished using it.  Here’s the release notes for the latest build that dropped on 5/1/2009.  Fresh install, and rewinding back through the project I had referenced in my first post.  This time I need to build a different project that doesn’t reference my day job and also how well things work with xUnit.  imageThe install was perfect again, no issues with that so far.  Here are the release notes, and a few steps setting up a new project.  I’m just going to start a new project and start from scratch and buzz through the same examples I worked through previously.  Here’s the new project setup. 

 

Now that I’ve got a new project to work against, we can generate the Pex test project and ask Pex to get busy.  First we’ll ask Pex to create a new xUnit test project.  Right-clicking the Facade project exposes the context menu, we’ll choose “Create Parameterized Unit Test Stubs”, aka PUTs. 

 

 

 

 

 

This gives us the following dialogs to set our project properties.   I’ll leave the blanks blank, those exist to filter the namespace, type name, and method names you want to filter for the project under test. We want all of them, so they’ll be left blank. 

A variation on this is if you open the context menu inside the class file.  The filter fields are filled in for you to limit Pex’ interaction.

 imageimage 

 

 

 

 

 

 

 

 

 

 

 

Here are the settings I’ve chosen, I’m going with xUnit this time around.

image

 

 

 

 

 

 

 

imageThe “Settings…” dialog is where I tell Pex that for all unit tests created, each will have the PexTest suffix.

"Mark all test results Inconclusive by default" - This setting will add the Assert.Inconclusive() assertion at the bottom of the test method which is the default for all Visual Studio unit tests when they are generated in the IDE.

"Globally qualify all types" - This setting will prefix all fields in the test class with global qualifier.

"Use Code Patterns" - Pex utilizes many different code patterns which are beyond the scope of this document. You can find them in this document's appendix.

"Generate Stubs file for project under test" - The Stubs Framework utilized by Pex is explained by one of the authors here.

We click OK on the settings dialog.  We click OK on the main dialog, and Pex shows us the dialog pictured below.  We’re getting a unit test project called …\ProjectName.Tests (plural suffix);  now is your only chance to move this project around.  If you like to keep your unit test libraries somewhere else besides the project it’s testing move it to that place.  The MSTest dialog gives us a singular suffix.  So if you have both, this might help you keep them apart.  Hopefully they will allow us to (re)label them in future versions of Pex.

 image

Click OK.

If Pex can’t find your test runner, you’ll be prompted for the location.

image 

 

 

 

 

 

 

 

 

Here are the new pieces Pex has added.

image

 

A few things to notice here. We get one Pex class for each of our classes so the test methods are segregated neatly. The point to make here is to get everything neat before you generate the test project.  Also, notice the suffix, "PexTest". We can't rename the class library suffix, so I chose to rename the class files so I know by looking at the project members which classes are mine, and which ones were generated by Pex. Also, the stub file mentioned previously is generated for us.

image

 

 

 

 

 

 

 

 

 

Here’s what Reflector can show us about what was created as well, the crunchy layer looks pretty much identical to the Crunchy layer.

image

namespace PeanutButter.Business.Facade.Creamy
{
    /// <summary>This class contains parameterized unit tests for CreamyLayerManager</summary>
    [PexClass(typeof(CreamyLayerManager))]
    [PexAllowedExceptionFromTypeUnderTest(typeof(InvalidOperationException))]
    [PexAllowedExceptionFromTypeUnderTest(typeof(ArgumentException), AcceptExceptionSubtypes = true)]
    public partial class CreamyLayerManagerPexTest
    {
        /// <summary>Test stub for Add(!!0)</summary>
        [PexGenericArguments(typeof(int))]
        [PexMethod]
        public void Add<T>([PexAssumeUnderTest]CreamyLayerManager target, T entityToAdd)
        {
            // TODO: add assertions to method CreamyLayerManagerPexTest.Add(CreamyLayerManager, !!0)
            target.Add<T>(entityToAdd);
        }
 
        /// <summary>Test stub for Remove(!!0)</summary>
        [PexGenericArguments(typeof(int))]
        [PexMethod]
        public void Remove<T>(
            [PexAssumeUnderTest]CreamyLayerManager target,
            T entityToRemove
        )
        {
            // TODO: add assertions to method CreamyLayerManagerPexTest.Remove(CreamyLayerManager, !!0)
            target.Remove<T>(entityToRemove);
        }
 
        /// <summary>Test stub for Update(!!0)</summary>
        [PexGenericArguments(typeof(int))]
        [PexMethod]
        public void Update<T>(
            [PexAssumeUnderTest]CreamyLayerManager target,
            T entityToUpdate
        )
        {
            // TODO: add assertions to method CreamyLayerManagerPexTest.Update(CreamyLayerManager, !!0)
            target.Update<T>(entityToUpdate);
        }
    }

 

The only Source Analysis violation are related to moving the using statements inside the namespace declaration and the adding the method arguments to the summary block. Source and Code Analysis compliance was one of the latest features added to the Pex release in use.

Notice the method decoration "[PexMethod]" This method won't be visible to the xUnit test outline since unit tests are reference by the FactAttribute. If it would have been decorated with "[Fact]" it would.  Also notice Pex methods are parameterized (PUTs) tests.  The xUnit methods will be generated from, and by, the PUTs.

The PexMethod(s) will be used during the Pex Exploration we will step through now.  Right-clicking the test project exposes the Pex Exploration menu item which starts the exploration process.

 

image

 

 

 

 

 

 

 

 

Pex Exploration finishes and the Pex Explorer tells us we have some problems - all of my methods are throwing NotImplementedExceptions” - nice.  image 

 

 

 

 

 

 

 

 

 

 

I’ll stop here, fix my code and pick-up with an Exploration exercise to allow Pex to do something meaningful with my test project.

 

Until then.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Comments

Gravatar # re: Flex Your Pex (Part 1)
Posted by vegetarian dishes on 11/29/2010 1:59 PM
Hey John,

Thanks for the tutorial, it made things much easier for me to understand.
Gravatar # re: Flex Your Pex (Part 1)
Posted by nike air jordans on 1/7/2011 3:35 PM
I’ll stop here, fix my code and pick-up with an Exploration exercise to allow Pex to do something meaningful with my test project.
Gravatar # re: Flex Your Pex (Part 1)
Posted by Quick cash loans on 2/1/2011 12:26 AM
Thanks for presenting an useful tutorial. It seems very useful for us. Great post.

Gravatar # re: Flex Your Pex (Part 1)
Posted by Batch PDF Converter on 2/18/2011 5:09 PM
I am the frequent visitor of this website.I had already added this blog to my favorites.Thank you for sharing this!

Gravatar # re: Flex Your Pex (Part 1)
Posted by Yale Alarm on 2/21/2011 9:35 PM
There is a site named pex for fun.made by microsoft. IT gives code puzles and you solve it. Its really fun. http://www.pexforfun.com/
Gravatar # re: Flex Your Pex (Part 1)
Posted by Dedicated Server on 2/23/2011 3:50 AM
allow Pex to do something meaningful with my test project.http://www.lpdedicated.com/
Gravatar # re: Flex Your Pex (Part 1)
Posted by Seo services in India on 3/3/2011 11:23 PM
Thanks for sharing such a great post as it's very interesting and informative.

Gravatar # re: Flex Your Pex (Part 1)
Posted by flour mill on 3/8/2011 10:14 PM
Thanks for presenting an useful tutorial. It seems very useful for us. Great post.
Gravatar # re: Flex Your Pex (Part 1)
Posted by etui cuir nokia c7 on 3/23/2011 12:50 AM
Pex Exploration finishes and the Pex Explorer tells us we have some problems - all of my methods are throwing NotImplementedExceptions” - nice.
Gravatar # re: Flex Your Pex (Part 1)
Posted by Fast cash on 3/28/2011 1:24 AM
Pex generates test suites with high code coverage. It finds interesting input-output values of your methods, which you can save as a small test suite with high code coverage.

Gravatar # re: Flex Your Pex (Part 1)
Posted by pop up canopies on 4/18/2011 7:02 AM
Thanks for walking through this, I don't think i would have figured it out otherwise!
Gravatar # hello
Posted by Kids aprons on 7/18/2011 5:52 PM
I have just recently found your blog and absolutely love the posts. Thank you, thank you. I am still .NET programming but can't wait to try these things in Ruby and it is a great help to see such clear examples. kids apron

Gravatar # re: Flex Your Pex (Part 1)
Posted by Costa Rica Real Estate on 7/20/2011 11:19 PM
This is a very useful post! I started to read your blog several months ago and truing to visit every single week. Thank you for sharing such a useful info.Regards,
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: