如何设置使用 pipenv 运行 pytest 的 GitHub 操作?

How do I setup a GitHub action that runs pytest with pipenv?

我有一个使用 pipenv to run pytest. I want to create a GitHub Action 的 Python 项目,每次我提交拉取请求时都会 运行 pytest。

我试过使用 python-app.yml 入门工作流程。

name: Python application

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
    - name: Set up Python 3.8
      uses: actions/setup-python@v1
      with:
        python-version: 3.8
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Lint with flake8
      run: |
        pip install flake8
        # stop the build if there are Python syntax errors or undefined names
        flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
        # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
        flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
    - name: Test with pytest
      run: |
        pip install pytest
        pytest

但是我得到以下构建失败。

ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
##[error]Process completed with exit code 1.

我想避免创建 requirements.txt 文件并简单地使用 pipenv 到 运行 pytest。

如何创建使用 pipenv 到 运行 pytest 的 GitHub 操作?

选项 1

使用 dschep/install-pipenv-action@v1 GitHub 操作。

name: Python application

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v1
      - name: Set up Python 3.7
        uses: actions/setup-python@v1
        with:
          python-version: 3.7
      - name: Install pipenv
        uses: dschep/install-pipenv-action@v1
      - name: Run tests
        run: |
          pipenv install --dev
          pipenv run pytest

选项 2

只需 运行 pip install pipenv 命令——dschep/install-pipenv-action@v1 为您完成。

name: Python application

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v1
      - name: Set up Python 3.7
        uses: actions/setup-python@v1
        with:
          python-version: 3.7
      - name: Install pipenv
        run: pip install pipenv
      - name: Run tests
        run: |
          pipenv install --dev
          pipenv run pytest

我只是想补充一点,我在使用 pipenv 和 pytest 时也遇到了 FileNotFoundError:

在使用 pipenv run pytest 时,我在每个项目文件中都遇到了包含错误(您可以在此处查看日志:https://github.com/johannesgrothe/Smarthome_Bridge/runs/3080772118) 我需要做的是使用 pipenv run python -m pytest,现在一切都运行得很好 (https://github.com/johannesgrothe/Smarthome_Bridge/runs/3098788625)。我需要很多 pipenv 库,所以它肯定是在使用 pipenv python 实例。

我相信这两个命令做的完全一样,我不知道为什么第一个不起作用而后者却起作用。

由于我花了很多时间来弄清楚这个问题,我认为分享它会对其他人有所帮助,我希望这是适合它的地方。

注意:上一个答案中列出的 dschep/install-pipenv-action 操作已存档。

另一个选择是 palewire/install-python-pipenv-pipfile ,它安装 python 和 pipenv。

将它用于 运行 pytest 的示例:

name: Project Tests
on:
  push:
    branches: 
      - main
  pull_request:
    branches:
      - main 
jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Install Python, pipenv and Pipfile packages
        uses: palewire/install-python-pipenv-pipfile@v2
        with:
          python-version: 3.10.0
      - name: Run tests
        run: |
          pipenv install --dev
          pipenv run pytest

上述脚本中的 pipenv 安装假定 pytest 列在项目 Pipfile 的 [dev-packages] 部分下。