You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Github Actions中pytest版本命令正常但测试命令无法识别的问题求助

Github Actions中pytest版本命令正常但测试命令无法识别的问题求助

大家好,我正在为自己的Python包imfp配置Github Actions的构建与测试流程,工具用的是poetry和pytest,虚拟环境也已经设置好了。前面的步骤都顺利执行,可到运行测试这一步就卡壳了,遇到了很奇怪的问题:

pytest --version能正常输出pytest 7.2.2,但执行pytest tests时却报错pytest: command not found。我参考其他帖子的建议试了几种写法,结果都不行:

  • py.test tests → 报错py.test: command not found
  • python -m pytest tests → 报错No module named pytest
  • py -m pytest tests → 报错No module named pytest
  • python3 -m pytest tests → 报错No module named pytest

以下是我的Github Actions脚本:

name: build-test

on:
  push:
    branches: [main, master]
  pull_request:
    branches: [main, master]

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.11"]
        poetry-version: [latest]
        os: [windows-latest]
    defaults:
      run:
        shell: bash
    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install and configure Poetry
        uses: snok/install-poetry@v1
      - name: Configure Poetry
        run: |
          poetry config virtualenvs.in-project true
      - name: Install project
        run: |
          poetry install --no-interaction
      - name: Activate venv windows
        run: |
          source .venv/scripts/activate
          pytest --version
        if: runner.os == 'Windows'
      - name: Activate venv other
        run: |
          source .venv/bin/activate
          pytest --version
        if: runner.os != 'Windows'
      - name: Run tests
        run: |
          python3 -m pytest tests/

需要说明的是,我这个情况和大多数类似问题不太一样——别人大多是pytest --version都无法执行,而我这里这个命令是正常的,唯独跑测试时各种方式都失效了,有没有大佬能帮我分析下问题出在哪?

备注:内容来源于stack exchange,提问作者Christopher Carroll Smith

火山引擎 最新活动