UnitTests has a feature to capture KeyboardInterrupt
, finishes a test and then report the results.
-c, --catch
Control-C during the test run waits for the current test to end and then reports all the results so far. A second Control-C raises the normal KeyboardInterrupt exception.
See Signal Handling for the functions that provide this functionality.
cf https://docs.python.org/2/library/unittest.html#command-line-options
In PyTest, Ctrl + C will just stop the session.
Is there a way to do the same as UniTests:
KeyboardInterrupt
pytest-html
) Thanks
[Edit 11 November 2016]
I tried to put the hook in my conftest.py
file but it does not seem to work and capture. In particular, the following does not write anything in toto.txt
.
def pytest_keyboard_interrupt(excinfo):
with open('toto.txt', 'w') as f: f.write("Hello")
pytestmark = pytest.mark.skip('Interrupted Test Session')
Does anybody has a new suggestion?
Your issue may lie in the execution ordering of your hook, such that pytest exits prior to your hook being executed. This could happen if an unhandled exception occurs in preexisting handling of the keyboard interrupt.
To ensure your hook executes sooner, use tryfirst
or hookwrapper
as described here .
The following shall be written in conftest.py file:
import pytest
@pytest.hookimpl(tryfirst=True)
def pytest_keyboard_interrupt(excinfo):
with open('toto.txt', 'w') as f:
f.write("Hello")
pytestmark = pytest.mark.skip('Interrupted Test Session')
Take a look at pytest's hookspec .
They have a hook for keyword interrupt.
def pytest_keyboard_interrupt(excinfo):
""" called for keyboard interrupt. """
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.