微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我目前正在编写一个脚本,该脚本安装要测试的软件,然后使用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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐