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

时间:2015-06-28 06:00:15

标签: 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.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?

2 个答案:

答案 0 :(得分:3)

方法#1

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

var test = a.replace( /\{key:(\d+)\}/g, "<span class='highlightable'>$1</span>");

方法#2

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

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

答案 1 :(得分:0)

如果测试失败,

py.test必须返回非零退出代码。处理这种情况的最简单方法是使用subprocess.check_call()

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"
    # ...