python-py.test:获取KeyboardInterrupt调用拆解
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-py.test:获取KeyboardInterrupt调用拆解,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1166字,纯文字阅读大概需要2分钟。
内容图文
![python-py.test:获取KeyboardInterrupt调用拆解](/upload/InfoBanner/zyjiaocheng/675/60041b93f241499bab0b00c405377f30.jpg)
我正在使用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,则会调用设置和拆卸.
内容总结
以上是互联网集市为您收集整理的python-py.test:获取KeyboardInterrupt调用拆解全部内容,希望文章能够帮你解决python-py.test:获取KeyboardInterrupt调用拆解所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。