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

python-py.test:获取KeyboardInterrupt调用拆解

我正在使用py.test编写一些测试,在测试中我使用了funcargs.这些funcarg在conftest.py中定义了自己的设置和拆卸,如下所示:

conftest.py:

def pytest_funcarg__resource_name(request):
  def setup():
    # do setup
  def teardown():
    # do teardown

我的问题是,当有人使用CTRL C停止测试执行时,所有内容都不为人所知.
我知道有一个pytest_keyboard_interrupt钩子,但我不知道该怎么做.

很抱歉这个笨拙的问题.

解决方法:

您没有提供完整的示例,所以也许我缺少了一些东西.但是,这里有一个使用request.cached_setup()帮助器的示例:

def pytest_funcarg__res(request):
    def setup():
        print "res-setup"
    def teardown(val):
        print "res-teardown"
    return request.cached_setup(setup, teardown)

def test_hello(res):
    raise KeyboardInterrupt()

如果使用“ py.test”运行此命令,则会得到:

============================= test session starts ==============================
platform linux2 -- Python 2.7.3 -- pytest-2.2.5.dev4
plugins: xdist, bugzilla, pep8, cache
collected 1 items

tmp/test_keyboardinterrupt.py res-setup
res-teardown


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! KeyboardInterrupt !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/home/hpk/p/pytest/tmp/test_keyboardinterrupt.py:10: KeyboardInterrupt

这表明如果在测试执行过程中发生KeyboardInterrupt,则会调用设置和拆卸.

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

相关推荐