Implement code coverage and show in the pipeline
Code coverage is a metric used to measure the proportion of source code that is executed during automated tests. It provides insight into the effectiveness of test suites by identifying areas of code that lack sufficient test coverage. By ensuring comprehensive code coverage, development teams can increase confidence in their software's reliability, identify potential bugs early in the development process, and ultimately deliver higher-quality products to end users.
While the concept of code coverage is platform-agnostic, its implementation details differ between Azure DevOps and GitHub. These details also differ across development frameworks and programming languages.
Azure DevOps
Let's assume that you have a .NET (C#) project for which you want to implement code coverage by using Azure Pipelines. This would involve the following sequence of tasks:
- Set up test and coverage tools: Use a testing framework that supports code coverage analysis such as NUnit, MSTest, or xUnit for .NET. Integrate a code coverage tool such as Coverlet or OpenCover into your project. These tools can generate code coverage reports in various formats compatible with Azure Pipelines.
- Configure Azure Pipelines: In the Azure DevOps portal, navigate to your Azure DevOps project and select the pipeline where you want to add code coverage. Edit the pipeline configuration and add steps to install dependencies and build your project.
- Add code coverage tasks: Add tasks to the pipeline to run tests with code coverage enabled and generate coverage reports. Use the appropriate command-line arguments for your test runner to enable code coverage analysis and specify the output format for coverage reports.
- Publish code coverage reports: Publish the generated code coverage reports as artifacts or attach them to the pipeline run. Use Azure Pipelines' built-in publishing capabilities or custom tasks to upload coverage reports to Azure DevOps or external services.
Here's a sample pipeline that illustrates this approach:
```yaml
trigger:
- main
pool:
vmImage: "windows-latest"
steps:
- task: UseDotNet@2
inputs:
packageType: "sdk"
version: "8.0.x"
- script: dotnet restore
displayName: "Restore Dependencies"
- script: dotnet build --configuration Release
displayName: "Build Solution"
- script: dotnet test --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
displayName: "Run Tests with Code Coverage"
- task: PublishCodeCoverageResults@2
inputs:
summaryFileLocation: "$(Agent.TempDirectory)/**/*.xml"
failIfCoverageEmpty: true
```
This pipeline generates code coverage reports as part of the build process. This allows you to review and analyze the coverage results in the Azure DevOps portal. To perform more detailed analysis, you could download code coverage artifacts and examine them by using Visual Studio (when using the Visual Studio Test task) or code coverage tool-specific options.
GitHub
To make easier comparison with Azure DevOps-based approach, let's assume again that you have a .NET (C#) project for which you want to implement code coverage by using GitHub Actions workflow. This would involve the following sequence of tasks:
- Set up test and coverage tools: As with Azure DevOps, use a testing framework that supports code coverage analysis. Verify that these tools can generate code coverage reports in the formats compatible with GitHub.
- Create GitHub Actions workflow: On the GitHub repository page, navigate to the .github/workflows directory. Create a new YAML file (for example, ci.yml) to define your workflow configuration. Define triggers for your workflow, such as pull requests or pushes to specific branches.
- Configure workflow steps: Define steps in your workflow YAML to set up the environment, restore dependencies, build your project, run tests, and generate code coverage reports. Ensure that your project's source code, test configuration, and necessary dependencies are included in the repository. Use actions such as actions/checkout to check out your repository, actions/setup-dotnet to set up .NET Core SDK, and coverallsapp/github-action or codecov/codecov-action to upload code coverage reports.
Here's a sample workflow that illustrates this approach:
```yaml
name: Code Coverage
on:
pull_request:
branches:
- main
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: "8.0.x"
- name: Restore Dependencies
run: dotnet restore
- name: Build Solution
run: dotnet build --configuration Release
- name: Run Tests
run: dotnet test --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
- name: Upload Code Coverage Report
uses: coverallsapp/github-action@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
```
This GitHub Actions workflow will automatically run tests and generate code coverage reports whenever a pull request is created or code is pushed to the main branch. Make sure you have the necessary permissions and API tokens to upload code coverage reports to third-party services like Coveralls or Codecov.