Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running unit tests

In our solution we have several test projects. Most of them contain unit tests that run fast but some are for integration, acceptance and UI tests which take quite some time to finish. Now I'd like to have a convenient way of running only the unit tests. Either by using the hotkey to run all or using the new feature to run tests after each build.

Unfortunately VS also runs all the slow tests as well. In VS10 I grouped the tests in Test View by project and selected only the ones containing unit tests. It's not as easy as I wished it to be but at least it was possible. Now there doesn't seem to be a way around running all tests in a solution.

The only alternative that I see at the moment is to remove all but the unit tests from the solution but this would limit the refactoring capabilities.

So does anyone know how to easily group and run only unit tests in the new VS?

like image 753
devdumbblind Avatar asked Aug 29 '12 06:08

devdumbblind


People also ask

When should you run unit tests?

Short answer: run all unit tests on the build server for every commit, on every branch. Assuming your unit tests don't take a really long time to run, there really is no downside to this. As for running all unit tests on every build task locally, that would be a overkill.

Why do we run unit tests?

One of the benefits of unit tests is that they isolate a function, class or method and only test that piece of code. Higher quality individual components create overall system resiliency. Thus, the result is reliable code. Unit tests also change the nature of the debugging process.

How often should you run unit tests?

When you follow the Red/Green/Refactor process, ideally you'd be running your unit test suite at least three times for each iteration: Red. Run the test suite.


4 Answers

In Visual Studio 2012 Update 1 CTP, Test Explorer has added the option to group tests by Project or Traits (Categories).

Please go to this link to see what's new there.

like image 152
Qing Zhao Avatar answered Oct 21 '22 14:10

Qing Zhao


You can use the search filter in Unit Text Explorer.

enter image description here

In the new Test Exlorer shipped with 2012, you can group tests by 'Test Results', 'FileName',... etc but in RTM build group/search by 'TestCategory' is not present from Test Explorer. However there is a filter in commandline and TeamBuild based on TestCategory (Running selective unit tests in VS 2012 RC using TestCaseFilter) in VS2012.

From a member of VS dev Team, they understood the importance of this missing feature. It is in the backlog, and it should be available in furutre builds/updates.

The following blog provides more details: http://blogs.msdn.com/b/vikramagrawal/archive/2012/07/23/running-selective-unit-tests-in-vs-2012-rc-using-testcasefilter.aspx

like image 28
Cybermaxs Avatar answered Oct 21 '22 16:10

Cybermaxs


As of Visual Studio 2012 Update 2 (currently on CTP) now we are able to run a subset of unit test using "Playlists" or based on "Lists & Classes". More information here: http://blogs.msdn.com/b/visualstudioalm/archive/2013/01/30/first-ctp-for-visual-studio-update-2.aspx#playlists

Original Answer:

AFAIK, The new unit testing framework in VS2012 "supports selective execution based on filtering condition through TestCaseFilter". Unfortunately, currently there is not support to do this through the user interface but it is in the product group's backlog. You can use this approach through Test Build or command line. Please refer to the following blog: http://blogs.msdn.com/b/vikramagrawal/archive/2012/07/23/running-selective-unit-tests-in-vs-2012-rc-using-testcasefilter.aspx

EDIT: Following the MSDN documentation to use test categories to group tests.

http://msdn.microsoft.com/en-us/library/dd286595.aspx

Also, refer to this question: How to exclude certain tests in the Visual Studio 2012 Test Runner?

It shows an example of filtering based on the test path.

like image 21
Mauricio Aviles Avatar answered Oct 21 '22 16:10

Mauricio Aviles


I'd recommend using TestDriven.Net. Group all Unit Testing projects in one solution folder, and all other tests in another. Then just right click the "Unit Tests" folder and choose "Run All Tests". I always favor this great test runner of the VS native testing tools.

Another approach is using NCrunch - It runs all you tests in the background while you code. It handles integration tests pretty well, as they run in parallel, in the background so it doesn't block you (you also choose which test projects are run). One catch though - I believe it doesn't support VS2012 yet, but probably soon.

like image 32
seldary Avatar answered Oct 21 '22 14:10

seldary