Monday, October 26, 2009

Pex and Code Contrat

Last week I found two new .Net libraries from Microsoft Research: Pex and Code Contract. Both are right now available as review versions for Visual Studio 2008 Professional version and will be part of .Net framework 4.0 and Visual Studio 2010.

Pex is a library with tools for unit test. It looks very handy and easy to use. It reduces a lot of tedious codes to create test with data. The Pex Exploration integrated with VS 2005 will be able to generate tests with all the data to cover up to 100% of codes. Pex is smart enough to search all the possible branches with data and tests. I remember that I had created a lots of tests to cover all the possible cases and it is very tedious to find branches and repeated uncountable times. The biggest issue to maintain unit tests is that when source codes are changed, the coverage will changed again. In order to cover new cases or update tests, re-examinate source codes and tests need a lots effort. Pex looks much better to handle all those cases. That's really awesome.

In additional to auto-generate test codes, Pex also cover mocking data cases. Actually, I found Pex from the resent DNRTV show: White Box Tesing with Pex, where mocking demo is shown there simulate reading file case. I'll be very interested in trying this great library.

Another very interesting library is Code Contract library. This is really cool stuff I have been looking for. Basically, it is used to define contract for class members, method parameters and method return expectations. The contract is used as static class within .Net codes. Within VS, you can enable is static checking and run-time checking. Any violations will generate exceptions as expected by Contract. In addition to that, it will generate code documentation as well. From its syntax, it is not too complicated but makes the coding much easier. This is definitely a great enhancement to .Net 4.0 and VS 2008 and coming VS 2010.