Can MsTest unit tests be grouped in categories

Hamish Grubijan picture Hamish Grubijan · Nov 24, 2010 · Viewed 9.1k times · Source

In MbUnit one can do something like this:

[Test]
[TestCategory("Bad Arguments")]
[TestCategory("Fast")]
[ExpectedException(typeof(ArgumentNullException))]
public void TestCopyWithBadHref()
{
   . . . 
}

Note these two:

[TestCategory("Bad Arguments")]
[TestCategory("Fast")]

Since TeamBuild can be integrated with MsTest to perform gated check-ins and/or run at night, it is a great feature! However, given that some tests can run for a long time, it is convenient to separate them into the tests that should run before every check-in is confirmed, and the tests that should run at night instead due to their duration as well as other factors.

One way to go about achieving this might be creating several projects - one for slow tests, one for fast tests, etc. However, this separation is inconvenient. Project dependencies would not feel as natural, plus some tests can be in more than one logical category.

It would be great if MsTest had something similar to what MbUnit has had for a long time. For instance, one can run MbUnit.Cons.exe and specify the category to be used with a command-line switch.

How can I achieve the same using MsTest? We are a MSFT shop, and I failed to sell MbUnit to my co-workers.

Answer

Mike Zboray picture Mike Zboray · Nov 24, 2010

You can use the /category option to filter the tests in VS 2010.

It uses the testcategory attribute. Details on the /category option.

C# code might look something like this:

[Description("test 123456"), TestCategory("Edit Tests"), TestCategory("Non-Smoke"), TestMethod]
public void VerifyEditsPersist()