Skip to content

Catch exceptions in other Qt threads #11

Closed
@jdreaver

Description

@jdreaver

I recently switched to pytest-qt, and I love it so far. Before, I used my own pytest fixture to create a QApplication. I found a way to catch exceptions on all threads by overriding sys.excepthook in the fixture and checking for errors:

caught_exceptions = []


@pytest.yield_fixture
def qtbot_(request, qtbot):
    # Set excepthook to catch pyside errors in other threads.
    global caught_exceptions
    caught_exceptions = []

    def pytest_excepthook(type_, value, tback):
        global caught_exceptions
        caught_exceptions.append(''.join(traceback.format_tb(tback)) + "\n" +
                                 str(type_.__name__) + ": " + str(value))
        sys.__excepthook__(type_, value, tback)
    sys.excepthook = pytest_excepthook

    yield qtbot

    sys.excepthook = sys.__excepthook__
    if caught_exceptions:
        pytest.fail("Caught qt exceptions:\n{}".format(
            "\n".join(caught_exceptions)))

I created this so I can see when my table model methods fail. Without this, if an error happens in the data() or header() methods of a table model, like when a table view is being populated using the model, the test will not fail.

I think something similar to this would be a nice feature of pytest-qt, but a better, less hacky implementation would probably be better.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions