How to set test category for all tests in the clas

2019-03-22 11:04发布

问题:

I am using MSTest, and I want to set the same test category for all methods in test class at once, without setting TestCategory attribute to each method individually. How can this be done?

The most convenient and obvious way would be to set TestCategory attribute on class, but it can be applied to methods only.

The ultimate goal is to skip integration tests during test run on TFS check-in.

回答1:

I've been looking to do something similar, and I've arrived at a solution that works really well for my purposes.

This does not solve the problem of applying a TestCategory on a per-class basis, but you can use the /test: command line argument for mstest to specify a search string to match any part of the fully qualified method name of the test. That means you can generally match against the class, or the namespace, or whatever search string you can arrive at that will match the target tests. And if that doesn't do it, you can use the /test: argument multiple times. I.e:

> mstest /testcontainer:My.dll /test:My.FullyQualified.Namespace 
    /test:My.FullyQualified.OtherNamespace.OtherClass

More Info

Edit:

Adding the TestCategory attribute at the class level is now available with MSTest V2, as noted in NomadeNumerique's answer below. Details



回答2:

The ultimate goal is to skip integration tests during test run on TFS check-in.

There are other ways to do this. In your TFS builds, you can set which unit tests you want to run, depending on their assembly name.

As the default behaviour, it will run all unit tests in assemblies which have "test" in their name. A simple fix would be to rename your integration tests to something that does not include "test".


If you do want to use the categories, you could try using AOP. For example, with Postsharp, you can create an aspect in your integration test assembly that puts the attribute on the method. Then enable the aspect for all public methods in your integration assembly if all tests are grouped in one dll or on each integration test class.



回答3:

To be able to set the [TestCategory] attribute at the class level, install the “MSTest V2” TestFramework using NuGet.

Ref: https://blogs.msdn.microsoft.com/devops/2016/06/17/taking-the-mstest-framework-forward-with-mstest-v2/



回答4:

One way to get around this limitation is to put the test category in the beginning of each test method. For example, name your unit tests

public void UnitTestDoSomething_ExpectThis()

and your integration test

public void IntegrationTestDoSomething_ExpectThis()

Then when you do your TFS query to get the integration tests you can do

Field[Automated Test Name] with Operator[Contains] and Value[IntegrationTest]

Although this is not a perfect solution, it will help you distinguish your tests in code and TFS. Alternatively, you can look at area and iteration paths.



回答5:

You can group by "Class name" into Test Explorer panel.

With the test TestCategory attribute you can not solve your issue just because attributes in C# are meta-data and can not be used as dynamic values.



回答6:

In VS 2017 this is possible (and looks to be part of VS2012 update 1).

You can put [TestCategory("Integration")] on a class in your unit test and have it apply to all tests, and likewise [TestCategory("Unit")] on your unit test classes.

You can then use the Test Explorer's Search bar to filter by Trait name = Unit and the "Run All" will only run the tests matching your search.

When you go to run these tests on your build server, you can use a switch like /category:Unit to only run the unit tests.