'pytest –version' works, but 'pytest tests' command not recognized with Bash in Github Actions

huangapple go评论66阅读模式
英文:

'pytest --version' works, but 'pytest tests' command not recognized with Bash in Github Actions

问题

对于我的Python包'imfp',我正在使用'Github Actions'通过'poetry'和pytest在虚拟环境中进行构建和测试。奇怪的是,'pytest --version'命令可以正常工作,显示'pytest 7.2.2'。然而,'pytest tests'返回错误,显示'pytest: command not found'。

根据Substack上其他帖子中的建议,我还尝试了以下命令:

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'的失败,但在这种情况下,该命令可以正常工作。

workflow日志:https://github.com/chriscarrollsmith/imfp/actions/workflows/actions.yml

英文:

For my Python package 'imfp', I am doing my building and testing via 'Github Actions' using 'poetry' and pytest in a virtual environment. I've got the Github Actions script working successfully until the very step, when I run the tests using pytest.

Weirdly, the 'pytest --version' command works just fine, printing 'pytest 7.2.2'. However, 'pytest tests' returns the error, 'pytest: command not found'.

Per suggestions found in other threads on Substack, I have also tried:

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

Here is my Github Actions script:

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/

Note that as far as I can tell, this question is not a duplicate. Most of the similar threads involve a failure of 'pytest --version', but that command is working in this case.

workflow logs: https://github.com/chriscarrollsmith/imfp/actions/workflows/actions.yml

答案1

得分: 2

根据jobs.<job_id>.steps[*].run

每个 run 关键词表示在运行环境中启动一个新的进程和shell。

因此,在上一步中激活的 venv 在下一步中不会保持激活状态。

在运行测试之前,需要再次激活它。

英文:

According to jobs.<job_id>.steps[*].run:

> Each run keyword represents a new process and shell in the runner environment.

So, venv activated in a previous step does not remain activated in the next one.

You need to activate it again before running tests.

答案2

得分: 2

激活虚拟环境并不总是必要的。您还可以直接调用虚拟环境中的二进制文件,分别位于虚拟环境的二进制文件目录中(在Windows上为Scripts,在其他地方为bin)。例如:.venv/Scripts/python -m pytest

或者,由于使用了Poetry,您还可以调用poetry run python -m pytest(这可能是最佳解决方案,在使用Poetry时一直使用poetry run)。

英文:

Activating a virtual environment is not always necessary. One can also directly call the binaries in the virtual environment's binaries directory instead (Scripts on Windows and bin anywhere else). So for example: .venv/Scripts/python -m pytest.

Alternatively, since Poetry is used, one can also call poetry run python -m pytest (which is probably the best solution, stick to poetry run as long as you use Poetry).

huangapple
  • 本文由 发表于 2023年3月31日 22:52:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定