Update to “How to run tests in a build without test metadata files and test lists (.vsmdi files)”: Test Categories

Buck Hodges

 Pierre Greborio, a developer over in MSTV, has contributed a great new feature to the power tool task that allows you to run tests without .vsmdi files: test categories.  Those of you who have used NUnit are probably familiar with the Category attribute.  Test categories allow you to execute specific groups of unit tests.  To tag a unit test method as belonging to a category, just add the Category attribute.  Unlike the test container feature, the test category feature will not be in Orcas (if you’d like to see it in a future release, be sure to let these folks know).

The new version of the TestToolsTask, targets file, and documentation are in an updated zip file attached to the original post, which has also been updated (download here).

While the details are discussed in the TestToolsTask.doc file, here’s how to use it.

Using the new Category attribute

IMPORTANT NOTE: The Category attribute feature will not be included in Visual Studio Codename Orcas.

The Category attribute must be specified for every test method that should be included in that test category. Note that there is no way to specify that all tests not in a test category should be run.

In order to use categories you need to:

1. Add a reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll in your project.

2. Add the namespace in the code:
using Microsoft.TeamFoundation.PowerTools.Tasks;

3. Add the Category attribute to the test method:
    public void MyTestMethod()
        // TODO: Add test code here

Finally, you will either need to specify the TestCategories in TfsBuild.proj if you are using Team Build or modify your project to call the task directly.

Pierre both wrote and tested the code (making heavy use of Reflector in the process).  Thanks, Pierre!

The other feature that’s new in this release is the support for test names.  This just exposes the equivalent to the mstest.exe /test command line switch.  The name that’s specified is implicitly a wildcard match.  Specifying “Blue” as the test name, for example, will execute every test method that has Blue in the name, including DarkBlue and BlueLight.  The TestName property will be available on the Orcas TestToolsTask (beta 2 and beyond).  The details are described in the documentation file.


Leave a comment

Feedback usabilla icon