Azure DevOps Pipeline中未找到Pytest

3
在我的Azure DevOps管道中。

(https://dev.azure.com/TheNewThinkTank/dark-matter-attractor/_build?definitionId=2&_a=summary)

我有一个测试阶段,希望在对应的Azure DevOps存储库中的所有Python文件上安装并运行pytest。
https://dev.azure.com/TheNewThinkTank/_git/dark-matter-attractor)。
Pytest的安装和运行方式遵循官方的Microsoft文档。
- script: |
    pip install pytest
    pip install pytest-cov
    pytest tests --doctest-modules --junitxml=junit/test-results.xml --cov=. --cov-report=xml --cov-report=html
  displayName: 'Test with pytest'

此链接可找到:https://learn.microsoft.com/zh-cn/azure/devops/pipelines/ecosystems/python?view=azure-devops

运行管道会导致在上述步骤出现错误:pytest: command not found

如果您想查看完整的回溯信息:

https://dev.azure.com/TheNewThinkTank/dark-matter-attractor/_build/results?buildId=62&view=logs&j=792982d5-3bb1-5d82-222e-228148b73448&t=1f7e9bd0-3cdf-5b91-30b0-87c9b2af71b7

我尝试过的其他方法:

按照官方 pytest 网站(https://docs.pytest.org/en/stable/getting-started.html)的指引,我尝试在安装命令中添加一个标志:pip install -U pytest,但结果相同。

是什么阻止了 Pipeline 发现已安装的 pytest 模块?

2个回答

5
请尝试运行python -m pytest命令:
- script: |
    pip install pytest
    pip install pytest-cov
    python -m pytest tests --doctest-modules --junitxml=junit/test-results.xml --cov=. --cov-report=xml --cov-report=html
  displayName: 'Test with pytest'

通过 pip 安装的模块不能被识别为系统级别指令。


你好,我知道这是一个旧评论。我尝试了这个方法,但它仍然显示找不到pytest。你有其他的解决方法吗? - Obiii
1
刚刚遇到了同样的问题,但是当我使用以下命令安装pytest后:pip install -U pytest --user,python -m pytest 就可以正常工作了。 - marek_lani

0

@Krzysztof Madej,当前目录未加载为PYTHONPATH,因此出现了模块未找到的错误

 /usr/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_cfn_params_handler.py:17: in <module>
    from manifest.cfn_params_handler import CFNParamsHandler
E   ModuleNotFoundError: No module named 'manifest'

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