Skip to content

pytest_keyboard_interrupt hook is also called with --maxfail #1865

@blueyed

Description

@blueyed

When using -x / --maxfail the Interrupted exception is thrown, and gets handled in the same place as KeyboardInterrupt (

pytest/_pytest/main.py

Lines 99 to 106 in 86ec3f3

except KeyboardInterrupt:
excinfo = _pytest._code.ExceptionInfo()
if initstate < 2 and isinstance(
excinfo.value, pytest.exit.Exception):
sys.stderr.write('{0}: {1}\n'.format(
excinfo.typename, excinfo.value.msg))
config.hook.pytest_keyboard_interrupt(excinfo=excinfo)
session.exitstatus = EXIT_INTERRUPTED
), because it is a subclass.

Therefore I think the hook name is confusing / misleading.
If this behavior is intentional, it should get added to the documentation at least.

I have noticed this, because pytest-testmon uses this hook to not save its data: https://github.com/tarpas/pytest-testmon/blob/55c89bb3df65c94e226ead8dd465f3dd5bb8927b/testmon/pytest_testmon.py#L173-L174
Here a check using excinfo.typename could be added to handle 'Interrupted' different from 'KeyboardInterrupt'.

btw: is there a way to import the Interrupted class from pytest?

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: enhancementnew feature or API change, should be merged into features branch

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions