Python 如何在函数外部触发py.test失败?

Python 如何在函数外部触发py.test失败?,python,pytest,Python,Pytest,我目前正在编写一个脚本,安装我的测试软件,然后使用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.Pop

我目前正在编写一个脚本,安装我的测试软件,然后使用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失败来告诉我的安装和测试构建代码不要发布一些构建

如果测试失败,py.test必须返回非零退出代码。最简单的处理方法是使用:

方法#1 我想这就是你要走的路。基本上,只需将test.py视为一个黑盒过程,并使用退出代码确定是否存在任何测试失败(例如,是否存在非零退出代码)

进近#2 另一个更干净的方法是


我不太明白到底是什么阻止了您遵循伪代码?测试失败需要查看pytest的日志中是否出现任何失败。如何检查日志中的故障?
try:
    subprocess.check_call(["py.test", "smoke_test_suite.py"])
except subprocess.CalledProcessError:
    print "Smoke tests have failed, not publishing"
else:
    print "Smoke tests have passed, publishing"
    # ...
exit_code = subprocess.Popen(["py.test", "smoke_test_suite.py"]).wait()
test_failures = bool(exit_code)
import pytest
exit_code = pytest.main("smoke_test_suite.py")
test_failures = bool(exit_code)