GitHub Actions Workflow

Add the following job to your .github/workflows/…yaml. It triggers on pushes to the mentioned branch in the yaml file:

  - name: Run tests and generate coverage
    id: generate-test-coverage
    run: |
      source .venv/bin/activate
      uv pip install coverage pytest
      coverage run -m pytest tests/
      coverage xml -o coverage.xml
      cat coverage.xml

  - name: Fetch upload script
    env:
      API_BASE: https://backend.codeant.ai
    run: |
      curl -sS -X GET "${API_BASE}/pr/analysis/coverage/script/get" \
        --output upload_coverage.sh

  - name: Make script executable
    run: chmod +x upload_coverage.sh

  - name: Upload coverage and set status
    env:
      ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
      REPO_NAME:    ${{ github.repository }}
      COMMIT_ID:    ${{ github.sha }}
    run: |
      bash upload_coverage.sh \
        -t "$ACCESS_TOKEN" \
        -r "$REPO_NAME" \
        -c "$COMMIT_ID" \
        -f coverage.xml

Don’t forget to add your access token to the repo secrets under the name “ACCESS_TOKEN”. The access token should have access to the repo.

Coverage config file

You have to create a .coveragerc file in the project’s root folder to include all the source files in the test coverage calculation.

Example:

# include every Python file under the repo root
source = .

# exclude tests, virtualenvs, build artifacts, etc.
omit =
    */tests/*
    */.venv/*
    */build/*
    */dist/*

When you assign source to ”.” , It checks for every python file in the root folder and its sub directories. You can omit some directories by placing them in the omit section of the file.

How it works

With the above configuration:

  1. coverage run -m pytest tests/ will count every .py under the workspace as “valid” lines except for those in the omitted directories.
  2. Lines actually executed by your tests are marked “covered.”
  3. coverage xml -o coverage.xml produces a Cobertura-style report reflecting true coverage over the entire codebase.
  4. Using this coverage xml, we calculate the coverage percentage and the status check will be done on every new push to the branch.