Unit Testing in Visual Studio 11

There are some great features of Unit Testing in Visual Studio 11. I am excited to see how the new version of Visual Studio brings new features and abilities, improving upon some things that were difficult in 2010.

Creating Unit Tests

First things first, you’ll want to know how to create and run a unit test! In Visual Studio 11, you have the ability to plug in MSTest, NUnit, and xUnit without extra hassle. You will also be able to build and run these test cases with a build definition (on a build server). In this first video, I make a simple test case and run it using the Unit Test Explorer.

VIDEO – Creating Unit Tests in Visual Studio 11

Running Unit Tests

Now that you’ve seen how to run a unit test after creating it, let’s dive a bit deeper. Featured below are more of the specifics of the Unit Test Explorer, how to run unit tests, and some highlighted features within it.

VIDEO – Running Unit Tests in Visual Studio 11

Debugging a Failed Unit Test

What happens when you run into a failed unit test? The Unit Test Explorer will give you information about the failed test (i.e. why it didn’t pass, the exception, if one was thrown, the stack trace, etc.) as well as jumping right to the code when double clicked. This video looks at these aspects and fixes a unit test that has failed.

VIDEO – Debugging Failed Unit Tests in Visual Studio 11

Code Clones

A new feature with Visual Studio 11, is the ability to search for code clones within your solution. There are two, in my opinion, very helpful approaches to take. I will go into both, but there is one thing I want to note: Visual Studio 11 will analyze/search for code clones only within the solution that is open. So, if you have other projects that do not exist within your solution but you will reference them, they will not be analyzed/searched for clones.

Analyze for Code Clones

The first approach when looking for code clones is to merely analyze your entire solution for code that seems similar. This  doesn’t simply look for code that is identical, but also code that is simple.  This means you may consider refactoring code to keep the cost of maintenance down in the future.

VIDEO – Analyze for Code Clones in Visual Studio 11

Search for Code Clones

The second approach for looking for code clones doesn’t look at the high level solution, but  instead, searches for specific lines that you deem may be similar elsewhere in the solution. This is helpful in similar ways to searching the whole solution, but it also has other advantages. For instance, if you’re writing a new method that seems oddly familiar, you can highlight code and search for a clone to see if it has already been written.

VIDEO – Search for Code Clones

Code Coverage

In Visual Studio 11, you can now easily analyze the code coverage your tests have, (whereas it would take eight clicks in Visual Studio 2010). The video below shows how to run code coverage and shows the results in the new Code Coverage Results view.

VIDEO – Code Coverage in Visual Studio 11

Run Tests After Build

Visual Studio 11 does not directly have test impact analysis, such as Visual Studio 2010 did. Test impact analysis is not visible via a Build in Test Manager. Instead, they have added the ability to run unit tests after a build. There is of course logic as to which tests to run, but it is not logic based off of test impact analysis. This video will run through the logic path that will happen when tests run automatically after a build is complete.

VIDEO – Run Unit Tests After a Build in Visual Studio 11


All in all, there are some pretty neat features in Visual Studio 11 for unit testing. It has definitely been made easier to do common scenarios that one would find in an ALM environment. I think some of my favorite features are the new views and the ability to look for redundant code that can be refactored.

-Find other related Dev on Demand Videos.

Do you want to move to Visual Studio 11 NOW? For help moving forward, please contact Rick.Flath@nwcadence.com.

About the Author:

One Comment

  1. Kalyan Bandarupalli January 2, 2013 at 3:13 pm

    Great post with details, The new testing features introduced in Visual Studio 2012 update 1 can be found here http://www.techbubbles.com/testing/testing-features-added-in-visual-studio-2012-update1/

Leave A Comment