Recently (fairly recently) the good people of Microsoft Reseach released power tools for Visual Studio 2008/2010 that enables the automatic generation of unit tests with high coverage for untested bits of code called Pex.
You can check it out here.
While I haven’t had a chance to play with the Moles Power Tools, Pex’s value was immediately apparent after installation. I’ll illustrate with a simple example shortly.
Before jumping into the code, I want to make an observation of the types of engineers I’ve met and their stance on Test Driven Development (based on my experiences).
On one extreme, you have the TDD enthusiast. These are people who ordinarily couldn’t sell water in a desert, but could make you HELOC your home if you were even thinking about buying into TDD. Conversations with TDD enthusiasts come to an abrupt end when the slightest bit of resistance is introduced to the topic (after which you’ll be eternally labeled a dullard in the eyes of the enthusiast until you come around).
On the other end you have the TDD hater. The haters look at unit tests as a complete waste of time, bringing nothing of value to anything but the bank accounts of consultants/contractors charging huge amounts per hour. Conversations about TDD usually cannot be started with these guys until an explanation of how you’re paid is disclosed. And even if you’re salaried, you’ll still get a only fraction of the hater’s attention if you’re lucky. (On a side note, in my experience, people on this end of the extreme are usually people who have problems with paradigm shifts, not out of an inability to learn new things, but an unwillingness to learn new things. An example of something a TDD hater might have said in their past would be ‘Internet-based applications? That’ll never take off…desktop apps are forever where its at!’).
And then, like most things in life, you have the guys in the middle of the road. They understand the importance of Test Driven Development and high code coverage and how that effects the stability of an application, but can’t make the switch to where they can write proper tests first and produce a working application in a timely manner. Enter Pex! Within seconds Pex can have you feeling like Big Meech (or Larry Hoover, whoever you like).
OK now for the code.
So the assignment handed to you by is a web based contact management system. The tables are all there populated with the data they want to view/edit, they just need the interface and they need it yesterday. You’re not an enthusiast and you’re not a hater. You’re a moderate who’d love to jump into writing tests first but you’re more productive jumping into the code first. So, you do what you do best and jump into the code.
NB. For this example, I’m not going through the trouble of creating a database with a contact table. I’ll use in memory collections to illustrate.
So you’re all setup and you’re project structure looks like the following:
The context acts as the layer between the presentation and data access layers, data access manages getting data from the repository (in this case an in memory collection), and the Contact object is the object modeled after the Contact table in the database (or in this case defines the type stored in the in memory collection).
In the business and data access layers you have your standard CRUD operations with the data access layer only visible to the business logic layer. So now when you import the core into your presentation project, only the business logic members should be accessible (OOP 101).
After installing Pex, you’ll have new context menu options available when you right click a project as shown here:
Followed by the following menu options:
NB. The filter options are used to direct Pex to generate tests ONLY for the specified the namespace(s), type(s), and methods.
And finally you’re prompted with a dialog that requests the location of the new Test Project that will be generated for you. I chose the defaults which generated the following test project for me:
From here you write your assertions and whatever else necessary to get your tests passing!
Here’s a link to a Channel9 Video on Pex.