3

I'm trying to generate a code coverage report using the build pipeline for the C# unit tests (MSTest2). Report can be generated using the Reportgenerator.exe but expects .xml file as the input. I have added the Visual Studio test task which has generated a .coverage file in the build artifact. We could use CodeCoverage.exe to convert .coverage to .xml file.

To test this locally, I have copied the .coverage file and on running:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Team Tools\Dynamic Code Coverage Tools\amd64>CodeCoverage collect /IIS /session:WebSession /output:'C:\CoverageFiles\test.coverage'

and

C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Team Tools\Dynamic Code Coverage Tools\amd64>CodeCoverage analyze /output:'c:\CoverageFiles\results.xml' 'c:\CoverageFiles\test.coverage'

the script is not throwing any error and xml file is also not generated.

Is there any other way to generate the .xml file from .coverage file? Any help on this is appreciated.

Dev
  • 1,451
  • 20
  • 30
  • 1
    Can you show us what you've done so far? – Roman Marusyk May 11 '19 at 20:36
  • @RomanMarusyk Trying to run this locally using C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Team Tools\Dynamic Code Coverage Tools\amd64> '.\CodeCoverage.exe' collect /output 'c:\CoverageFiles\test.coverage' and it is erroring – Dev May 11 '19 at 21:33
  • 1
    Ok, what errors? Should we guess what error you got? – Roman Marusyk May 11 '19 at 21:40
  • @RomanMarusyk Modified to C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Team Tools\Dynamic Code Coverage Tools\amd64>CodeCoverage collect /IIS /session:WebSession /output:'C:\CoverageFiles\test.coverage' C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Team Tools\Dynamic Code Coverage Tools\amd64>CodeCoverage analyze /output:'c:\CoverageFiles\results.xml' 'c:\CoverageFiles\test.coverage' – Dev May 11 '19 at 22:07
  • Now no errors but I do not see .xml file either in that location. When I open the same coverage file in VS, it can view the Code coverage report. – Dev May 11 '19 at 22:07
  • dev edit you post and put the code you tried and the errors please. – ArcSet May 12 '19 at 14:33
  • I found [this](https://github.com/danielpalme/ReportGenerator/wiki/Visual-Studio-Coverage-Tools) on Github, but it is in C# – Theo May 12 '19 at 14:42

1 Answers1

1

You can use coverlet to create a coverage report in different format. Coverlet will be invoked by msbuild. You can check my azure-pipelines.yml, where I use coverlet in combination with reporgenerator to create the code coverage in the build pipeline.

Parts of the yml:

- script: |
    echo $(Build.SourcesDirectory)
    cd src\Mwd.Exceptions.Solution
    mkdir $(Build.SourcesDirectory)\results
    dotnet test --logger trx /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura  
    copy Mwd.Exceptions.Test\coverage.cobertura.xml $(Build.SourcesDirectory)\results
    dotnet tool install dotnet-reportgenerator-globaltool --tool-path . --version 4.0.0-rc4
    .\reportgenerator "-reports:$(Build.SourcesDirectory)\results\coverage.cobertura.xml" "-targetdir:$(Build.SourcesDirectory)\results" "-reporttypes:HTMLInline;HTMLChart"

dotnet test --logger trx /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura creates the coverage in cobertura format, at the time the only format build pipelines are supporting.

.\reportgenerator "-reports:$(Build.SourcesDirectory)\results\coverage.cobertura.xml" "-targetdir:$(Build.SourcesDirectory)\results" "-reporttypes:HTMLInline;HTMLChart" generate the report.

Since coverlet adds build targets to msbuild, compiling your solution(s) via devenv.exe should also be supported.

Moerwald
  • 10,448
  • 9
  • 43
  • 83
  • I believe coverlet is used for .net core project and not the C# unit tests written using MSTest2? – Dev May 12 '19 at 16:27
  • At least there is a [fix](https://github.com/tonerdo/coverlet/issues/358) regarding .net 4.6.1, maye its already included in the latest release. There is also this [GitHub repo](https://github.com/tonerdo/coverlet/issues/358) using mstest-tests. – Moerwald May 12 '19 at 16:36