Implementing Code coverage in Azure DevOps and GitHub Actions
Code coverage is a crucial metric to ensure test completeness. Here’s how to implement it in Azure DevOps and GitHub Actions.
1. Azure DevOps Implementation
Set Up Test and Coverage Tools
Choose the appropriate tools based on your language:
.NET:
Coverlet, integrated withdotnet test.Java:
JaCoCo.Python:
pytest-cov.JavaScript/TypeScript:
nyc(Istanbul).
Configure Azure Pipelines
Define your pipeline using YAML and ensure test and coverage tools are included.
Example Pipeline Configuration for .NET:
xxxxxxxxxx231trigger2main3pool4 vmImage'windows-latest'5variables6 buildConfiguration'Release'7stages8stageBuild9 displayName"Build and Test"10 jobs11jobBuildAndTest12 displayName"Build and Run Tests"13 steps14taskUseDotNet@215 inputs16 packageTypesdk17 version'6.x'18scriptdotnet restore19 displayName"Restore NuGet Packages"20scriptdotnet build --configuration $(buildConfiguration)21 displayName"Build Solution"22scriptdotnet test --configuration $(buildConfiguration) --collect"Code Coverage" --results-directory $(System.DefaultWorkingDirectory)/TestResults23 displayName"Run Tests with Coverage"Add Code Coverage Tasks
Publish code coverage results to Azure DevOps for visualization.
Example (JaCoCo for Java):
xxxxxxxxxx51taskPublishCodeCoverageResults@12 inputs3 codeCoverageTool'JaCoCo'4 summaryFileLocation'$(System.DefaultWorkingDirectory)/TestResults/Coverage/coverage.xml'5 reportDirectory'$(System.DefaultWorkingDirectory)/TestResults/CoverageReport'Example (Coverlet for .NET):
xxxxxxxxxx41taskPublishCodeCoverageResults@12 inputs3 codeCoverageTool'cobertura'4 summaryFileLocation'$(System.DefaultWorkingDirectory)/TestResults/coverage.cobertura.xml'Publish Code Coverage Reports
Ensure test results and coverage are published to the pipeline summary.
xxxxxxxxxx51taskPublishTestResults@22 inputs3 testResultsFormat'TRX'4 testResultsFiles'**/.trx'5 mergeTestResultstrue2. GitHub Actions Implementation
Set Up Test and Coverage Tools
Install and configure tools for your language in the actions workflow:
.NET:
Coverlet.Java:
JaCoCo.Python:
pytest-cov.JavaScript/TypeScript:
nyc(Istanbul).
Create GitHub Actions Workflow
Define the workflow in a .github/workflows/ci.yml file.
Configure Workflow Steps
Example for a .NET application:
xxxxxxxxxx291nameCI2on3 push4 branches5main6 pull_request7 branches8main9jobs10 build-and-test11 runs-onubuntu-latest12 steps13nameCheckout code14 usesactions/checkout@v315nameSetup .NET16 usesactions/setup-dotnet@v317 with18 dotnet-version6.019nameInstall dependencies20 rundotnet restore21nameBuild22 rundotnet build --configuration Release23nameRun Tests with Code Coverage24 rundotnet test --configuration Release --collect"Code Coverage" --results-directory ./TestResults25nameUpload Coverage Results26 usesactions/upload-artifact@v327 with28 namecode-coverage29 path./TestResultsAdd Coverage Report Publishing
Use a tool like or to publish the coverage report.
Example for Codecov:
Add Codecov to your workflow:
xxxxxxxxxx51nameUpload to Codecov2usescodecov/codecov-action@v33with4files./TestResults/coverage.cobertura.xml5token$ secrets.CODECOV_TOKENRetrieve the token from the Codecov dashboard for your repository and store it as a GitHub secret.
Summary
| Feature | Azure DevOps | GitHub Actions |
|---|---|---|
| Code Coverage Tool | Coverlet, JaCoCo, pytest-cov, nyc | Coverlet, JaCoCo, pytest-cov, nyc |
| Test Execution | dotnet test, pytest, npm test | dotnet test, pytest, npm test |
| Coverage Report | PublishCodeCoverageResults Task | Codecov/Custom report integration |
| Result Visualization | Azure DevOps Pipeline Summary | Codecov dashboard or GitHub Artifacts |






















Leave a Reply