py.test: 获取 KeyboardInterrupt 调用拆卸 [英] py.test: get KeyboardInterrupt to call teardown

查看:107
本文介绍了py.test: 获取 KeyboardInterrupt 调用拆卸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using py.test to write some tests and in my tests I utilize funcargs. These funcargs have their own setups and teardowns defined in the conftest.py like this:

conftest.py:

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

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

My problem is when someone uses CTRL+C to stop the test executions it leaves everything un-teardowned. I know there is a hook pytest_keyboard_interrupt but I dont know what to do from there.

抱歉问了一个菜鸟问题.

Sorry for the noobish question.

推荐答案

你没有提供一个完整的例子,所以也许我遗漏了一些东西.但这里有一个使用 request.cached_setup() 助手的例子:

You don't provide a full example so maybe i am missing something. But here is an example of how it can work, using the request.cached_setup() helper:

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"运行它,你会得到:

If you run this with "py.test" you get:

============================= 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,则调用 setup 和 teardown.

which shows that setup and teardown are called if a KeyboardInterrupt occurs during test execution.

这篇关于py.test: 获取 KeyboardInterrupt 调用拆卸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆