12

I've switched to using VS 2012 and for most parts it is great. The problem is I can't find how to filter my unit test by category?

4 Answers 4

13

Assuming you have used the TestCategoryAttribute like this:

[TestMethod, TestCategory("MyTestCategory")]
public void MyTest() {}

In the Test Explorer window, to include a particular category, type this in the filter text box: Trait:"MyTestCategory"
To remove tests of a particular category, type this in the filter text box:
-Trait:"MyTestCategory"

You can filter out or include multiple categories. This allows you to filter out integration tests and automated UI tests in the Test Explorer window so that you can use the "Run Tests After Build" more effectively.

See here for more: http://msdn.microsoft.com/en-us/library/hh270865.aspx#BKMK_Searching_and_filtering_the_test_list

4
  • So I got this to work however whenever I re-open a visual studio this is cleared out of the test filter. Whenever I open Visual Studio i want it to automatically have -Trait:"BuildSkip" always added when i open a specific solution/all the time (if I can't per solution).
    – SteckDEV
    Mar 11, 2015 at 17:47
  • Microsoft doesn't seem to have provided a way to do this with their UI. There may be a way to hack it, but I usually just select the filter I want from the drop down of recent choices. Mar 12, 2015 at 2:36
  • 1
    Thank you. Filtering tests by TestCategory works the same way in Visual Studio 2015
    – Boogier
    Sep 20, 2016 at 9:10
  • 1
    Works just fine in VS 2022 as well.
    – julealgon
    Nov 11, 2021 at 21:39
7

In the most recent Visual Studio 2012 CTP this feature has been added. The Test Explorer now has the option to group tests by traits (categories) and to filter by traits (categories) https://msdn.microsoft.com/en-us/library/hh270865.aspx

0
4

I too have been looking for this feature and as far as I can tell you can't filter by Category. I tried using a search filter Category:"categoryName", but that didn't work. It appears you can run by test categories from the command line (http://msdn.microsoft.com/en-us/library/dd286683.aspx), and you can define which categories to run in a build (http://msdn.microsoft.com/en-us/library/dd286595.aspx), but it does not appear possible from the IDE.

I was hoping to try out the default test tools of Visual Studio 2012, but with the reliance I have on test categories, the only option appears to be 3rd party commercial tools, such as JetBrains dotCover.

0

In the new Test Exlorer shipped with 2012, you can group tests by 'Test Results', 'FileName',... etc but in RTM build group/search by 'TestCategory' is not present from Test Explorer. However there is a filter in commandline and TeamBuild based on TestCategory (Running selective unit tests in VS 2012 RC using TestCaseFilter) in VS2012.

From a member of VS Team, they understood the importance of this missing feature. It is in the backlog, and it should be available in furutre builds/updates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.