.NET Tips and Tricks

Blog archive

Free Tool: Automated Testing with Moles and Pex in Visual Studio 2010

In an earlier set of articles, I showed how to use Microsoft's Fakes Framework and complained about how Fakes is only available in the Ultimate version of Visual Studio 2012. And that's true; but for Visual Studio 2010, Microsoft also has the Moles Isolation Framework for .NET. Moles is similar to shims in the Fakes Framework but, unfortunately, won't see any future development (it's been replaced with Fakes). Still, Moles does work and you can use it for free, even for commercial projects.

But Microsoft has a related tool for addressing a different problem: missing tests. As developers, we tend to test code to make sure it works -- we feed in the results we wrote the code to handle. So Microsoft has a companion to Moles and Fakes called Pex. Unfortunately, to use Pex you must either be an MSDN subscriber or use Pex under the academic license (i.e., not for commercial use).

Pex is a test generator: aim it at your application and Pex generates test cases for you. Pex does that by examining your code and then generating tests that ensures, among other things, every line gets executed. Pex is particularly interested in generating "edge cases": what happens at the outer limits of the ranges you've built into your code. So, for instance, string variables will be tested with null/Nothing values, zero length strings, escape characters, and strings of punctuation marks.

You need to beware of something here: If your code is accessing database or a Web Service, Pex's tests could take a very long time. If your code is performing updates, you could end up with a lot of "interesting" data in your database (e.g. null/Nothing values, zero length strings, strings of punctuation marks). This is, of course, why Pex comes with Moles or Fakes: you really need to isolate your code from your database before running Pex.

You can download Pex through Visual Studio's Extension Manager. After isolating your code, right-mouse click in the method you want Pex to generate tests for and select Run Pex. Then wait. As it's running, Pex will display a table of the inputs it's feeding to your code and the output from each test, including any exceptions. The Pex Explorer window gives you a TreeView of the test results that complements the table view.

Once Pex is finished beating up on your code (Microsoft refers to this as "Pex Explorations"), you can review the results. You can, with a mouse click, save any one or all of Pex's tests as a Visual Studio test; if a test generates an exception, have the generated test accept that exception as an expected result of the test. Alternatively, for any test, you can have Pex write a pre-condition into your code (Pex adds code to your method that tests for that input and throws an exception if it turns up).

If you want to make sure that all the conditions that you wouldn't normally think of are applied to your code (and are an MSDN subscriber or a non-commercial developer), you should take Pex out for a spin.

Posted by Peter Vogel on 01/22/2013

comments powered by Disqus


Subscribe on YouTube