如何设置 GitHub action 以使用 pipenv 运行 pytest?

12
我有一个使用 pipenv 运行 pytest 的 Python 项目。我想创建一个 GitHub Action,每当我提交拉取请求时都会运行 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 Action?
2个回答

22

首先安装pipenv,然后你就可以使用pipenv运行pytest了。

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 pipenv
        run: pip install pipenv
      - name: Run tests
        run: |
          pipenv install --dev
          pipenv run pytest

1
dschep/install-pipenv-action@v1pip install pipenv 有什么区别? - Mo...
1
@MoK 看起来 dschep/install-pipenv-action@v1 基本上是运行 pip install pipenv 并进行了一些额外的更改(例如在 sudo 下运行):https://github.com/dschep/install-pipenv-action/blob/master/src/main.ts。 - Ryan Payne
3
"pip install pipenv" 单独使用就可以了。我在回答中加了第二个选项。 - Ryan Payne
谢谢!使用 pipenv run pytest . 而不是仅仅使用 pytest . 解决了我的 pytest not found 问题。 - Ryan Aquino
我们应该使用 pipenv sync --dev 而不是 pipenv install --dev 吗? - room13
pipenv sync --dev 是什么意思? - Ryan Payne

1

注意:之前回答中列出的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 install假定pytest在项目的Pipfile的[dev-packages]部分中列出。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接