Can you create unit tests with VSTS and still have fun?
Posted by Brian FinnertyWe’ve just released a new Drill on Unit Testing with Visual Studio Team System - I was pleasantly surprised to discover that it is possible to have fun (and learn something useful) while creating unit tests against your existing code. The very first coding challenge we set developers in this 3 hour Drill is to create a unit test for a gaming application in which two crafts negotiate their way around a game area avoiding obstacles and trying to shoot each other. Normally I wouldn’t condone this type of violent behavior between two rival programmatic “entities”, but it is all in the service of further educational goals…really….
In this coding challenge, the sample project contains a Ship class which represents the two game entities and the behavior of the ships is controlled by two different state classes, SearchingState and AttackingState. There is no rope-a-dope strategy in this scenario, by the way - both ships are just out to seek and destroy each other, no questions asked. The state can be set by calling the SetState method of the Ship class and passing an instance of the required state object. There is no way to verify the behavior of the SetState method, the solution doesn’t contain a VSTS test project, and there are no unit tests. So your job is to add a new test project to the existing solution, and to create a unit test which verifies the behavior of the SetState method of the Ship class. Not a bad way to create your first unit test in VSTS, now is it?
You’ll be glad to hear that we stay with the destructive gaming theme for the rest of this Drill, focusing on the following tasks:
- Introduction to Unit Testing with Visual Studio 2005
- Implementing the dependency injection pattern
- Implementing the test stub pattern
- Implementing the mock object pattern
- Using shared fixtures to improve test performance
- Using custom assertion and finder methods
Incidentally, we use a number of reference materials throughout this Drill that will really help to further your knowledge of VSTS, including the book Working with Microsoft Visual Studio 2005 Team System by Richard Hundhausen (Microsoft Press, ISBN-10: 0-7356-2185-3). Another VSTS source that I’ve found to be pretty informative is Jeff Behler’s Blog, which gives you the inside track on Microsoft’s use of VSTS to manage internal software projects. Have fun and remember that we only encourage use of the AttackingState in sample applications that serve a higher purpose!













