Azure Pipeline执行pytest时出现Bash退出码5错误求助
修复方案
问题根源
pytest未收集到任何测试用例,且pytest-azurepipelines插件在无测试结果时返回错误码5,导致管道失败。
具体修复步骤:
- 规范测试文件命名:pytest默认识别
test_*.py或*_test.py格式的文件,确保你的测试文件符合该命名规则(避免命名为tests.py这类不符合规范的名称)。 - 指定测试路径:如果测试文件不在当前工作目录或命名特殊,在pytest命令中明确指定路径。例如测试文件在
tests目录下,修改脚本为:
若为单个测试文件- script: | pip install pytest pytest-azurepipelines pytest tests/ displayName: 'pytest'my_test.py,则用:- script: | pip install pytest pytest-azurepipelines pytest my_test.py displayName: 'pytest' - 允许无测试用例时不失败:如果你的场景允许无测试用例的情况,添加
--no-fail-on-empty-suite参数让pytest不返回错误码:- script: | pip install pytest pytest-azurepipelines pytest --no-fail-on-empty-suite displayName: 'pytest' - 验证工作目录文件:在pytest步骤前添加目录检查命令,确认测试文件存在于当前工作目录:
- script: | ls -la /home/vsts/work/1/s displayName: 'Check working directory contents'
内容的提问来源于stack exchange,提问作者Nintendo
相关产品推荐
相关产品推荐

