Skip to content Skip to sidebar Skip to footer

Py.test: Get KeyboardInterrupt To Call Teardown

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

Solution 1:

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()

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

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


Post a Comment for "Py.test: Get KeyboardInterrupt To Call Teardown"