New Unit Tests features of Visual Studio 2012

New Unit Tests features of Microsoft Visual Studio 2012

Visual Studio 2012 was already launched some time ago. A lot of new features were introduced in this new version. Some of the changes made to Visual Studio 2012 are big and in some aspects, the difference between the old version of Visual Studio and the new one are like the differences between Windows 8 and Windows 7.

I think that a lot of developers have already played a little with this new version of Visual Studio. The first thing that you notice after installing is the time necessary to load the new Visual Studio – it loads very quickly. The second thing is the new UI, that is Metro like.

One area where Visual Studio 2012 made a big step forward was Unit Testing. A lot of new features were introduced that not only increase the developer’s productivity, but also increase the quality of the code. In the next part of the post we will discover what new features were introduced from the unit testing perspective.

Run tests after each build

In the old version of Visual Studio, we didn’t have a mechanism for running unit tests after each build. That is why we had to click on “Run All Tests” each time when we built the solution. This could become annoying after a while. Visual Studio 2012 already has the build feature and it can be deactivated with only one click.

Figure 1

All the tests run in the background, without bothering us.

Debug/Run Tests Directly  from Code

How many times did you have to go to the Test Explorer window in order to run your current test. From now on, Visual Studio 2012 has a shortcut in the menu that will run the tests selected in the code.

Figure 2 - Visual Studio 2012

Group and Run Unit Tests Based on Category

From now on we can add one or more categories to our unit test. This attribute can be added not only to each test in part but also to the unit test class. Using this feature allows us to run unit tests only from a specific category. This is a great feature not only on the developers’ machine, but also on the build machines.

Figure 3

We need to specify the TestCaseFilter  option in order to be able to run the unit tests that have a specific category from the command line:

Vstest.console.exe myFooProject.dll /TestCaseFilter:”TestCategory=”Atomic”



Until now, if we had a unit test that would check if a method throws an expected exception; we would have to decorate our method with an attribute. But this is not the best solution, because there were cases when we could not determine the source of the exception precisely.

Figure 4

This feature is only available for Windows Store Application right now. We hope that this new feature will also become available for other types of projects.

Smart unit test discovery

The algorithm that is used to discover new unit tests was improved New unit tests are usually discovered extremely quickly. Gone are the days when we had to rebuild the solution and wait until the new unit tests were discovered by Visual Studio.

Integration with JavaScript

Using a plug-in like Chutzpah Test Adapter, we are able to run unit tests written in JavaScript and run them in Test Explorer. We can also run tests written in C# and JavaScript at the same time. This helps us run the entire test as one unit. The only thing that is necessary is to install Chutzpah Test Adapter and use a unit test package for JavaScript such as qunit.

Unit testing is a lot easier with this new version of Visual Studio. These are only a few of the new features that Visual Studio 2012 brings to the table. You can try it and discover these new great features for yourself.


Tags: ,

One comment

Ctrl+R,T for running the current unit test was available also in previous versions of VS.

I think another two nice new additions to VS2012, useful for unit testing are MS Fakes Framework (the successor to Moles) and the ability to run unit tests written with other unit tests frameworks (NUnit etc.) by using custom tests adapters..

Sure, both were available for a long time from other companies (Moq, TestDriven.Net) and finally MS came with an in-house offering.

Leave a Reply

Your email address will not be published. Required fields are marked *