How to set test category for all tests in the class
Solution 1
To be able to set the [TestCategory]
attribute at the class level, install the “MSTest V2” TestFramework using NuGet.
Solution 2
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
Edit:
Adding the TestCategory
attribute at the class level is now available with MSTest V2, as noted in NomadeNumerique's answer below. Details
Solution 3
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.
Solution 4
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.
Solution 5
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.
Related videos on Youtube
Comments
-
alex almost 2 years
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.
-
nikita about 11 yearsInteresting. Don't think its possible to mark all methods in class with attribute working with TFS tests. TFS understands TestCategory and Priority and these attributes only method specific. You can move integration tests to separate project and point TFS not to run it.
-
Ryan Gates about 11 yearsCould you separate your unit and integration tests into two different assemblies and then configure your build accordingly?
-
alex about 11 yearsWell, we have a dozens of libraries and each of them has a corresponding test library that may contain both - unit and integration tests. Splitting each of them into a library for unit and a library for integration test is possible, but does not look like the most elegant approach.
-
KMoraz about 11 yearsThe closest you can get is defining categories as const strings, but you still have to change all attributes manually, e.g.:
[TestCategory(INTEGRATION)]
-
Pedro Pombeiro about 10 yearsIf you are using am AOP framework such as PostSharp, you could leverage it to inject the attributes you want into every method (maybe look at an attribute of yours at class level and translating it to TestCategory at the method level). I've t fine this for the Description attribute and it worked well (although I was using NUnit).
-
-
Dagrooms about 7 yearsBut what do you do with multiple categories?
-
Ben Anderson over 4 yearsOther answers are old, if you use VS 2017 or newer you should follow this answer