From 67ef85bc7c2e0e740782568e90674131f826b5fe Mon Sep 17 00:00:00 2001 From: Nils Uhrberg Date: Thu, 30 Jan 2025 16:22:36 +0100 Subject: [PATCH] ci: Add code coverage comment into pytest workflow --- .github/workflows/pytest.yaml | 56 +++++++++++++++++++++++++++++++++-- README.md | 3 ++ tests/test_file.py | 2 +- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/.github/workflows/pytest.yaml b/.github/workflows/pytest.yaml index 10e8c3e..5436797 100644 --- a/.github/workflows/pytest.yaml +++ b/.github/workflows/pytest.yaml @@ -4,14 +4,18 @@ on: pull_request: types: [ opened, reopened, synchronize ] workflow_call: - +permissions: + id-token: write + contents: write + checks: write + pull-requests: write jobs: test: strategy: fail-fast: false matrix: - os: [ ubuntu-latest, windows-latest, macos-latest ] - python-version: [ "3.10","3.11", "3.12", "3.13", "3.14" ] + os: [ ubuntu-latest ] + python-version: [ "3.10" ] runs-on: ${{ matrix.os }} timeout-minutes: 30 name: 🧪 Run pytests wit ${{matrix.python-version}} on ${{matrix.os}} @@ -63,3 +67,49 @@ jobs: # (Optional) Title of the test results section in the workflow summary title: Test results + - name: ✏️ Write test result as comment + uses: MishaKav/pytest-coverage-comment@v1 + with: + pytest-xml-coverage-path: ./coverage.xml + title: Coverage Report + badge-title: Code Coverage + hide-badge: false + hide-report: false + create-new-comment: false + hide-comment: false + report-only-changed-files: true + remove-link-from-badge: false + junitxml-path: ./junit.xml + junitxml-title: Pytest summary + github-token: ${{ secrets.GITHUB_TOKEN }} + # - name: Upload math result for job 1 + # uses: actions/upload-artifact@v4 + # with: + # name: code-coverage + # path: ./coverage.xml + # - name: Upload math result for job 1 + # uses: actions/upload-artifact@v4 + # with: + # name: code-coverage + # path: ./coverage.xml + # - name: Download math result for job 1 + # uses: actions/download-artifact@v4 + # with: + # name: code-coverage + - name: Pytest coverage comment + if: ${{ github.ref == 'refs/heads/feature/base-cicd' }} + id: coverageComment + uses: MishaKav/pytest-coverage-comment@main + with: + hide-comment: true + pytest-xml-coverage-path: ./coverage.xml + + - name: Update Readme with Coverage Html + run: | + sed -i '//,//c\\n\${{ steps.coverageComment.outputs.coverageHtml }}\n' ./README.md + + - name: Push changes + uses: ad-m/github-push-action@master + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + branch: ${{ github.ref }} diff --git a/README.md b/README.md index 3b66430..82c46f0 100644 --- a/README.md +++ b/README.md @@ -1 +1,4 @@ + + + [![🧪 pytest](https://github.com/codecentric-oss/lightml/actions/workflows/pytest.yaml/badge.svg)](https://github.com/codecentric-oss/lightml/actions/workflows/pytest.yaml) \ No newline at end of file diff --git a/tests/test_file.py b/tests/test_file.py index fb2462e..7e5b1e2 100644 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -23,4 +23,4 @@ def test_dummy_function(): - assert dummyfunction() == 0 + assert dummyfunction() == 1