Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I filter unit tests in Visual Studio 2012 by category?

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?

like image 632
Ido Ran Avatar asked Sep 20 '12 07:09

Ido Ran


People also ask

How do you run a specific test in VS?

Tests can be run from Test Explorer by right-clicking in the code editor on a test and selecting Run test or by using the default Test Explorer shortcuts in Visual Studio.

How do I automate unit testing in Visual Studio?

To generate unit tests, your types must be public. Open your solution in Visual Studio and then open the class file that has methods you want to test. Right-click on a method and choose Run IntelliTest to generate unit tests for the code in your method. IntelliTest runs your code many times with different inputs.

How do I get unit test coverage in Visual Studio?

Starting in Visual Studio 2022 Update 2, you can enable faster code coverage test results by selecting Tools > Options > Environment > Preview Features, then selecting Code coverage experience improvements, and then restarting Visual Studio.


3 Answers

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

like image 72
David Meredith Avatar answered Nov 05 '22 22:11

David Meredith


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

like image 29
Todd King Avatar answered Nov 06 '22 00:11

Todd King


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.

like image 4
DeveloperRob Avatar answered Nov 05 '22 22:11

DeveloperRob