Skip to content
Latchkey

Generate PyTest Coverage Badge workflow (Azure/counterfit)

The Generate PyTest Coverage Badge workflow from Azure/counterfit, explained and optimized by Latchkey.

C

CI health: C - fair

Point runs-on at Latchkey and get 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: Azure/counterfit.github/workflows/testcoverage.yamlLicense MITView source

What it does

This is the Generate PyTest Coverage Badge workflow from the Azure/counterfit repository, a real project running GitHub Actions. It is shown here with attribution under its MIT license.

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

The workflow

workflow (.yml)
---
name: Generate PyTest Coverage Badge
on:
  pull_request:
    types:
      - closed

  push:
    branches: [main]

  # Run at 4am everyday (see https://crontab.guru)
  schedule:
    - cron: "0 4 * * *"

env:
  PYTHON_VERSION: 3.8.0
  SECONDS: 120

jobs:
  coverage_if_merged:
    if: github.event.pull_request.merged == true || github.event.pull_request == false
    runs-on: ubuntu-latest
    steps:
      - name: Install OS dependencies
        run: |
          sudo apt update
          sudo apt install -y expect \
            python3-opengl \
            xvfb
          echo 'export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python' >> "${HOME}/.bashrc"

      - name: Setup Anaconda
        uses: conda-incubator/setup-miniconda@v2
        with:
          activate-environment: counterfit
          auto-update-conda: true
          python-version: ${{ env.PYTHON_VERSION }}

      - name: Setup Conda Environment
        run: |
          echo 'conda activate "counterfit"' >> "${HOME}/.profile"

      - name: Checkout Counterfit repository
        uses: actions/checkout@v3
        with:
          ref: ${{ github.head_ref }}
          fetch-depth: 0

      - name: Populate installation acceptance test
        run: |
          cp "${GITHUB_WORKSPACE}/.github/helpers/quality-gates/install_test.exp" ./install_test.exp

      - name: Setup Counterfit
        shell: bash -l {0}
        run: |
          source "${HOME}/.profile"
          cd "${GITHUB_WORKSPACE}/"
          pip install --no-input .[dev]
          python -c "import nltk;  nltk.download('stopwords')"

      - name: Test Counterfit installation
        shell: bash -l {0}
        run: |
          source "${HOME}/.profile"
          expect -d ./install_test.exp Linux ${{ env.SECONDS }} "${GITHUB_WORKSPACE}/examples/terminal/terminal.py"

      - name: Generate coverage report
        run: |
          source "${HOME}/.profile"
          cd "${GITHUB_WORKSPACE}/"
          coverage run -m pytest
          coverage json

      - name: Get Coverage Percentage
        id: coverage_percent
        uses: notiz-dev/github-action-json-property@release
        with: 
            path: 'coverage.json'
            prop_path: 'totals.percent_covered_display'
      - run: echo ${{ steps.coverage_percent.outputs.prop }}

      - name: Create Coverage Badge
        uses: schneegans/dynamic-badges-action@v1.6.0
        with:
          auth: ${{ secrets.COUNTERFITGISTTOKEN }}
          gistID: 45c1f54d01942c69453aac7f60401dc4
          filename: counterfit_test_coverage_badge.json
          label: Coverage
          message: ${{ steps.coverage_percent.outputs.prop }}%
          valColorRange: ${{ steps.coverage_percent.outputs.prop }}
          maxColorRange: 100
          minColorRange: 0

The same workflow, on Latchkey

Removes redundant runs and caps runaway jobs. Added and changed lines are highlighted.

---
name: Generate PyTest Coverage Badge
on:
  pull_request:
    types:
      - closed
 
  push:
    branches: [main]
 
  # Run at 4am everyday (see https://crontab.guru)
  schedule:
    - cron: "0 4 * * *"
 
env:
  PYTHON_VERSION: 3.8.0
  SECONDS: 120
 
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
 
jobs:
  coverage_if_merged:
    timeout-minutes: 30
    if: github.event.pull_request.merged == true || github.event.pull_request == false
    runs-on: latchkey-small
    steps:
      - name: Install OS dependencies
        run: |
          sudo apt update
          sudo apt install -y expect \
            python3-opengl \
            xvfb
          echo 'export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python' >> "${HOME}/.bashrc"
 
      - name: Setup Anaconda
        uses: conda-incubator/setup-miniconda@v2
        with:
          activate-environment: counterfit
          auto-update-conda: true
          python-version: ${{ env.PYTHON_VERSION }}
 
      - name: Setup Conda Environment
        run: |
          echo 'conda activate "counterfit"' >> "${HOME}/.profile"
 
      - name: Checkout Counterfit repository
        uses: actions/checkout@v3
        with:
          ref: ${{ github.head_ref }}
          fetch-depth: 0
 
      - name: Populate installation acceptance test
        run: |
          cp "${GITHUB_WORKSPACE}/.github/helpers/quality-gates/install_test.exp" ./install_test.exp
 
      - name: Setup Counterfit
        shell: bash -l {0}
        run: |
          source "${HOME}/.profile"
          cd "${GITHUB_WORKSPACE}/"
          pip install --no-input .[dev]
          python -c "import nltk;  nltk.download('stopwords')"
 
      - name: Test Counterfit installation
        shell: bash -l {0}
        run: |
          source "${HOME}/.profile"
          expect -d ./install_test.exp Linux ${{ env.SECONDS }} "${GITHUB_WORKSPACE}/examples/terminal/terminal.py"
 
      - name: Generate coverage report
        run: |
          source "${HOME}/.profile"
          cd "${GITHUB_WORKSPACE}/"
          coverage run -m pytest
          coverage json
 
      - name: Get Coverage Percentage
        id: coverage_percent
        uses: notiz-dev/github-action-json-property@release
        with: 
            path: 'coverage.json'
            prop_path: 'totals.percent_covered_display'
      - run: echo ${{ steps.coverage_percent.outputs.prop }}
 
      - name: Create Coverage Badge
        uses: schneegans/dynamic-badges-action@v1.6.0
        with:
          auth: ${{ secrets.COUNTERFITGISTTOKEN }}
          gistID: 45c1f54d01942c69453aac7f60401dc4
          filename: counterfit_test_coverage_badge.json
          label: Coverage
          message: ${{ steps.coverage_percent.outputs.prop }}%
          valColorRange: ${{ steps.coverage_percent.outputs.prop }}
          maxColorRange: 100
          minColorRange: 0

What changed

3 third-party actions are referenced by a movable tag. Pin them 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