Requiring unit tests prior to check-in
Yet another question that came up during our Team System migration was
How do we require developers to check in code that has passed all unit tests?
Another perfectly reasonable request. Unit tests can't help you if you aren't using them. Requiring developers to pass unit tests
before checking code in is always a good idea.
There is an existing
Testing Policy that should work great for this, right? Well, let's try adding it..
- Right click the project in Team Explorer
- Click Team Project Settings
- Click Source Control
- Click the Check-in Policy tab
- Click Add
- Select Testing Policy
- Click Browse
The test Metadata File (*.vsdmi) it's asking for is created under the
Solution Items folder when you create unit tests. This file will be under source control, so browse to the project and locate that file.
After I selected the file, I was greeted with this error message:
This Metadata file has zero test lists. Please choose one containing at least one unit test.
The Test Policy requires a test list. Unfortunately, the Developer edition of Team System (what we're all using here at Vertigo) cannot create test lists. This is a case where the role-based editions really bite us! I'm also left wondering why there isn't a default "all tests" list of some kind, but that's a discussion for another post.
In order to create a test list, we need Test Edition or Team Suite. Luckily I have Test Edition in a VM, so I created a test list in the
Test Manager:

I only put one unit test (ExpandPathTest) in this particular list (Sample) for simplicity's sake. Now that I have a test list, I can finally create the policy:

Once you do this, you'll get the expected policy failure
if you try to check in code and you haven't run the unit tests.

Note that
the check-in process will not run the unit tests for you. You must do a build, then run the tests via the Test View window prior to the checkin.
Oops, I did it again! Let's fix the offending code. Then I build the solution, and re-run this test..

Now I can check in without any policy warnings. Just remember, this policy warns about two things:
- If you haven't run the unit tests at all
- If your unit tests have errors
If either of those things is true, it's a check-in policy violation.