【问题标题】:How do I setup a GitHub action that runs pytest with pipenv?如何设置使用 pipenv 运行 pytest 的 GitHub 操作?
【发布时间】:2020-03-30 18:43:07
【问题描述】:

我有一个使用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?

【问题讨论】:

    标签: python github pytest pipenv github-actions


    【解决方案1】:

    选项 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
    

    【讨论】:

    • dschep/install-pipenv-action@v1pip install pipenv有什么区别
    • @MoK 看起来 dschep/install-pipenv-action@v1 基本上是在运行 pip install pipenv 并进行了一些额外的更改(例如在 sudo 中运行):github.com/dschep/install-pipenv-action/blob/master/src/main.ts
    • @MoK pip install pipenv 自己的作品就好了。我在答案中添加了第二个选项。
    • 谢谢!,运行pipenv run pytest . 而不仅仅是pytest . 解决了我的pytest not found 问题
    【解决方案2】:

    我只是想补充一点,我在使用 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 实例。

    我相信两个命令的作用完全相同,我不知道为什么第一个不起作用,但后者起作用。

    由于我花了很多时间才弄清楚这一点,我认为分享它会对其他人有益,我希望这是适合它的地方。

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    【解决方案3】:

    注意:上一个答案中列出的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] 部分下。

    【讨论】:

      猜你喜欢
      • 2020-11-18
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      • 1970-01-01
      • 2022-10-23
      • 2022-08-13
      • 2018-11-29
      相关资源
      最近更新 更多