Visual Studio Unit Test Organization

Visual Studio 2012 introduced first introduced us to the “Test Explorer” that we all use today. I remember the feedback at the time as some were quite upset about  missing their old “Test Results” window from VS2010 and older versions. At the surface level there was definitely less options for grouping your tests in the new Test Explorer, but it was too tightly integrated with mstest.

blog1

Visual Studio 2010

While this information is not anything new (in fact almost 3 years old), I thought it worth reviewing as it seems many people were simply not aware of some of the simple organization components. The new Test Explorer gives us much more extensibility, and I’ve found no problem utilizing and organizing the tests in the way I prefer. That being said, there are two key methods to be aware of when organizing your tests:

1. You need to right-click and select “Group By” from the context menu to achieve some of the standard grouping options (its surprising how many people are not aware of these default groups).

blog2

2. Secondly, and most important, I have thoroughly appreciated the ability to group the unit tests by assigned attributes, or “Traits”. Traits are custom properties or values that can be assigned as a method attribute to any test (or even test class), like this:

 blog3

 

In this scenario, if I now group my tests by “Traits” I will receive all my tests bucketed into “Report Execution” (or whatever custom names you have applied) and “No Traits” for those I do not markup. You can also utilize the “TestProperty” attribute to apply traits, and values associated with Traits. You can even apply multiple traits to a test method. This will force the test to show twice under each group (but actually only run once from what I understand).

blog4

This functionality should allow you to fine tune however you want to view and group your tests in a very custom way. For much more in depth information on utilizing the traits and attributes, check this out: http://blogs.msdn.com/b/visualstudioalm/archive/2012/11/20/part-2-using-traits-with-different-test-frameworks-in-the-unit-test-explorer.aspx

You can also take a look at ReSharper, which offers much more functionality in sorting, grouping and running tests inside Visual Studio.

Leave a Reply