19

I want to run coverage from the command line, but can't seem to get NCover 1.5.8 to instrument the code. It must be possible as when I run coverage tests with TestDriven.net it works. the difference seems to be that TD.NET is able to get NCover to use framework 4.0 (you get this in the log when it runs : MESSAGE: v4.0.30319) but from the command line I can't make it (I get this in the log : MESSAGE: v2.0.50727)

So how can I make NCover play nice with nunit from the commandline, like it does with TD.NET?

Sam Holder
  • 32,535
  • 13
  • 101
  • 181

2 Answers2

27

after more searching I found this:

If you have found this thread because you are trying to get NCover 1.5.8 to work with .NET 4 then the following should fix this error:

Open a command prompt and type the following set COMPLUS_ProfAPI_ProfilerCompatibilitySetting=EnableV2Profiler

This instructs the .NET 4 CLR to load the .NET Framework 2.0 Profiler.

For more information see: http://msdn.microsoft.com/en-us/library/dd778910.aspx

at the end of the thread here

which seems to solve my problem

EDIT:

it doesn't solve my problem really. Now it just allows the coverage.xml to be generated, but it only contains the v2.0 framework assemblies, so only the .net 2.0 assemblies are profiled....

Grrr. back to the drawing board...

EDIT 2

Hallelujah! I have figured this out by a process of random googling and changing. anyway, due to some pointers found here I was able to figure out that what I needed to do was to alter the exe.config of the application running the code (nunit.console-x86.exe in this case) file to not only force a specific version of the .net framework to be loaded but also to allow the legacy activation policy to be used. To cut a long story short I was able to solve this by:

  • Adding to the nunit-console-x86.exe.config the following section:
<configuration>  
    <startup useLegacyV2RuntimeActivationPolicy="true">  
        <supportedRuntime version="v4.0.30319"/>  
    </startup>  
</configuration>
  • setting these in the environment that the command is launched from:

    set ProfAPI_ProfilerCompatibilitySetting=EnableV2Profiler
    set COMPLUS_ProfAPI_ProfilerCompatibilitySetting=EnableV2Profiler
    (not sure both are necessary, but that's what I did)

I also added a similar setting to the NCover.Console.Exe.config, but it turns out that is unnecessary.

EDIT 3

this is the command line I am using (note that I copied all of ncover and nunit and my test assemblies into one directory to simplify things)

NCover.Console.exe nunit-console-x86.exe /framework=4.0.30319 Your.Test.Assembly.dll //x coverage.xml //reg

David Gardiner
  • 16,892
  • 20
  • 80
  • 117
Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • could you please show the whole command line you are using? I am trying to follow this but fail to get ncover 1.5.8 to profile my 4.0 code. I decided to ditch Gallio and go for simply invoking the ncover.console from command line and have it run the my nunit tests with coverage... but i can't get nowhere. I spent probably about six hours on this already and it's terribly frustrating... – Peter Perháč Feb 09 '11 at 11:51
  • thanks, will try again really hard. i can't believe how difficult this is to setup... – Peter Perháč Feb 09 '11 at 12:04
  • @Peter, what OS are you running on? Perhaps there is one step I did but I might have missed out. I think I also forced ncover to execute as a 32bit app, rather than letting the OS choose (its compiled as AnyCPU), this was achieved by executing this at the command line: `CorFlags NCover.Console.exe /32BIT+`. I also registered the `CoverLib.dll` file in the NCover folder with regsvr32, although the //reg option should take care of that – Sam Holder Feb 09 '11 at 13:43
  • oh, looks like there's *a lot* more config than I expected... I decided to let this slide and have someone else play with it when they decide it's absolutely necessary to get this working. I started off with much hope that I could get coverage reports from command line but after having spent so much time with it and getting nowhere, i am giving it up. – Peter Perháč Feb 09 '11 at 15:16
  • 1
    @Peter, I'll still be here when you/they want to try again :) – Sam Holder Feb 09 '11 at 15:59
  • @Sam, It looks like I'm in the same situation as Peter. I'm using NUnit 2.5.9 & NCover 1.5.8 on a 32bit box. Here's what I'm trying to run. set ProfAPI_ProfilerCompatibilitySetting=EnableV2Profiler set COMPLUS_ProfAPI_ProfilerCompatibilitySetting=EnableV2Profiler "C:\Program Files\NCover\NCover.Console.exe" "C:/Program Files/NUnit 2.5.9/bin/net-2.0/nunit-console-x86.exe" "mytests.nunit" /nologo /xml="nunit.xml" /framework=4.0.30319 //w "." //a "MyAssembly" //xml "ncover.xml" //reg My result is a NCover.xml file which just has an empty coverage element. – Mike737 Apr 01 '11 at 04:02
  • 1
    @mike737 have you modified your nunit-console-x86.exe.config to contain the useLegacyV2RuntimeActivationPolicy="true" and added the supportedRuntime version="v4.0.30319" – Sam Holder Apr 01 '11 at 07:54
  • @Sam Yes I have added those elements – Mike737 Apr 04 '11 at 06:18
  • 1
    @Mike737, have you tried registering the `CoverLib.dll` with regsvr32 manually? that could make a difference. Can you post more details of your issue, perhaps in another question which you could post a link to here? – Sam Holder Apr 04 '11 at 11:32
  • @Sam That worked. Not sure why it wouldn't have been registered as it was working fine with .Net 3.5. Much appreciated Sam. – Mike737 Apr 05 '11 at 05:50
  • 1
    @Mike737, great! glad you got it sorted out. Now I feel like the time I spent sorting it out wasn't wasted! – Sam Holder Apr 05 '11 at 07:58
  • An important node with these changes. They do make it work but you must use the x86 console for nunit. I thought I could equally apply this to the nunit-console.exe but this did not work. If anyone has an explination as to why I'd appreciate it. Thanks for figuring this out though. – Craig Suchanec Jul 11 '11 at 16:46
  • 5
    FWIW - i got this to work setting only the COMPLUS_ProfAPI_ProfilerCompatibilitySetting env and making the nunit config change. – russau Jul 21 '11 at 00:16
4

I may have misunderstood your problem, but if you're trying to force NCover to run in the .NET 4.0 runtime you can try to place the supportedRuntime element in its configuration file.

<configuration>
  <startup>
    <supportedRuntime version="v4.0.30319"></supportedRuntime>
  </startup>
</configuration>

This will force .NET 4.0 upon the executable without the need to rebuild it.

João Angelo
  • 56,552
  • 12
  • 145
  • 147
  • thanks. I tried this, but whilst this runs ncover and ncover runs nunit it does not seem to be possible to connect the profiler, and ncover just hangs and I get `Profiled process terminated. Profiler connection not established.` after the nunit tests are run and the logs and coverage file are not updated... – Sam Holder Feb 07 '11 at 14:05
  • I upvoted this as it was along the right track, and was ultimately helpful. the answer was making NUnit run in the .NET 4.0 runtime and also adding the `useLegacyV2RuntimeActivationPolicy="true"` attribute to the `startup` element in the configuration file. See my answer below. thanks for the help though. – Sam Holder Feb 08 '11 at 08:11
  • Thanks for the tips, nocver finally ran but the coverage generated was 0% which is wrong.I am using .net 4.5.2 and nunit 2.6.3 Any ideas ?? – user448070 Oct 16 '14 at 12:42