Nirman
Nirman

Reputation: 6783

Unable to generate code coverage report using ReportGenerator

I have created a build pipeline in Azure DevOps for one of my ASP.NET MVC application. There exist projects for unit testing, and I need to generate code coverage report, for which I have used coverlet.msbuild NuGet package, and "ReportGenerator".

Following is the packages.config file of one of the unit test project:

<packages>
  <package id="coverlet.msbuild" version="2.8.0" targetFramework="net461" developmentDependency="true" />
  <package id="NUnit" version="2.6.3" targetFramework="net45" />
  <package id="ReportGenerator" version="4.4.6" targetFramework="net461" />
</packages>

Also, please find the yaml of Build solution, test assemblies, and ReportGenerator tasks in build pipeline:

Build Solution:

steps:
- task: VSBuild@1
  displayName: 'Build solution **\SmartStoreNET.sln'
  inputs:
    solution: '**\SmartStoreNET.sln'
    msbuildArgs: '/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura'

Test Assemblies

steps:
- task: VSTest@2
  displayName: 'Test Assemblies'
  inputs:
    testAssemblyVer2: |
     **\$(BuildConfiguration)\*test*.dll
     !**\obj\**
    codeCoverageEnabled: true
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfiguration)'

ReportGenerator

steps:
- task: Palmmedia.reportgenerator.reportgenerator-build-release-task.reportgenerator@4
  displayName: ReportGenerator
  inputs:
    reports: '$(Build.SourcesDirectory)/tests/**/coverage.cobertura.xml'
    targetdir: '$(Build.SourcesDirectory)/CodeCoverage'
    reporttypes: 'HtmlInline_AzurePipelines;Cobertura;Badges'

While executing the pipeline I am getting following error in ReportGenerator task:

The report file pattern 'd:\a\1\s/tests/**/coverage.cobertura.xml' is invalid. No matching files found.

Can anyone please suggest what is missing here, or what could be the potential issue.

Any help on this would be much appreciated.

Thanks,

Nirman

Upvotes: 1

Views: 9583

Answers (1)

Leo Liu
Leo Liu

Reputation: 76760

Unable to generate code coverage report using ReportGenerator

AFAIK, the properties /p:CollectCoverage=true and /p:CoverletOutputFormat=cobertura are used for the test task to generate coverage results not for the build task.

But there is an issue for getting coverlet to run using the Visual Studio Test task, so that we could not use above properties for the VS test task directly.

As workaround, you could try to install the tool during the pipeline and then generate the report with powershell scripts:

dotnet tool install dotnet-reportgenerator --tool-path . --version 4.0.12
dotnet tool install coverlet.console --tool-path . --version 1.4.1
mkdir .\reports
$unitTestFile = gci -Recurse | ?{ $_.FullName -like "*bin\*test*.dll" }
$coverlet = "$pwd\coverlet.exe"
& $coverlet $unitTestFile.FullName --target "dotnet" --targetargs "vstest $($unitTestFile.FullName) --logger:trx" --format "cobertura"
gci -Recurse |
?{ $_.Name -eq "coverage.cobertura.xml"} |
%{ &"$pwd\reportgenerator.exe" "-reports:$($_.FullName)" "-targetdir:reports" "-reportstypes:HTMLInline;HTMLChart" }

Check the this thread and the document for some more details.

Hope this helps.

Upvotes: 3

Related Questions