Capture exit code for pytest from python(从python捕获pytest的退出代码)
本文介绍了从python捕获pytest的退出代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
运行python.main()时,任何失败的单元测试都不会向调用测试的python模块返回退出代码%1。
如上所述here,运行pytest不会引发系统退出,但是有没有办法让失败的单元测试表现相同,或者允许将代码(1)返回到调用函数?
推荐答案
pytest.main()
将返回其退出代码-即an ExitCode
enum,从pytest 5.0.0开始。如果测试失败,则返回ExitCode.TESTS_FAILED
;如果所有测试均通过,则返回ExitCode.OK
。
site-packages/pytest/__main__.py
的源代码,如果使用python -m pytest
调用测试,则执行该源代码:
import pytest
if __name__ == "__main__":
raise SystemExit(pytest.main())
py.test
(或pytest
)入口点脚本基本相同
import re
import sys
from pytest import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script.pyw|.exe)?$', '', sys.argv[0])
sys.exit(main())
这篇关于从python捕获pytest的退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:从python捕获pytest的退出代码


基础教程推荐
猜你喜欢
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01