gpt4 book ai didi

python - 如何使 py.test 失败触发外部函数?

转载 作者:行者123 更新时间:2023-11-28 16:31:52 24 4
gpt4 key购买 nike

我目前正在编写一个脚本来安装我的待测软件,然后使用 py.test 自动运行我的冒烟测试。如果在这些测试中的任何一个发生故障,我想告诉我的软件不要将软件发布到构建服务器。伪代码基本上是这样的:

def install_build_and_test():
# some python code installs some_build
install_my_build(some_build)

# then I want to test my build
subprocess.Popen(["py.test", "smoke_test_suite.py"])
# test_failures = ???

# If any failures occurred during testing, do not publish build
if test_failures is True:
print "Build will not publish because there were errors in your logs"

if test_failures is False:
publish_build(some_build)

我的问题是如何使用 pytest 失败告诉我的 install_and_test_build 代码不要发布 some_build?

最佳答案

方法#1

我认为这就是您要走的路。基本上,只需将 test.py 视为黑盒进程并使用退出代码来确定是否存在任何测试失败(例如,是否存在非零退出代码)

exit_code = subprocess.Popen(["py.test", "smoke_test_suite.py"]).wait()
test_failures = bool(exit_code)

方法#2

另一种更简洁的方法是 run py.test in python directly .

import pytest
exit_code = pytest.main("smoke_test_suite.py")
test_failures = bool(exit_code)

关于python - 如何使 py.test 失败触发外部函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31096631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com