Tuesday, 16 July 2013

How to Run Tests After Build in Visual Studio 2012 Professional

One of the great features of Visual Studio 2012 that gets blogged about a lot is the option to run all your tests automatically after you build your project. If you’ve read about this feature and went looking under the Test menu for the option to turn it on, you may have been disappointed to find it’s not there. I know I was.

Many blog posts and tutorials don’t mention that this option isn’t available unless you have VS 2012 Premium or Ultimate. I’m running Professional, so no special sauce for me.

I want to Run Tests After Build, but I only have VS 2012 Professional…

Yeah, it seems to me like most folks out there aren’t running VS 2012 Ultimate, but many of us want to use this feature. There are several third party tools which will let you do this like NCrunch, which does continuous testing while you code. But that is a paid tool, and you may not have budget for it, or you may have to use the tools given to you by your employer.

Scott Hanselman occasionally says that the compiler is the first unit test, and I would often find myself hitting the F6 key in VS 2012 to build my solution, just to make sure it did.

When I started to get serious about testing I began to use the “Run All” link in Test Explorer frequently.

RunAllTestExplorer

That button saves any modified files, builds the solution (equivalent of F6) and then runs all my tests. Pretty good, except it means a trip to the mouse. I was missing the simple efficiency of pressing F6 and getting a result quickly.

It turns out there is a keyboard “shortcut” to Run All, it’s Ctrl-R, A. I think it’s quicker to use the mouse…

Remapping keys in Visual Studio 2012

After 6 or 8 months of clicking and wishing I had Visual Studio Ultimate, I realised that keyboard shortcuts in Visual Studio are fully customisable. Shortly after I decided to remap my F6 key to the Run All command in Test Explorer.

You can read how to do this on MSDN.

Here is the result:

VSOptionsKeyboardRemapF6

When you do this, the Build command gets moved to Ctrl+Shift+B.

VSOptionsKeyboardBuild

Before I remapped my F6 key, I used to keep Test Explorer hidden down the left side of the main window. I figured I didn’t need to see it, except when I was running tests which was done with the mouse. One change I made to my window layout is to make the Test Explorer window always visible (now it’s at the bottom of the screen) so that I can see if there are any failed tests after I press F6.

FailedTest

Conclusion

It’s not as cool or easy as just ticking a box like all the folks with Ultimate, but if you are looking for a free and easy way to run all tests at build, remapping your F6 key is a good way to go.

One shortcoming of this suggestion is that if you use the menu to build your solution, your tests don’t get run, but I can live with that.

Happy coding (and testing)!

3 comments:

  1. Awesome tip! Exactly what I was looking for. Thanks for posting!

    ReplyDelete
  2. So Cool thanks for this it just works great, although when Test Explorer window is hidden or unpinned you have no way to know weather you broke any test, anyway thank you.

    ReplyDelete
    Replies
    1. Glad you found it useful. I agree that is a limitation. I ended up moving the Test Explorer window to the bottom pane so that it could be visible enough at all times to give me that visual feedback. There is a screen shot of that layout near the bottom of the post. Cheers, Jason

      Delete