Does VS2010 Code Coverage support nUnit?

12,602

Solution 1

It's doable, but requires a bit of setup. I just got it working with xUnit. Presumably the below will work with NUnit too.

For this to work, you'll need three projects

  • The System Under Test -- SUT
  • A testing project using your favorite unit testing framework -- xUnitTest
  • A VS Test Project -- VSTest

    1. Create the VSTest project (breath easy)
    2. Delete the default UnitTest1.cs file
    3. Add a "Generic Test" to VSTest
    4. Specify the full path to the win32 version of the console runner for your testing framework, such as xunit.console.x86.exe
    5. Specify the full path to the xUnitTest dll
    6. Under the VS Test Menu->Edit Test Settings->Local->
    7. Data & Diagnostics "Tab"
    8. Make sure only Code Coverage is enabled
    9. Select Code Coverage row, then click the "Configure" button above (yes, this is a well designed UI :)
    10. In the "Code Coverage Detail" dialog
    11. Select both the dlls for SUT and xUnitTest
    12. Enable "Instrument assemblies in test"

Now when you run the VS2010 test, it'll correctly instrument the test dlls, run the code runner and gather the info into Visual Studio.

Solution 2

Visual Studio 2012 added support for third party unit test frameworks.

The test framework plugins available include:

  • NUnit
  • xUnit.net
  • MbUnit
  • QUnit
  • Jasmine

Adding support is very easy because you can add it from the Visual Studio Extension Manager.

For example you can add the "NUnit Test Adapter".

And now the "Analyze Code Coverage" works with third party unit framework. You can inspect all tests or only one.

And if your using VS 2010 don't be afraid to upgrade to VS2012 because sln files are compatibles (you need VS2010 with SP1).

Solution 3

After fiddling with this for a little over an hour, I could not get it to work properly. I was able to get the generic test running properly with instrumentation for NUnit 2.5.5 using nunit-console.exe. Although the NUnit console runner ran my test successfully via the generic VSTest, I was never able to generate code coverage results.

Although the test features of VS2010 sound great when reading about them, the implementation seems overly complex and very heavy. I've been using NUnit for years with great success and very low friction.

I decided to try out JetBrains dotCover beta and within 5 minutes I downloaded it, installed it, and was able to configure and use it very successfully. It integrates seemlessly into ReSharper 5 and just worked for my NUnit tests. The code highlighting is a great visual tool to quickly locate blocks of code that were not executed by the tests.

Solution 4

I tried every method I found on Google to enable that and then decided to give TestDriven.NET a try. It worked brilliantly: right-clik on the solution, test with coverage and done. Seriously, it couldn't be any easier.

Solution 5

You need to turn off shadowcopy in NUnit to get it to work. To do so, use the /noshadow flag.

e.g. C:\Program Files (x86)\NUnit 2.6\bin\nunit-console-x86.exe MyLibrary.dll /noshadow

Share:
12,602

Related videos on Youtube

stiank81
Author by

stiank81

System Developer mainly living in the .Net-world. Coding in C#, Javascript, html, css, ..

Updated on September 03, 2020

Comments

  • stiank81
    stiank81 almost 4 years

    According to this schema VS2010 Premium and Ultimate has a tool for checking Code Coverage - together with a few other testing tools. Does this support nUnit too, or just MS test?

  • stiank81
    stiank81 about 14 years
    Thanks for commenting. +1. Don't know if I can trust this without a source if you're actually selling a competing product :-P So I'm gonna leave this hanging for a while longer..
  • sergeb
    sergeb about 14 years
    This is fair. You don't have to trust me - download a trial of VS2010 and see it yourself :) And we are not competing with VS - we are offering complementary products to enhance it... BTW, I just tried built-in to run dasBlog (uses NUnit) tests in VS2010 Premium - it doesn't see the test. VS2010 Trial download is here - msdn.microsoft.com/en-us/vstudio/bb984878.aspx
  • stiank81
    stiank81 about 14 years
    Sounds frustrating.. I didn't try myself yet, but it sounds like it should be possible then. Please shout out if you figure it out!
  • stiank81
    stiank81 about 14 years
    Thanks! Not the smooth answer I was hoping for though.. :-P
  • LeffeBrune
    LeffeBrune almost 13 years
    Have been trying for several hours now. I do get the instrumented assemblies passed to nunit-console but never get code coverage results back.
  • LeffeBrune
    LeffeBrune almost 13 years
    I tried to set this up and got nunit running instrumented assemblies, but code coverage data is missing. Upvoted your bug on MS site.
  • LeffeBrune
    LeffeBrune almost 13 years
    There is a small detail that escaped from me -- you absolutely have to use x86 runner for your tests (such as nunit-console-x86.exe). Changing the test runner to x86 fixed issues I have been having.
  • LeffeBrune
    LeffeBrune almost 13 years
    Make sure you use x86 runner for your tests (nunit-console-x86.exe).
  • Bentley Davis
    Bentley Davis over 12 years
    I just tried this for NUnit and worked around an issue. The coverage only reported on the NUnitTest.dll and did not show up for the SUT.In step 11, instead of just selecting the SUT which is in the SUT directory I had to click add assembly and choose the SUT.exe that is in the bin directory of the NUNITTest directory.
  • Tod Thomson
    Tod Thomson about 11 years
    This is the way to do it. I would not waste your time doing the ticked (more complex solution) when this will work fine.
  • Forty-Two
    Forty-Two almost 11 years
    Only applies to VS2012 Ultimate or Premium. Analyze Code Coverage is not avaliable in Professional. source and source