Hinweis
GitHub Code Quality befindet sich derzeit in Öffentliche Vorschau und kann geändert werden. Während der öffentlichen Vorschauphase werden Code Quality nicht abgerechnet, obwohl Scans von Code Quality GitHub Actions Minuten verbrauchen.
In den folgenden Schritten generieren Sie aus Ihrer Testsuite einen Cobertura-XML-Bericht zur Testabdeckung, laden ihn zu GitHub hoch und sehen sich die Ergebnisse der Testabdeckung in Ihren Pull Requests an.
Voraussetzungen
- Code Quality ist für Ihr Repository aktiviert.
- Ihr Repository verfügt über eine Testsuite, die in GitHub Actionsausgeführt wird.
- Ihr Testframework kann einen Abdeckungsbericht im Cobertura-XML-Format erstellen.
Schritt 1: Generieren eines Cobertura XML-Abdeckungsberichts
Konfigurieren Sie Ihr Testframework, um einen Abdeckungsbericht im Cobertura-XML-Format auszugeben. Die Codeabdeckung funktioniert mit jeder Programmiersprache, die dieses Format erzeugen kann.
- Identifizieren Sie das Abdeckungstool für Ihre Sprache aus der folgenden Tabelle.
- Fügen Sie dem CI-Workflow den entsprechenden Befehl oder die entsprechende Konfiguration hinzu, damit bei jeder Ausführung der Tests eine Cobertura-XML-Datei generiert wird.
| Sprache | Framework /Tool | So generieren Sie Cobertura XML |
|---|---|---|
| Python | pytest + pytest-cov | pytest --cov=. --cov-report=xml |
| Java | JaCoCo | Verwenden Sie das cover2cover.py Skript oder das JaCoCo-to-Cobertura Gradle/Maven-Plug-In |
| JavaScript/TypeScript | Istanbul/ nyc | nyc report --reporter=cobertura |
| Ruby | SimpleCov | Fügen Sie SimpleCov::Formatter::CoberturaFormatter hinzu. |
| Go | go test + gocover-cobertura | go test -coverprofile=cover.out && gocover-cobertura < cover.out > coverage.xml |
Tipp
Wenn Ihr Framework oben nicht aufgeführt ist, überprüfen Sie die Dokumentation zur Cobertura-Ausgabeunterstützung. Viele Tools unterstützen sie entweder direkt oder können aus anderen Formaten in Cobertura XML konvertieren.
Schritt 2: Hochladen des Abdeckungsberichts
Nachdem Ihre Tests einen Cobertura-XML-Bericht generiert haben, laden Sie ihn bei GitHub hoch, damit die Ergebnisse zur Testabdeckung in Pull Requests angezeigt werden.
-
Öffnen Sie die CI-Workflowdatei Ihres Repositorys (z. B
.github/workflows/ci.yml. ). -
Fügen Sie den folgenden Schritt nach dem Schritt hinzu, der Ihre Tests ausführt, und generiert den Abdeckungsbericht:
YAML - name: Upload coverage report if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository uses: actions/upload-code-coverage@v1 with: file: COVERAGE-FILE-PATH.xml language: LANGUAGE label: LABEL- name: Upload coverage report if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository uses: actions/upload-code-coverage@v1 with: file: COVERAGE-FILE-PATH.xml language: LANGUAGE label: LABEL -
Ersetzen Sie die folgenden Werte:
COVERAGE-FILE-PATH.xml: Der Pfad zu Ihrem Cobertura-XML-Bericht (z. Bcoverage.xml. odertarget/site/jacoco/cobertura.xml).LANGUAGE: Die primäre Sprache des behandelten Codes (z. B.Python,Java,JavaScript).LABEL: Eine optionale Bezeichnung zum Identifizieren dieses Abdeckungsberichts (z. Bcode-coverage/pytest. ).
-
Übernehmen Sie den Commit, und übertragen Sie die Workflowänderung.
Vollständiges Workflowbeispiel
In diesem Beispiel werden Python Tests mit pytest-cov ausgeführt und der Abdeckungsbericht hochgeladen:
# This workflow runs your test suite, generates a Cobertura XML coverage report, and uploads it to GitHub. Once this workflow is committed, coverage results appear automatically on every pull request.
name: Code Coverage
# Run on pushes to the default branch (to establish the baseline) and on pull requests (to compare against it). Code Quality compares PR branch coverage to the default branch, so both triggers are needed.
on:
push:
branches: [main]
pull_request:
branches: [main]
# The `code-quality: write` permission is required to upload coverage data. No other elevated permissions are needed.
permissions:
contents: read
code-quality: write
jobs:
test:
runs-on: ubuntu-latest
steps:
# Check out the PR head commit (not the merge commit) so coverage line numbers map correctly to the diff.
- uses: actions/checkout@v6
with:
ref: ${{ github.event.pull_request.head.sha || github.sha }}
# Replace this step with whatever language setup your project uses (Node.js, Java, Go, etc.). The upload action works with any language that produces a Cobertura XML report.
- uses: actions/setup-python@v5
with:
python-version: "3.x"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pytest pytest-cov
# Adapt this step for your test framework. The key requirement is producing a Cobertura XML file. For other languages, see the framework table earlier in this article.
- name: Run tests with coverage
run: pytest --cov=. --cov-report=xml
# This step replaces any third-party coverage upload (Codecov, Coveralls, etc.). After this runs, the `github-code-quality[bot]` bot posts a coverage summary directly on the pull request.
- name: Upload coverage report
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
uses: actions/upload-code-coverage@v1
with:
file: coverage.xml
language: Python
label: code-coverage/pytest
name: Code CoverageThis workflow runs your test suite, generates a Cobertura XML coverage report, and uploads it to GitHub. Once this workflow is committed, coverage results appear automatically on every pull request.
on:
push:
branches: [main]
pull_request:
branches: [main]Run on pushes to the default branch (to establish the baseline) and on pull requests (to compare against it). Code Quality compares PR branch coverage to the default branch, so both triggers are needed.
permissions:
contents: read
code-quality: write
jobs:
test:
runs-on: ubuntu-latest
steps:The code-quality: write permission is required to upload coverage data. No other elevated permissions are needed.
- uses: actions/checkout@v6
with:
ref: ${{ github.event.pull_request.head.sha || github.sha }}Check out the PR head commit (not the merge commit) so coverage line numbers map correctly to the diff.
- uses: actions/setup-python@v5
with:
python-version: "3.x"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pytest pytest-covReplace this step with whatever language setup your project uses (Node.js, Java, Go, etc.). The upload action works with any language that produces a Cobertura XML report.
- name: Run tests with coverage
run: pytest --cov=. --cov-report=xmlAdapt this step for your test framework. The key requirement is producing a Cobertura XML file. For other languages, see the framework table earlier in this article.
- name: Upload coverage report
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
uses: actions/upload-code-coverage@v1
with:
file: coverage.xml
language: Python
label: code-coverage/pytestThis step replaces any third-party coverage upload (Codecov, Coveralls, etc.). After this runs, the github-code-quality[bot] bot posts a coverage summary directly on the pull request.
# This workflow runs your test suite, generates a Cobertura XML coverage report, and uploads it to GitHub. Once this workflow is committed, coverage results appear automatically on every pull request.
name: Code Coverage
# Run on pushes to the default branch (to establish the baseline) and on pull requests (to compare against it). Code Quality compares PR branch coverage to the default branch, so both triggers are needed.
on:
push:
branches: [main]
pull_request:
branches: [main]
# The `code-quality: write` permission is required to upload coverage data. No other elevated permissions are needed.
permissions:
contents: read
code-quality: write
jobs:
test:
runs-on: ubuntu-latest
steps:
# Check out the PR head commit (not the merge commit) so coverage line numbers map correctly to the diff.
- uses: actions/checkout@v6
with:
ref: ${{ github.event.pull_request.head.sha || github.sha }}
# Replace this step with whatever language setup your project uses (Node.js, Java, Go, etc.). The upload action works with any language that produces a Cobertura XML report.
- uses: actions/setup-python@v5
with:
python-version: "3.x"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pytest pytest-cov
# Adapt this step for your test framework. The key requirement is producing a Cobertura XML file. For other languages, see the framework table earlier in this article.
- name: Run tests with coverage
run: pytest --cov=. --cov-report=xml
# This step replaces any third-party coverage upload (Codecov, Coveralls, etc.). After this runs, the `github-code-quality[bot]` bot posts a coverage summary directly on the pull request.
- name: Upload coverage report
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
uses: actions/upload-code-coverage@v1
with:
file: coverage.xml
language: Python
label: code-coverage/pytest
Schritt 3: Anzeigen der Abdeckungsergebnisse für Pullanforderungen
- Öffnen Sie eine Pullanforderung (oder pushen Sie an eine vorhandene Anforderung), die den von Ihnen konfigurierten Workflow auslöst.
- Nachdem der Workflow abgeschlossen ist, suchen Sie im Pull Request nach einem Kommentar von
github-code-quality[bot]. Der Kommentar umfasst:- Der Prozentsatz der aggregierten Testabdeckung für den Pull-Request-Branch im Vergleich zum Standard-Branch.
- Eine Aufschlüsselung nach Dateien, die zeigt, welche Dateien an Abdeckung gewonnen oder verloren haben.
Nächste Schritte
- Ergebnisse interpretieren: Verstehen Sie Abdeckungsmetriken und Aufschlüsselungen pro Datei in Ihren Pull Requests. Siehe Die Codequalitätsergebnisse Ihres Repositorys interpretieren.