Automatically Publish Test Results to Pull Requests Using GitHub Actions

In this guide, you’ll learn to set up a GitHub Action workflow that ensures that every pull request is tested before merging. Whenever a pull (PR) request is raised, your unit tests will be automatically ran and the results published to the as a comment on the PR. Plus, I’ll show you how to prevent merging unless all tests pass.

Screenshot of a pull request with failing tests


Some quick notes. This will guide is written for a .NET application. However, it can be easily adapted for any language or tech stack. A video walkthrough of this post can be found here.


Create the GitHub Actions Workflow

To set up the workflow:

  1. In the root of your repository, create a .github/workflows directory.
  2. Add a new file named run-unit-tests-on-pr.yml. This file defines the GitHub Actions workflow.

Add the following to your YAML file:

name: Run tests and publish results on PRs

on:
  pull_request:
    branches:
      - main

permissions:
  contents: read
  checks: write
  pull-requests: write

jobs:
  run-tests:
    name: Run Tests
    runs-on: ubuntu-latest

    steps:
      - name: Check out code
        uses: actions/checkout@v4

      - name: Set up .NET
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: '9.x'

      - name: Restore dependencies
        run: dotnet restore

      - name: Run tests
        run: |
          mkdir -p test-results
          dotnet test --logger "trx;LogFileName=test-results.trx" --results-directory ./test-results
        continue-on-error: true

      - name: Publish test results
        uses: EnricoMi/publish-unit-test-result-action@v2
        if: always()
        with:
          files: test-results/test-results.trx

Explanation

  1. Trigger: The workflow is set to trigger when a pull requests is raised into the main branch.
  2. Permissions: The workflow is granted permissions to read the contents of you repository, write to pull request (allowing it to comment with the results), and to write statuses.
  3. Job Setup: The workflow runs on ubuntu-latest for faster performance.
  4. Steps:
    • Check out the code.
    • Set up .NET
    • Restore project dependencies with dotnet restore.
    • Run tests, saving the results in a test-results directory using the trx logger.
    • Publish test results as a comment on the PR using the publish-unit-test-result-action GitHub Action.

Test the Workflow

Create a pull request to test the workflow:

  1. Push changes to a new branch.
  2. Open a pull request targeting main.
  3. Watch as the workflow automatically triggers.

Test Results:

  • If all tests pass, the workflow should post a comment showing successful results and marks the check as passed.
  • If any tests fail, the workflow should post a comment indicating the failures, along with a link to detailed results. The pull request check fails as well.

Enforce Test Passing with Branch Protection Rules

To prevent broken pull requests from being merged:

  1. Navigate to Settings in your repository.
  2. Under Branches, click Add branch ruleset.
  3. Configure the rules:
    • Name the ruleset.
    • Set enforcement status to Active.
    • Target the default branch (e.g., main).
    • Require status checks to pass before merging.
    • Add a rule to require a pull request before merging.
  4. Click Create.

Now, pull requests with failing tests can’t be merged until the issues are resolved.


Wrapping Up

You’ve now automated your tests and enforced quality control in your development workflow. With GitHub Actions handling test runs and branch protection rules preventing merges of failing code, you’ve set your team up for success.

If you found this guide helpful, be sure to implement it in your projects today. Happy coding!

Comments are closed