Unit testing with Pex

by Viktor 27. November 2008 08:12

 

Something that may not be as cool or fun making as a new web 2.0 function is making unit tests for that function, but they are just as important to help make sure that your function still works after one year when you rewrite some dependent code in your project.

Microsoft is trying to help us developers with this with their new Pex project that will be included in Visual Studio 2010. Pex is a system that will help you automatically generate unit tests for your code.

Pex will generate all this tests after analyzing the code as deep as it can and the limit today is that it can only analyze managed code.

 

A very quick example of how Pex can be used,

 

We have two classes, a data class named User with one public constructor that takes a username and then we have the static class with one method, IsUser to test if a user object is the specified user.

If we now want to run unit tests on the IsUser method we right click in the method and select "Run Pex Explorations" 

Pex will then analyze your code (and underlying code called by your code) and find out how many different test cases there need to be to test the code.

When Pex is finished with this it will show a result window with the tests and results 

Here we can see that Pex found two paths through our code. Once with null as the user, this did not work, and once with a user object. Pex will also try to suggest a fix for your code if it fails a test. This was something that did not always work during my tests of Pex as it sometimes generated uncompilable code. From here you can save these generated tests in a test project, send a e-mail to the colleague responsible for the bug or add it as a work item in Team Foundation Server.

This has just been a very quick introduction to what Pex has to offer, for more information I suggest a visit to the Pex project page at http://research.microsoft.com/pex/.

Right now there are two preview versions of Pex released, one for Visual Studio 2010 that you can use on commercial projects and then a version for Visual Studio 2008 that can only be used for none commercial projects. 

Tags: , ,

Development

blog comments powered by Disqus

Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 2.5 Sweden License.


Welcome to the Dropit blog!

Here we, the people that work at Dropit, will write about stuff that interests us. For example web development, especially with .NET and EPiServer - but we'll also talk about other techniques that interest us, marketing on the web, social phenomenons, pop culture, games and software development in general.