Pex 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!