An introduction to Pex
.NET April 16th, 2009Pex generates Unit Tests from Parameterized Unit Tests through Automated White box Testing based on Input Generation
Ill be the first to admit, I have never been a big fan of unit testing. To me, it seems like a big waste of time. I’m not sure why, but I just don’t like it – I suppose it has something to-do with spending time writing tests instead of actually doing proper, paid work. Yes, yes, I know that tests “prove” that your code works, as long as you have coded your tests right. But what if you haven’t?
Anyway, I thought id give Pex a test since it generates unit tests for me. I love things that generate things for me, because I like to do things as quickly as possible, and the computer is millions of times faster than me at generating stuff.
For starters – head over to the Pex website, and download it – I downloaded the academic version, since im running VS.NET 2008 pro. Installing it after downloading it also might help matters.
Once you have installed it, you should be able to just open up a project and when right clicking inside a .cs file, you should see a few menu items:

Once you click on that little baby, Pex should start whizzing into action. Be warned, Pex will only test Public classes and methods – it dosent like testing anything else that isnt public, and I believe it will give you a warning.
In VS.NET’s status bar, you will get a bunch of messages, along the lines of “Pex: listening to monitored process (cold start)” and then “Pex: Finished”. It should also pop up a window, something vaguely like the following:

I know this looks confusing at first, but it really isn’t. Its quite simple really. You have various options, but what’s interesting (I think) is the grid view in the middle. The first icon says if the test passed or failed – green = pass, red = ? (Take a guess, gwan, take a guess!) The number denotes the number of the test (its incremental), and the name is the value it has tried If the test fails and throws an exception, that will be listed under the Summary and Error message bits.
If you click on the test, you can see the following to the side :

The details is the actual code for the unit test that it has executed. I know, it says “this.” instead of the class name, and that’s because Pex creates a copy of the code to run tests on, in a partial class. Under the stack trace subtab, it well, gives you the stack trace (Please tell me you knew what that would do. Please.)
This is where Pex becomes rather … brilliant – it allows you to save all of these generated tests into its own project, simply by selecting the tests you want to save, and hitting the fancy “Save Test…” button – it will go off, and generate its own project (It will ask you for the name of the project, where it should live etc, I will provide a screenshot later) – and it will save the tests to that project! Tests that you can run later, and you don’t have to use vs.net’s test suite – you can decide to plug in different testing libraries such as MBUnit (my current fav) by downloading extensions that are available on CodePlex, or I believe you can create your own.
More on Pex later!


May 21st, 2009 at 2:10 am
[...] Studio 2008 Pro – Pro because you need at least that for integrated unit tests and Pex to [...]
May 5th, 2012 at 7:11 am
great insights you are sharing here. I admire the way you are sharing it. Is there any way I could sign up for more?
November 27th, 2012 at 9:03 am
Excellent website you have here but I was wondering if you knew
of any message boards that cover the same topics talked about here?
I’d really love to be a part of community where I can get opinions from other experienced people that share the same interest. If you have any recommendations, please let me know. Appreciate it!
February 19th, 2013 at 6:53 pm
Hi! I’ve been following your blog for a long time now and finally got the bravery to go ahead and give you a shout out from New Caney Tx! Just wanted to mention keep up the great work!
February 19th, 2013 at 7:16 pm
I have been surfing on-line more than three
hours lately, but I by no means discovered
any attention-grabbing article like yours. It is lovely value enough for me.
Personally, if all web owners and bloggers made excellent content material as
you did, the internet shall be much more helpful than ever
before.
March 6th, 2013 at 10:25 pm
” The stepmother went over to the young “Jessie James,
” placed her hand under his chin, tilted his head
upward, looked him squarely in the eyes for a moment, and then turned
to her husband and said, “You are wrong. If so, taking steps like trying to prevent further injury or loss of blood are the most important thing you can do.
And, for individuals who are eligible for both CLASS and Medicaid,
a portion of the CLASS benefits will be used to offset the costs
to Medicaid. Some conditions affect basically
one organ or tissue type, others are more widespread.
She was not the type to accept poverty and illiteracy without protest.