使用 GitHub actions,如何将 Python 文件从 PR 传递到 pytest?

问题描述 投票:0回答:1

在我的 GH 操作中,我获取了 PR 中所有已更改的文件。如何将每个 Python 文件传递给 pytest? (我不想在整个目录上运行 pytest,只是更改了文件。)这是我当前代码的

jobs
部分:

jobs:
  build:
    name: Testing Python Files
    runs-on: ubuntu-latest
    # Required permissions to run the action
    permissions:
      contents: read
      pull-requests: read
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Get all the changed files
        id: changed-files
        uses: tj-actions/changed-files@v42
        # To compare changes between the current commit and the last pushed remote commit set `since_last_remote_commit: true`. e.g
        with:
          since_last_remote_commit: true

      - name: Set up Python 3.10
        uses: actions/setup-python@v5
        with:
          python-version: "3.10"

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

      - name: Testing Python files
        run: |
          for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
            # Check for Python solution files
            if [[ $file == *.py ]]; then
                echo "Testing $file"
                pytest $file
            fi
         done

我得到的输出是:

Testing Lessons/Intro-to-Python/1/Activities/01-Ins_Python_Intro/Solved/variables_solution.py
======================== test session starts =========================
platform linux -- Python 3.10.13, pytest-8.0.1, pluggy-1.4.0
rootdir: /home/runner/work/GitHub-Actions-Playground/GitHub-Actions-Playground
collected 0 items
===================== no tests ran in 0.01s =======================
Error: Process completed with exit code 5.
python pytest github-actions cicd
1个回答
0
投票

为什么不起作用?

首先,让我们检查一下退出代码 5 是什么意思这里

Exit code 5: No tests were collected

那么答案很简单

Testing Lessons/Intro-to-Python/1/Activities/01-Ins_Python_Intro/Solved/variables_solution.py
没有测试。

由于这在第一个文件上失败,它甚至不会检查下一个文件。

解决方案

  1. 过滤掉没有测试的文件或仅运行那些有测试的文件。
  2. 忽略退出代码。在 bash 中,这是通过
    set -e
    完成的,但是请不要这样做:)
© www.soinside.com 2019 - 2024. All rights reserved.