Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCover does not cover under TeamCity

I have a project that does NOT give me any coverage in the output coverage file when running under TeamCity. When I run from the command line it covers fine. Apparently there is some permission problem with the local system account that is used when TeamCity runs, ie if I change the TeamCity runner to use one of the logins I use to accesss the machine it gives coverage. However I don't want to maintain the password of that user in the TeamCity service.

I'm using the -register:user param when kicking OpenCover off in the nant script, that doesn't help.

What is the recommended setup to get the coverage working when running under TeamCity ?

like image 722
Orn Kristjansson Avatar asked Jan 15 '23 18:01

Orn Kristjansson


2 Answers

Preregister OpenCover.Profiler.dll using regsvr32 manually. But to execute this you should run cmd with admin rights.

like image 69
Pavel Bakshy Avatar answered Jan 31 '23 07:01

Pavel Bakshy


I recently set this up with MSBuild and it is working fine. Here are the relevant parts of my MSBuild script:

<Project DefaultTargets="Build;Test;Archive;" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <UsingTask AssemblyFile=
        "..\..\tools\MSBuildCommunityTasks\MSBuild.Community.Tasks.dll"
        TaskName="MSBuild.Community.Tasks.Xslt" />

  <PropertyGroup>
    <!--Default Configuration-->
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>

    <!--Default Platform-->
    <Platform Condition=" '$(Platform)' == '' ">"Any CPU"</Platform>

    <!--Test assemblies-->
    <TestAssemblies>HelloWorld.Mvc3UI.UnitTests\bin\$(Configuration)\HelloWorld.Mvc3UI.UnitTests.dll</TestAssemblies>

  </PropertyGroup>

  <Target Name="Build">
    <MSBuild Targets="Clean;Rebuild" Projects="HelloWorld.sln"  ContinueOnError="false"/>
  </Target>

    <Target Name="Test">

        <!-- Check Code Coverage -->
        <Exec Command="..\..\tools\OpenCover.4.0.519\OpenCover.Console.exe -register:user -target:..\..\tools\NUnit\nunit-console.exe -targetargs:&quot;$(TestAssemblies) /xml=NUnitReport.xml /noshadow&quot; -filter:&quot;+[HelloWorld.Mvc3UI*]* -[HelloWorld.Mvc3UI.UnitTests*]*&quot; -output:OpenCoverReport.xml"></Exec>
        <!-- Format Code Coverage Report -->
        <Exec Command="..\..\tools\ReportGenerator.1.5.0.0\ReportGenerator.exe OpenCoverReport.xml OpenCoverReport"></Exec>

        <Xslt Inputs="NUnitReport.xml"
            Xsl="..\..\tools\NUnit\NUnitReport.xsl"
            Output="NUnitReport.html" />
    </Target>

    <Target Name="Archive">
        <Exec Command="..\..\tools\7-Zip.9.20\7za.exe a build.zip &quot;HelloWorld.Mvc3UI\*&quot;"></Exec>
    </Target>

</Project>

The key is to use the /noshadow option for NUnit and to use the -register switch for OpenCover (it requires COM registration to run, but this switch does a temporary registration for the TeamCity user account).

To prevent OpenCover from hanging sometimes, it also helps to disable .NET 2.0 in the nunit-console.exe.config file as a supported runtime as outlined here:

  <startup useLegacyV2RuntimeActivationPolicy="true">
    <!-- Comment out the next line to force use of .NET 4.0 -->
    <!-- <supportedRuntime version="v2.0.50727" /> -->
    <supportedRuntime version="v4.0.30319" />
  </startup>
like image 40
NightOwl888 Avatar answered Jan 31 '23 08:01

NightOwl888