Visual Studio 2012 RC – What’s new in Unit Testing

Unit Testing feature in Visual Studio 2012 has been very enthusiastically received by the developer community. Our focus during RC milestone was to respond to feedback we received from Visual Studio 11 Beta. This article talks about some of the new features that we have built during this milestone, many of which were driven by customer feedback.


Smart Test Discovery

Previously, when a test project was built, Test Explorer would perform an exhaustive search to discover the tests. Now we have implemented a Smart Test Discovery algorithm which significantly improves the time required for updates to appear on the Test Explorer Window. In fact, this is now so blindingly fast that for the first few times I would rebuild the solution just to make sure that discovery was happening.


Run Tests After Build a.k.a Continuous Test Runner

This feature was introduced in Beta and so well-liked that we have now moved it into the Test Explorer tool bar.


When this feature is turned on, the tests are run automatically after every successful build. If there are failed tests from previous runs, they are executed first.


Enhancements in Metro Unit Testing

1. Assert.ThrowsException

Previously, we were using the ExpectedException attribute on a Test Method to handle scenarios where the test would throw an exception. This approach has many limitations, primarily the ability to precisely identify the source of the exception. To address this we have added Assert.ThrowsException.



Test will fail if the specified exception is not thrown by the test code.

NOTE: Assert.ThrowsException is available only in Unit Test Library for Metro style apps. In conjunction, we have removed ExpectedException Attribute from Unit Test Library for Metro style apps.


2. Code Analysis

Code Analysis is now enabled for Unit Test Library (Metro style apps).


3. Portable Unit Test Framework library

The Portable Class Library project in Visual Studio 2012 RC enables you to write and build managed assemblies that work on multiple .NET Framework platforms. For example, you can create classes that contain shared business logic for desktop apps, Windows Metro style apps, and mobile apps, and you can then reference those classes from those projects.

We have converted the Unit Test Framework into a Portable Class Library.


Run/Debug Tests from Editor

We have enabled a shortcut menu in the Visual Studio Editor which allows you to run or debug the tests under the cursor.



Compatibility with Visual Studio 2010

Based on extensive feedback we received from customers, we have implemented the following features related to compatibility.

1. Test List Editor (.vsmdi) can now be opened in Visual Studio 2012 RC. This feature has been deprecated and you cannot run tests from this editor. You also cannot create new Test Lists in Visual Studio 2012 RC. We recommend that you use Test Categories to group tests and maintain test lists.

2. All Test Templates in Visual Studio 2010 (Ordered Tests, Generic Tests, Web Performance Tests & Load Tests) can now be added in a Test Project. They will be listed in the Test Explorer. You can run them from the Test Explorer.



We have added a number of articles in MSDN for Unit Tests. Check out the Verifying Code by Using Unit Tests link for an overview and pointer to various walkthroughs and detailed articles.

If you have feature suggestions for Unit Testing, you can log them at Visual Studio User Voice.

You can log bugs at Microsoft Connect.

You can also discuss your issues in MSDN Forums.