- Added official support for Python 3.13.
- Dropped support for EOL Python 3.8.
- Dropped support for EOL PySide 2.
- Type annotations are now provided. Note that because the Qt library used is defined at runtime, Qt classes are currently annotated as
Any. - Fixed PySide6 exceptions / warnings about being unable to disconnect signals
with
qtbot.waitSignal(#552, #558). - Reduced the likelyhood of trouble when using
qtbot.waitSignal(s)andqtbot.waitCallbackwhere the signal/callback is emitted from a non-main thread. In theory, more problems remain and this isn't a proper fix yet. In practice, it seems impossible to provoke any problems in pytest-qt's testsuite. (#586)
pluggy >=1.1is now required: we now use new-style hook wrappers, which are less error prone.- Fixed exception handling so they are properly cleared in Python 3.12, due to the new sys.last_exc attribute (#532).
- Added official support for Python 3.12.
- Python 3.7 is no longer supported.
qappnow sets up theQApplicationinstance with a command line argument like thisQApplication([prog_name])instead of using an empty listQApplication([]). Hereprog_nameis the name of the app which defaults topytest-qt-app, but can be redefined in thepytest.inifile, see :ref:`qapp fixture<setting-qapp-name>`. Alternatively, the arguments that will be passed toQApplicationcan be defined explicitly using theqapp_argsfixture. This means that the default behavior of theqapp_argsfixture is now also changed accordingly: it now returns the list[prog_name]instead of an empty list. Thanks to @The-Compiler (#483) and @hakonhagland (#515).
- Failure during publish.
- Import the
codesub-package from the correct location rather than the deprecatedpypackage, restoring compatibility with pytest 7.2.0, wherepywas dropped. Thanks @The-Compiler for the PR. - Use
pytest.hookimplto configure hooks, avoiding a deprecation warning in pytest 7.2.0. Thanks @The-Compiler for the PR. - Now
pytest-qtwill check if any of the Qt libraries is already imported by the time the plugin loads, and use it if that is the case (#412). Thanks @eyllanesc for the PR. - Most custom
pytest-qtexceptions can be accessed viaqtbot(for exampleqtbot.TimeoutError), but it was not always explicit in the documentation that this is the recommended way to access those exceptions, instead of importing them frompytestqt.exceptions. This is now clarified in the documentation and examples, and an alias toScreenshotErrorhas been added toqtbotso it can be accessed in the same way (#460).
pytest-qtnow requires Python 3.7+.- Improved PEP-8 aliases definition so they have a smaller call stack depth by one and better parameter suggestions in IDEs. (#383). Thanks @luziferius for the PR.
- Updated model tester handling around
hasChildrenbased on Qt's updates. - New
qapp_clsfixture returning theQApplicationclass to use, thus making it easier to use a custom subclass without having to override the wholeqappfixture. Thanks @The-Compiler for the PR. - Updated model tester to track/verify in-flight changes based on Qt's updates. Thanks @The-Compiler for the PR.
- New
qtbot.screenshot()method which can be used to take a screenshot of the given widget. Thanks @The-Compiler for the PR.
- Restored compatibility with PySide2 5.11, which doesn't depend on the
shiboken2project, used by pytest-qt 4.0.0. The dependency is now not needed anymore, and the.isdeletedattribute ofqt_compat(which isn't intended for public use) is removed.
- The
sipmodule now gets imported directly ifPyQt5.sip/PyQt6.sipwasn't found, as it's still packaged like that in some distributions (#369). Thanks @The-Compiler for the PR.
PySide6 and PyQt6 (6.1+) are now supported. Thanks @jensheilman and @The-Compiler for the PRs (#328, #330).
pytest-qtnow requires Python 3.6+.When using PyQt5,
pytest-qtnow requires PyQt5 5.11 or newer (#330).Support for Qt4 (i.e.
PyQt4andPySide) is now dropped (#279).The
qtbot.waitActiveandqtbot.waitExposedcontext managers are now available with all Qt APIs, rather than only PyQt5 (#361). Thanks @The-Compiler for the PR.The
qtbot.waitForWindowShownmethod is deprecated, as the underlying Qt method was obsoleted in Qt 5.0 and removed in Qt 6.0. Its name is imprecise and the pytest-qt wrapper does not raise TimeoutError if the window wasn't shown. Please use theqtbot.waitExposedcontext manager instead (#361). Thanks @The-Compiler for the PR.The old
qtbot.stopForInteraction()name is now removed as it was cumbersome and rarely used. Useqtbot.stop()(added in 1.1.1) instead (#306). Thanks @The-Compiler for the PR.The old
SignalTimeoutErrorexception alias is now removed, as it was renamed toTimeoutErrorin 2.1 (#306). Thanks @The-Compiler for the PR.The old
qt_wait_signal_raisingoption is now removed, as it was renamed toqt_default_raisingin 3.1 (#306). Thanks @The-Compiler for the PR.qtbot.waitSignalandwaitSignals(as well as their PEP-8 aliases) supported passingNoneas signal, making them wait for the given timeout instead. This is not supported anymore, useqtbot.wait(ms)instead (#306). Thanks @The-Compiler for the PR.Various arguments to
qtbotmethods are now keyword-only (#366):qtbot.waitActive:timeout(widgetbeing the only positional argument)qtbot.waitExposed:timeout(widgetbeing the only positional argument)qtbot.waitSignal:timeout,raisingandcheck_params_cb(signalbeing the only positional argument)qtbot.waitSignals:timeout,raisingandcheck_params_cbs(signalsbeing the only positional argument)qtbot.assertNotEmitted:wait(signalbeing the only positional argument)qtbot.waitUntil:timeout(callbackbeing the only positional argument)qtbot.waitCallback:timeoutandraising(with no positional arguments)
The same applies to the respective PEP-8 aliases. Thanks @The-Compiler for the PR.
Various classes are now not importable from
pytestqt.pluginanymore, and should instead be imported from the module they're residing in since the 1.6.0 release (#306):pytestqt.plugin.QtBot->pytestqt.qtbot.QtBotpytestqt.plugin.SignalBlocker->pytestqt.wait_signal.SignalBlockerpytestqt.plugin.MultiSignalBlocker->pytestqt.wait_signal.MultiSignalBlockerpytestqt.plugin.Record->pytestqt.logging.Recordpytestqt.plugin.capture_exceptions->pytestqt.exceptions.capture_exceptions(but consider usingqtbot.capture_exceptionsinstead)pytestqt.plugin.format_captured_exceptions->pytestqt.exceptions.format_captured_exceptions
The
qt_api.extract_from_variantandqt_api.make_variantfunctions (which were never intended for public usage) as well as all class aliases (such asqt_api.QWidgetorqt_api.QEvent, among others) are now removed. Thanks @The-Compiler for the PR.The default timeouts for
qtbot.waitSignal,waitSignals,waitUntilandwaitCallback,waitActiveandwaitExposedhave been raised from 1s to 5s. This makes them in line the default timeout used by Qt's underlying methods such asQSignalSpy::wait. To get the old behavior back, explicitly passtimeout=1000to those functions (#306). Thanks @The-Compiler for the PR.waitUntilnow raises aTimeoutErrorwhen a timeout occurs to make the cause of the timeout more explicit (#222). Thanks @karlch for the PR.The
QtTest::keySequencemethod is now exposed (if available, with Qt >= 5.10) (#289). Thanks @The-Compiler for the PR.addWidgetnow enforces that its argument is aQWidgetin order to display a clearer error when this isn't the case (#290). Thanks @The-Compiler for the PR.New option
qt_qapp_namecan be used to set the name of theQApplicationcreated bypytest-qt, defaulting to"pytest-qt-qapp"(#302). Thanks @The-Compiler for the PR.When the
-s(--capture=no) argument is passed to pytest, Qt log capturing is now disabled as well (#300). Thanks @The-Compiler for the PR.PEP-8 aliases (
add_widget,wait_active, etc) are no longer just simple assignments to the methods, but they are real methods which call the normal implementations. This makes subclasses work as expected, instead of having to duplicate the assignment (#326, #333). Thanks @oliveira-mauricio and @jensheilman for the PRs.Errors related to the
qt_compatmodule (such as an invalidPYTEST_QT_APIsetting or missing Qt API wrappers) are now shown as a more human-readable error message rather than an internal pytest error (#355). Thanks @The-Compiler for the PR.
- Improve message in uncaught exceptions by mentioning the Qt event loop instead of Qt virtual methods (#255).
pytest-qtnow requirespytestversion >= 3.0.qtbot.addWigetnow supports an optionalbefore_close_funckeyword-only argument, which if given is a function which is called before the widget is closed, with the widget as first argument.
- Fix Off-by-one error in
modeltester(#249). Thanks @ext-jmmugnes for the PR.
- Fixed compatibility with PyQt5 5.11.3
- The
CallbackBlockerreturned byqtbot.waitCallback()now has a newassert_called_with(...)convenience method.
- If Qt's model tester implemented in C++ is available (PyQt5 5.11 or newer),
the
qtmodeltesterfixture now uses that instead of the Python implementation. This can be turned off by passingforce_py=Truetoqtmodeltester.check(). - The Python code used by
qtmodeltesteris now based on the latest Qt modeltester. This also means that thedata_display_may_return_noneattribute forqtmodeltesterisn't used anymore. - New
qtbot.waitCallback()method that returns aCallbackBlocker, which can be used to wait for a callback to be called. qtbot.assertNotEmittednow has a newwaitparameter which can be used to make sure asynchronous signals aren't emitted by waiting after the code in thewithblock finished.- The
qt_wait_signal_raisingoption was renamed toqt_default_raising. The old name continues to work, but is deprecated. - The docs still referred to
SignalTimeoutErrorin some places, despite it being renamed toTimeoutErrorin the 2.1 release. This is now corrected. - Improve debugging output when no Qt wrapper was found.
- When no context is available for warnings on Qt 5, no
None:None:0line is shown anymore. - The
no_qt_logmarker is now registered with pytest so--strictcan be used. qtbot.waitSignalwith timeout0now expects the signal to arrive directly in the code enclosed by it.
Thanks @The-Compiler for the PRs.
- Another fix related to
QtInfoMsgobjects during logging (#225).
- Fix handling of
QtInfoMsgobjects during logging (#225). Thanks @willsALMANJ for the report.
- Removed
qtbot.mouseEventproxy, it was an internal Qt function which has now been removed in PyQt 5.11 (#219). Thanks @mitya57 for the PR. - Fix memory leak when tests raised an exception inside Qt virtual methods (#187). Thanks @fabioz for the report and PR.
- Properly handle chained exceptions when capturing them inside virtual methods (#215). Thanks @fabioz for the report and sample code with the fix.
- Use new pytest 3.6 marker API when possible (#212). Thanks @The-Compiler for the PR.
PYTEST_QT_APIenvironment variable correctly wins overqt_apiini variable if both are set at the same time (#196). Thanks @mochick for the PR.
- New
qapp_argsfixture which can be used to pass custom arguments toQApplication. Thanks @The-Compiler for the PR.
modeltesternow acceptsQBrushforBackgroundColorRoleandTextColorRole(#189). Thanks @p0las for the PR.
- Fix issue where
pytestqtwas hiding the information when there's an exception raised from another exception on Python 3.
- Fixed tests on Python 3.6.
waitSignalandwaitSignalsnow provide much more detailed messages when expected signals are not emitted. Many thanks to @MShekow for the PR (#153).qtbotfixture now can capture Qt virtual method exceptions in a block usingcaptureExceptions(#154). Thanks to @fogo for the PR.- New qtbot.waitActive and qtbot.waitExposed methods for PyQt5. Thanks @The-Compiler for the request (#158).
SignalTimeoutErrorhas been renamed toTimeoutError.SignalTimeoutErroris kept as a backward compatibility alias.
With pytest-qt 2.0, we changed some defaults to values we think are much
better, however this required some backwards-incompatible changes:
pytest-qtnow defaults to usingPyQt5ifPYTEST_QT_APIis not set. Before, it preferredPySidewhich is using the discontinued Qt4.- Python 3 versions prior to 3.4 are no longer supported.
- The
@pytest.mark.qt_log_ignoremark now defaults toextend=True, i.e. extends the patterns defined in the config file rather than overriding them. You can passextend=Falseto get the old behaviour of overriding the patterns. qtbot.waitSignalnow defaults toraising=Trueand raises an exception on timeouts. You can setqt_wait_signal_raising = falsein your config to get back the old behaviour.PYTEST_QT_FORCE_PYQTenvironment variable is no longer supported. SetPYTEST_QT_APIto the appropriate value instead or the newqt_apiconfiguration option in yourpytest.inifile.
- From this version onward,
pytest-qtis licensed under the MIT license (#134). - New
qtmodeltesterfixture to testQAbstractItemModelsubclasses. Thanks @The-Compiler for the initiative and port of the original C++ code for ModelTester (#63). - New
qtbot.waitUntilmethod, which continuously calls a callback until a condition is met or a timeout is reached. Useful for testing asynchronous features (like in X window environments for example). waitSignalandwaitSignalscan receive an optional callback (or list of callbacks) that can evaluate if the arguments of emitted signals should resume execution or not. AdditionallywaitSignalshas a neworderparameter that allows to expect signals and their arguments in a strict, semi-strict or no specific order. Thanks @MShekow for the PR (#141).- Now which Qt binding
pytest-qtwill use can be configured by theqt_apiconfig option. Thanks @The-Compiler for the request (#129). - While
pytestqt.qt_compatis an internal module and shouldn't be imported directly, it is known that some test suites did import it. This module now uses a lazy-load mechanism to load Qt classes and objects, so the old symbols (QtCore,QApplication, etc.) are no longer available from it.
- Exceptions caught by
pytest-qtinsys.excepthookare now also printed tostderr, making debugging them easier from within an IDE. Thanks @fabioz for the PR (126)!
Note
The default value for raising is planned to change to True starting in
pytest-qt version 1.12. Users wishing to preserve
the current behavior (raising is False by default) should make
use of the new qt_wait_signal_raising ini option below.
New
qt_wait_signal_raisingini option can be used to override the default value of theraisingparameter of theqtbot.waitSignalandqtbot.waitSignalsfunctions when omitted:[pytest] qt_wait_signal_raising = true
Calls which explicitly pass the
raisingparameter are not affected. Thanks @The-Compiler for idea and initial work on a PR (120).qtbotnow has a newassertNotEmittedcontext manager which can be used to ensure the given signal is not emitted (92). Thanks @The-Compiler for the PR!
SignalBlockernow has aargsattribute with the arguments of the signal that triggered it, orNoneon a time out (115). Thanks @billyshambrook for the request and @The-Compiler for the PR.MultiSignalBlockeris now properly disconnects from signals upon exit.
- Exception capturing now happens as early/late as possible in order to catch all possible exceptions (including fixtures)(105). Thanks @The-Compiler for the request.
- Widgets registered by
qtbot.addWidgetare now closed before all other fixtures are tear down (106). Thanks @The-Compiler for request. qtbotnow has a newwaitmethod which does a blocking wait while the event loop continues to run, similar toQTest::qWait. Thanks @The-Compiler for the PR (closes 107)!- raise
RuntimeErrorinstead ofImportErrorwhen failing to import any Qt binding: raising the latter causespluggyinpytest-2.8to generate a subtle warning instead of a full blown error. Thanks @Sheeo for bringing this problem to attention (closes 109).
pytest.mark.qt_log_ignorenow supports anextendparameter that will extend the list of regexes used to ignore Qt messages (defaults to False). Thanks @The-Compiler for the PR (99).- Fixed internal error when interacting with other plugins that raise an error, hiding the original exception (98). Thanks @The-Compiler for the PR!
- Now
pytest-qtis properly tested with PyQt5 on Travis-CI. Many thanks to @The-Compiler for the PR!
PYTEST_QT_APIcan now be set topyqt4v2in order to use version 2 of the PyQt4 API. Thanks @montefra for the PR (93)!
- Reduced verbosity when exceptions are captured in virtual methods (77, thanks @The-Compiler).
pytestqt.pluginhas been split in several files (74) and tests have been moved out of thepytestqtpackage. This should not affect users, but it is worth mentioning nonetheless.QApplication.processEvents()is now called before and after other fixtures and teardown hooks, to better try to avoid non-processed events from leaking from one test to the next. (67, thanks @The-Compiler).- Show Qt/PyQt/PySide versions in pytest header (68, thanks @The-Compiler!).
- Disconnect SignalBlocker functions after its loop exits to ensure second emissions that call the internal functions on the now-garbage-collected SignalBlocker instance (#69, thanks @The-Compiler for the PR).
- Exceptions are now captured also during test tear down, as delayed events will
get processed then and might raise exceptions in virtual methods;
this is specially problematic in
PyQt5.5, which changed the behavior to callabortby default, which will crash the interpreter. (65, thanks @The-Compiler).
- Fixed log line number in messages, and provide better contextual information in Qt5 (55, thanks @The-Compiler);
- Fixed issue where exceptions inside a
waitSignalsorwaitSignalwith-statement block would be swallowed and aSignalTimeoutErrorwould be raised instead. (59, thanks @The-Compiler for bringing up the issue and providing a test case); - Fixed issue where the first usage of
qappfixture would returnNone. Thanks to @gqmelo for noticing and providing a PR; - New
qtlognow sports a context manager method,disabled(58). Thanks @The-Compiler for the idea and testing;
- Messages sent by
qDebug,qWarning,qCriticalare captured and displayed when tests fail, similar to pytest-catchlog. Also, tests can be configured to automatically fail if an unexpected message is generated. - New method
waitSignals: will block until all signals given are triggered (thanks @The-Compiler for idea and complete PR). - New parameter
raisingtowaitSignalsandwaitSignals: whenTruewill raise aqtbot.SignalTimeoutErrorexception when timeout is reached (defaults toFalse). (thanks again to @The-Compiler for idea and complete PR). pytest-qtnow requirespytestversion >= 2.7.
QApplication.exit()is no longer called at the end of the test session and theQApplicationinstance is not garbage collected anymore;QtBotno longer receives a QApplication as a parameter in the constructor, always referencingQApplication.instance()now; this avoids keeping an extra reference in theqtbotinstances.deleteLateris called on widgets added inQtBot.addWidgetat the end of each test;QApplication.processEvents()is called at the end of each test to make sure widgets are cleaned up;
pytest-qt now supports PyQt5!
Which Qt api will be used is still detected automatically, but you can choose one using the
PYTEST_QT_APIenvironment variable (the oldPYTEST_QT_FORCE_PYQTis still supported for backward compatibility).Many thanks to @jdreaver for helping to test this release!
- Now the module
``qt_compat``no longer setsQStringandQVariantAPIs to2for PyQt, making it compatible for those still using version1of the API.
- Now it is possible to disable automatic exception capture by using markers or
a
pytest.inioption. Consult the documentation for more information. (26, thanks @datalyze-solutions for bringing this up). QApplicationinstance is created only if it wasn't created yet (21, thanks @fabioz!)addWidgetnow keeps a weak reference its widgets (20, thanks @fabioz)
- Fixed 16: a signal emitted immediately inside a
waitSignalblock now works as expected (thanks @baudren).
This version include the new waitSignal function, which makes it easy
to write tests for long running computations that happen in other threads
or processes:
def test_long_computation(qtbot):
app = Application()
# Watch for the app.worker.finished signal, then start the worker.
with qtbot.waitSignal(app.worker.finished, timeout=10000) as blocker:
blocker.connect(app.worker.failed) # Can add other signals to blocker
app.worker.start()
# Test will wait here until either signal is emitted, or 10 seconds has elapsed
assert blocker.signal_triggered # Assuming the work took less than 10 seconds
assert_application_results(app)Many thanks to @jdreaver for discussion and complete PR! (12, 13)
Added
stopas an alias forstopForInteraction(10, thanks @itghisi)Now exceptions raised in virtual methods make tests fail, instead of silently passing (11). If an exception is raised, the test will fail and it exceptions that happened inside virtual calls will be printed as such:
E Failed: Qt exceptions in virtual methods: E ________________________________________________________________________________ E File "x:\pytest-qt\pytestqt\_tests\test_exceptions.py", line 14, in event E raise ValueError('mistakes were made') E E ValueError: mistakes were made E ________________________________________________________________________________ E File "x:\pytest-qt\pytestqt\_tests\test_exceptions.py", line 14, in event E raise ValueError('mistakes were made') E E ValueError: mistakes were made E ________________________________________________________________________________Thanks to @jdreaver for request and sample code!
Fixed documentation for
QtBot: it was not being rendered in the docs due to an import error.
Python 3 support.
Minor documentation fixes.
Small bug fix release.
First working version.