英文:
'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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论