Code Coverage–part 25
Install coverlet and report generator
dotnet tool install coverlet.console
dotnet tool install dotnet-reportgenerator-globaltool
Then
dotnet coverlet Infovalutar.sln –target “dotnet” –targetargs “test InfovalutarTest\InfovalutarTest.csproj –configuration release –no-build” –format opencover –exclude “[xunit*]*”
dotnet reportgenerator “-reports:coverage.opencover.xml” “-targetdir:coveragereport” “-reporttypes:HTMLInline;HTMLSummary;Badges”
And the result ?No assemblies have been covered.
Updating all NuGet packages for the solution – including a collector for coverlet.
Reading more, discover it works with
dotnet test –collect:”XPlat Code Coverage” -r resultsFolder
The problem is that generates a GUID inside resultsFolder … and reportGenerator cannot be pointed to a random folder
Playing with different options, such as
dotnet test /p:CollectCoverage=true /p:CoverletOutput=’results’ /p:CoverletOutputFormat=json -v n
No results in the results directory…
Found obscure reference https://github.com/tonerdo/coverlet/issues/201 that I need to add package coverlet.msbuild
Now
dotnet test –configuration release –no-build /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura -v m /p:CoverletOutput=cob.xml
dotnet reportgenerator “-reports:InfovalutarTest/cob.xml” “-targetdir:coveragereport” “-reporttypes:HTMLInline;HTMLSummary;Badges”
And generates report.
And because I want those to be added in AzureDevOps at test I add –logger trx and https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/test/publish-test-results?view=azure-devops&tabs=yaml
– task: PublishTestResults@2
inputs:
testRunner: VSTest
testResultsFiles: ‘**/*.trx’
– task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: cobertura
summaryFileLocation: ‘**/cob.xml’
It works! Last: going to shields.io to have a display of the tests
![Azure DevOps tests (branch)](https://img.shields.io/azure-devops/tests/ignatandrei0674/InfoValutar/5/master)
![Azure DevOps cc](https://img.shields.io/azure-devops/coverage/ignatandrei0674/InfoValutar/5/master)
Infovalutar
And one hour passes...(This is the result of 1 hour per day auto-challenge as a full cycle developer for an exchange rates application)
( You can see the sources at https://github.com/ignatandrei/InfoValutar/ )
Leave a Reply