Skip to content

Instantly share code, notes, and snippets.

@giridharprakash
Created December 20, 2019 03:22
Show Gist options
  • Save giridharprakash/5664f7c3ddcfab265678e863ad0b4e1f to your computer and use it in GitHub Desktop.
Save giridharprakash/5664f7c3ddcfab265678e863ad0b4e1f to your computer and use it in GitHub Desktop.
Code coverage script for .net core projects
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment