How to set test category for all tests in the class

21,348

Solution 1

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/

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

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

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.

Share:
21,348

Related videos on Youtube

alex
Author by

alex

interests: programming , C# , devops

Updated on July 09, 2022

Comments

  • alex
    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
      nikita about 11 years
      Interesting. 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
      Ryan Gates about 11 years
      Could you separate your unit and integration tests into two different assemblies and then configure your build accordingly?
    • alex
      alex about 11 years
      Well, 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
      KMoraz about 11 years
      The 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
      Pedro Pombeiro about 10 years
      If 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
    Dagrooms about 7 years
    But what do you do with multiple categories?
  • Ben Anderson
    Ben Anderson over 4 years
    Other answers are old, if you use VS 2017 or newer you should follow this answer