Code coverage features are only supported
Visual Studio Ultimate.
Resharper
I tried this approach to generate a report in local and also in Azure DevOps build pipeline.
Step 1:
Add this code coverage library (Go To Package) NuGet package for all test package.
Step 2:
I created a javascript file that does the entire heavy lifting of running unit tests with code coverage and convert to report and open the report.
| var commandExists = require('command-exists'); | |
| const execSync = require('child_process').execSync; | |
| const rimraf = require('rimraf'); | |
| function runCoverageReport() { | |
| console.log('started code coverage'); | |
| rimraf.sync('TestResults'); | |
| let result = execSync('dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=./TestResults/'); | |
| console.log(result.toString()); | |
| console.log('finished code coverage'); | |
| } | |
| function generateReport() { | |
| console.log('started report generation'); | |
| let result = execSync('reportgenerator "-reports:./**/TestResults/coverage.cobertura.xml" "-targetdir:./TestResults/CoverageReport/"'); | |
| console.log(result.toString()); | |
| console.log('finished report generation'); | |
| } | |
| function openReport() { | |
| const osPlatform = process.platform; | |
| console.log(`opening report for ${osPlatform}`); | |
| if (osPlatform === 'darwin') { | |
| execSync('open ./TestResults/CoverageReport/index.htm') | |
| } | |
| else if (osPlatform === 'win64' || osPlatform === 'win32') { | |
| execSync('start ./TestResults/CoverageReport/index.htm') | |
| } | |
| } | |
| async function main() { | |
| try { | |
| await commandExists('coverlet'); | |
| console.log('coverlet command found and running coverage report'); | |
| } catch (e) { | |
| console.log('installing coverlet.....'); | |
| execSync('dotnet tool install --global coverlet.console'); | |
| } | |
| runCoverageReport(); | |
| try { | |
| await commandExists('reportgenerator'); | |
| console.log('reportgenerator command found and running report'); | |
| } catch (e) { | |
| console.log('installing report generator.....'); | |
| execSync('dotnet tool install --global dotnet-reportgenerator-globaltool'); | |
| } | |
| generateReport(); | |
| openReport(); | |
| }; | |
| main(); |
Step 3:
You have a couple of options to run this. I always create prefer to create a npm script command in package.json
"scripts" : {
"code:coverage" : "node code-coverage.js"
},
"devDependencies": {
"command-exists": "^1.2.8"
}
Finally
I have tested this both on windows and mac.
One Last Thing
I also integrated this into Azure DevOps build pipeline
For test runner task

Add publish code coverage task and then set the below settings

Happy Coding!