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.

1 Answers

0
Moerwald On

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.