2 min read

Running Pester Tests in GitHub Actions

GitHub Actions is a powerful CI/CD platform that integrates tightly with your code repositories. Since PowerShell Core (pwsh) is available on all GitHub-hosted runners (Ubuntu, Windows, macOS), running Pester tests is straightforward.

Parent Topic: Pester CI/CD Integration Related: Pester Testing in PowerShell

1. The Workflow File

Create a file in your repository at .github/workflows/pester.yml.

Basic Workflow

This workflow runs on every push to main and executes the Build.ps1 script created in the Pester CI/CD Integration guide.

name: Pester Tests

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  test:
    name: Run Pester on Ubuntu
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Run Pester Tests
        shell: pwsh
        run: |
          # Install Pester if needed (GitHub runners usually have it, but force latest is safer)
          Install-Module Pester -Force -SkipPublisherCheck -Scope CurrentUser

          # Run the build script that generates NUnit XML
          ./Build.ps1

      - name: Upload Test Results Artifact
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: Pester-Results
          path: Artifacts/

2. Visualizing Results (Test Reporting)

By default, GitHub Actions logs show text output. To see a nice summary of passed/failed tests in the "Actions" tab, you can use a third-party action to parse the NUnit XML generated by Pester.

Add this step after running the tests:

      - name: Publish Test Report
        uses: dorny/test-reporter@v1
        if: always() # Run even if tests fail
        with:
          name: Pester Results
          path: Artifacts/TestResults.xml
          reporter: java-junit # Pester's NUnitXml is compatible here

3. Cross-Platform Testing

One of Pester's strengths is cross-platform compatibility. You can run your tests on Windows, Linux, and macOS simultaneously using a matrix strategy.

jobs:
  test:
    name: Test on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]

    steps:
      - uses: actions/checkout@v4

      - name: Run Pester
        shell: pwsh
        run: ./Build.ps1

Return to Pester CI/CD Integration