Running NUnit tests in Visual Studio 2010 with code coverage

19,477

Solution 1

The gallio automation framework is an open source project which should allow you to accomplish this. The site is at http://www.gallio.org/ There is initial VS2010 support in the last release with more work on it in the nightly builds.

There is more information on this here: http://weblogs.asp.net/astopford/archive/2010/04/13/gallio-and-vs2010.aspx

However, if you have money to spend on this take a look at http://TestDriven.Net

Solution 2

Jetbrains appears to be tackling this issue with a new product called "dotCover". It's still in it's infancy, but given their track record it should be a major player soon.

Till it releases you can "test" the nightly builds located here.

Solution 3

You can get the VS 2010 code coverage functionality to work while running NUnit tests, but it's a hell of configuration work:

Set up a generic test that runs the nunit-console-x86.exe like this:

Does VS2010 Code Coverage support nUnit? (simplified description for xUnit)

http://msdn.microsoft.com/en-us/library/ms182624(v=vs.100).aspx (general description)

Make sure:

  • you have "instrument in place" activated. Trusting the deployment of VS won't work
  • you run nunit-console-x86.exe. The 64-Bit version won't work.
  • all libraries that need code coverage results are build into a single folder. Copying them (e.g. by build event) won't work. (For advanced: You can instrument the libraries in another location than their build output folder, but that's quite tricky.)
  • the folder with all libraries is working directory for the generic test and you start NUnit from it.
  • you provide the argument to NUnit as relative path to the working folder.
  • you additionally provide NUnit with the command line option /noshadow

When you copy your dlls from different folders into a single folder for tests, strange things may happen. I for instance, got the reassurring result that almost all of the code for unit tests is coverred when running the unit tests. What a shame, that code coverage did not find the library tested by the unit tests!

Well, after setting up all build output directories I ran into the strangest problem: From that folder NUnit does not run. I have it running from 2 separate locations, but here it goes "nunit-console-runner.dll not found".

I only got it right with instrumenting the tested library at another location via:

  • tests > edit settings > local
  • Data and diagnostics > code coverage
  • deactivate the original location and add the library from the test folder

Today it worked instantly, but the other day I tried to get the locations right for about an hour. I still don't know what I messed up that day.

Solution 4

Adrian,

Have a look at TestMatrix - it will run your unit tests, does code coverage and unit test performance profiling - and all of these are natively integrated into Visual Studio.

Solution 5

I found the Code Coverage Runner project on CodePlex, which bridges the VS test and NUnit APIs via an adapter. As of writing this, the project hasn't been updated since March 2008, but it may suit your needs.

Share:
19,477
adrianbanks
Author by

adrianbanks

Software Engineer, primarily developing in .Net/C# & SQL Server. Organiser of DDD East Anglia. Blog - adrianbanks.co.uk Twitter - @adrianbanks

Updated on June 05, 2022

Comments

  • adrianbanks
    adrianbanks about 2 years

    We have recently upgraded from Visual Studio 2008 to Visual Studio 2010. As part of our code base, we have a very large set of NUnit tests. We would like to be able to run these unit tests within Visual Studio, but with code coverage enabled. We have ReSharper, so can run the tests within Visual Studio, but it does not allow the code coverage tool to do its thing and generate the coverage statistics.

    Is there any way to make this work, or will we have to convert the tests over to MSTest?


    EDIT: We've already tried the suggested tools:

    • TestDriven.Net
      This runs the NUnit tests fine within Visual Studio, and even works with the code coverage, but it also includes the tests as part of the covered assemblies. There doesn't seem to be a way to exclude the tests, but include the assembly being tested (our tests are in a different assembly from the code they test). We also don't really want to spend the additional £2000 it would cost us to get TestDriven.net for the whole team.

    • Gallio
      This didn't work. In order to get coverage output, the tests need to be run in the Visual Studio test runner. Gallio does not provide this support for NUnit.

    Any further ideas? We did find a couple of open source attempts to get this type of thing working, but they are stale and haven't been touched for a long time (and therefore don't support VS2010).

  • dbort
    dbort about 14 years
    +1 for TDD.Net. if you use nCover's Community Edition for coverage, and have upgraded your projects to .net4 you may need to buy a full version as when i tried the Community Edition did not support .net4 assemblies. Worth while investment though.
  • adrianbanks
    adrianbanks about 14 years
    That looks good, but it still is quite pricey compared to the "free" tools that are already available to us in VS2010 premium that we have already paid (a lot!) for.
  • sergeb
    sergeb about 14 years
    Valid point, Adrian! If the VS2010 tools you paid for were to do what you want... Again, compared to what you paid for VS2010 edition w/ code coverage, TestMatrix is free, wouldn't you agree?
  • Anindya Chatterjee
    Anindya Chatterjee almost 14 years
    TDD.net also has a Personal Edition which is totally free for non commercial use.
  • Jura Gorohovsky
    Jura Gorohovsky almost 13 years
    As an update, dotCover is publicly available, currently as version 1.1