Skip to content
Latchkey

Code Coverage workflow (tenpy/tenpy)

The Code Coverage workflow from tenpy/tenpy, explained and optimized by Latchkey.

F

CI health: F - at risk

Point runs-on at Latchkey and get caching, run de-duplication, job timeouts, SHA-pinned actions, self-healing for flaky steps, and up to 58% lower cost, applied automatically.

Grade your own workflow free or run it on Latchkey →
Source: tenpy/tenpy.github/workflows/code-coverage.ymlLicense Apache-2.0View source

What it does

This is the Code Coverage workflow from the tenpy/tenpy repository, a real project running GitHub Actions. It is shown here with attribution under its Apache-2.0 license.

Below, Latchkey shows a faster, safer version produced by its optimization engine.

The workflow

workflow (.yml)
name: Code Coverage
# Run pytest with code coverage and make a badge for the README from coverage percentage

# Note: The pytest.yml workflow runs pytest *without* coverage.
#       This workflow is supposed to run tests for the most recent python version.
#       That version can then be omitted in pytest.yml.

on:
  # pushes to main
  push:
    branches:
      - main

jobs:
  build:
    if: github.event.pull_request.draft == false

    runs-on: ubuntu-latest
    env:
      # make pytest output in color
      PY_COLORS: 1
    steps:

    - name: Checkout
      uses: actions/checkout@v5

    - name: Set up Python
      uses: actions/setup-python@v6
      with:
        python-version: "3.13"

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        python -m pip install --upgrade setuptools build
        python -m pip install --upgrade pytest
        python -m pip install --upgrade coverage

    - name: Build and install tenpy
      # also installs extra dependencies defined in pyproject.toml
      run: |
        python -m build .
        python -m pip install ".[test]"

    - name: Show tenpy config
      working-directory: ..
      run: |
        python -c "import tenpy; tenpy.show_config()"

    - name: Run pytest with coverage
      # pytest configuration in pyproject.toml
      # Note: This runs in the repo root directory, which contains the uncompiled tenpy package.
      #       To use the version we just installed, it is important to run `coverage`
      #       instead of `python -m coverage`.
      run: |
        coverage run -m pytest .
        coverage report -m --skip-covered --sort=miss
        coverage json
      # Note: to get a nicely rendered html page, you can run the following on your local machine:
      #         python -m coverage run -m pytest
      #         python -m coverage html
      #       It will write to htmlcov.
      #       We choose not to provide this html report from the workflow, because it generates
      #       quite large files.

    - name: Set environment variable for badge
      # note: this cuts off digits instead of rounding
      run: echo "COV_PERCENT=$(jq .totals.percent_covered coverage.json | xargs printf "%.0f")" >> $GITHUB_ENV

    - name: Archive code coverage results
      uses: actions/upload-artifact@v4
      with:
        name: code-coverage-report
        path: coverage.json
        if-no-files-found: error

    - name: Create badge
      # Only on pushes to the main branch
      if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
      uses: schneegans/dynamic-badges-action@v1.7.0
      with:
        auth: ${{ secrets.JAKOB_UNFRIED_GIST_TOKEN }}
        gistID: 9e2e197d6a2e6e2c9440b2c0eda04d5c
        filename: tenpy_coverage_badge.json
        label: Code Coverage
        message: ${{ env.COV_PERCENT }}%
        minColorRange: 50
        maxColorRange: 100
        valColorRange: ${{ env.COV_PERCENT }}

The same workflow, on Latchkey

Estimated ~20% faster on cache hits, plus fewer wasted runs and a safer supply chain. Added and changed lines are highlighted.

name: Code Coverage
# Run pytest with code coverage and make a badge for the README from coverage percentage
 
# Note: The pytest.yml workflow runs pytest *without* coverage.
#       This workflow is supposed to run tests for the most recent python version.
#       That version can then be omitted in pytest.yml.
 
on:
  # pushes to main
  push:
    branches:
      - main
 
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
 
jobs:
  build:
    timeout-minutes: 30
    if: github.event.pull_request.draft == false
 
    runs-on: latchkey-small
    env:
      # make pytest output in color
      PY_COLORS: 1
    steps:
 
    - name: Checkout
      uses: actions/checkout@v5
 
    - name: Set up Python
      uses: actions/setup-python@v6
      with:
        cache: 'pip'
        python-version: "3.13"
 
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        python -m pip install --upgrade setuptools build
        python -m pip install --upgrade pytest
        python -m pip install --upgrade coverage
 
    - name: Build and install tenpy
      # also installs extra dependencies defined in pyproject.toml
      run: |
        python -m build .
        python -m pip install ".[test]"
 
    - name: Show tenpy config
      working-directory: ..
      run: |
        python -c "import tenpy; tenpy.show_config()"
 
    - name: Run pytest with coverage
      # pytest configuration in pyproject.toml
      # Note: This runs in the repo root directory, which contains the uncompiled tenpy package.
      #       To use the version we just installed, it is important to run `coverage`
      #       instead of `python -m coverage`.
      run: |
        coverage run -m pytest .
        coverage report -m --skip-covered --sort=miss
        coverage json
      # Note: to get a nicely rendered html page, you can run the following on your local machine:
      #         python -m coverage run -m pytest
      #         python -m coverage html
      #       It will write to htmlcov.
      #       We choose not to provide this html report from the workflow, because it generates
      #       quite large files.
 
    - name: Set environment variable for badge
      # note: this cuts off digits instead of rounding
      run: echo "COV_PERCENT=$(jq .totals.percent_covered coverage.json | xargs printf "%.0f")" >> $GITHUB_ENV
 
    - name: Archive code coverage results
      uses: actions/upload-artifact@v4
      with:
        name: code-coverage-report
        path: coverage.json
        if-no-files-found: error
 
    - name: Create badge
      # Only on pushes to the main branch
      if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
      uses: schneegans/dynamic-badges-action@v1.7.0
      with:
        auth: ${{ secrets.JAKOB_UNFRIED_GIST_TOKEN }}
        gistID: 9e2e197d6a2e6e2c9440b2c0eda04d5c
        filename: tenpy_coverage_badge.json
        label: Code Coverage
        message: ${{ env.COV_PERCENT }}%
        minColorRange: 50
        maxColorRange: 100
        valColorRange: ${{ env.COV_PERCENT }}
 

What changed

1 third-party action is referenced by a movable tag. Pin it to the commit SHA (Latchkey resolves and applies this automatically) so a repointed tag cannot change what runs.

What Latchkey heals here

This workflow has steps that commonly fail on transient issues (network, registries, flaky browsers). On Latchkey managed runners they are detected, retried, and self-healed instead of failing your build:

This workflow runs 1 job per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.

Actions used in this workflow