Implement code coverage and show in the pipeline

Completed

Code coverage measures the percentage of source code executed during automated tests. This metric helps teams understand test effectiveness, identify untested code areas, and improve software quality. Implementing code coverage in your CI/CD pipeline provides visibility into testing gaps and helps maintain high-quality standards.

Implementation varies across platforms and programming languages, but the core principles remain consistent. Let's explore how to implement code coverage in Azure Pipelines and display results effectively.

Implementing code coverage in Azure Pipelines

For a .NET (C#) project, implementing code coverage involves several key steps:

  1. 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.
  2. 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.
  3. 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.
  4. 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.

Implementing code coverage with GitHub Actions

For comparison, here's how to implement code coverage for a .NET (C#) project using GitHub Actions:

  1. 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.
  2. 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.
  3. 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.