Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test and restore compatibility with older PySide/PyQt versions #597

Merged
merged 6 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ jobs:
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
qt-lib: [pyqt5, pyqt6, pyside6]
os: [ubuntu-latest, windows-latest, macos-latest]
include:
- python-version: "3.9"
qt-lib: pyqt61
os: ubuntu-latest
- python-version: "3.9"
qt-lib: pyside60
os: ubuntu-latest

steps:
- uses: actions/checkout@v4
Expand Down
11 changes: 9 additions & 2 deletions src/pytestqt/qt_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


def _import(name):
"""Think call so we can mock it during testing"""
"""Thin call so we can mock it during testing"""
return __import__(name)


Expand Down Expand Up @@ -111,7 +111,14 @@ def _import_module(module_name):

self._check_qt_api_version()

self.qInfo = QtCore.qInfo
# qInfo is not exposed in PySide6 < 6.8.2 (#232)
if hasattr(QtCore, "qInfo"):
self.qInfo = QtCore.qInfo
elif hasattr(QtCore, "QMessageLogger"):
self.qInfo = lambda msg: QtCore.QMessageLogger().info(msg)
else:
self.qInfo = None

self.qDebug = QtCore.qDebug
self.qWarning = QtCore.qWarning
self.qCritical = QtCore.qCritical
Expand Down
20 changes: 6 additions & 14 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,20 +309,13 @@ def test_events(events_queue, fix, i):
res.stdout.fnmatch_lines(["*3 passed in*"])


def test_header(testdir):
testdir.makeconftest(
"""
from pytestqt import qt_compat
from pytestqt.qt_compat import qt_api

def mock_get_versions():
return qt_compat.VersionTuple('PyQtAPI', '1.0', '2.5', '3.5')

assert hasattr(qt_api, 'get_versions')
qt_api.get_versions = mock_get_versions
"""
def test_header(testdir, monkeypatch):
monkeypatch.setattr(
qt_api,
"get_versions",
lambda: qt_compat.VersionTuple("PyQtAPI", "1.0", "2.5", "3.5"),
)
res = testdir.runpytest()
res = testdir.runpytest_inprocess()
res.stdout.fnmatch_lines(
["*test session starts*", "PyQtAPI 1.0 -- Qt runtime 2.5 -- Qt compiled 3.5"]
)
Expand Down Expand Up @@ -613,7 +606,6 @@ class Mock:
qtcore = Mock()
for method_name in (
"qInstallMessageHandler",
"qInfo",
"qDebug",
"qWarning",
"qCritical",
Expand Down
11 changes: 9 additions & 2 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
from pytestqt.exceptions import capture_exceptions, format_captured_exceptions
from pytestqt.qt_compat import qt_api


def has_pyside6_exception_capture():
return qt_api.pytest_qt_api == "pyside6" and tuple(
int(part) for part in qt_api.get_versions().qt_api_version.split(".")
) >= (6, 5, 2)


# PySide6 is automatically captures exceptions during the event loop,
# and re-raises them when control gets back to Python, so the related
# functionality does not work, nor is needed for the end user.
exception_capture_pyside6 = pytest.mark.skipif(
qt_api.pytest_qt_api == "pyside6",
has_pyside6_exception_capture(),
reason="pytest-qt capture not working/needed on PySide6",
)

Expand Down Expand Up @@ -51,7 +58,7 @@ def test_exceptions(qtbot):
)
result = testdir.runpytest()
if raise_error:
if qt_api.pytest_qt_api == "pyside6":
if has_pyside6_exception_capture():
# PySide6 automatically captures exceptions during the event loop,
# and re-raises them when control gets back to Python.
# This results in the exception not being captured by
Expand Down
33 changes: 21 additions & 12 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from pytestqt.qt_compat import qt_api


# qInfo is not exposed by PySide6 < 6.8.2 (#225)
HAS_QINFO = qt_api.qInfo is not None


@pytest.mark.parametrize("test_succeeds", [True, False])
@pytest.mark.parametrize("qt_log", [True, False])
def test_basic_logging(testdir, test_succeeds, qt_log):
Expand All @@ -14,7 +18,7 @@ def test_basic_logging(testdir, test_succeeds, qt_log):
:type testdir: _pytest.pytester.TmpTestdir
"""
testdir.makepyfile(
"""
f"""
import sys
from pytestqt.qt_compat import qt_api

Expand All @@ -26,14 +30,13 @@ def print_msg(msg_type, context, message):
qt_api.QtCore.qInstallMessageHandler(print_msg)

def test_types():
qt_api.qInfo('this is an INFO message')
if {HAS_QINFO}:
qt_api.qInfo('this is an INFO message')
qt_api.qDebug('this is a DEBUG message')
qt_api.qWarning('this is a WARNING message')
qt_api.qCritical('this is a CRITICAL message')
assert {}
""".format(
test_succeeds
)
assert {test_succeeds}
"""
)
res = testdir.runpytest(*(["--no-qt-log"] if not qt_log else []))
if test_succeeds:
Expand All @@ -44,7 +47,7 @@ def test_types():
res.stdout.fnmatch_lines(
[
"*-- Captured Qt messages --*",
"*QtInfoMsg: this is an INFO message*",
*(["*QtInfoMsg: this is an INFO message*"] if HAS_QINFO else []),
"*QtDebugMsg: this is a DEBUG message*",
"*QtWarningMsg: this is a WARNING message*",
"*QtCriticalMsg: this is a CRITICAL message*",
Expand All @@ -54,7 +57,7 @@ def test_types():
res.stdout.fnmatch_lines(
[
"*-- Captured stderr call --*",
"this is an INFO message*",
*(["this is an INFO message*"] if HAS_QINFO else []),
"this is a DEBUG message*",
"this is a WARNING message*",
"this is a CRITICAL message*",
Expand All @@ -66,17 +69,23 @@ def test_qtlog_fixture(qtlog):
"""
Test qtlog fixture.
"""
qt_api.qInfo("this is an INFO message")
expected = []
if HAS_QINFO:
qt_api.qInfo("this is an INFO message")
expected.append((qt_api.QtCore.QtMsgType.QtInfoMsg, "this is an INFO message"))

qt_api.qDebug("this is a DEBUG message")
qt_api.qWarning("this is a WARNING message")
qt_api.qCritical("this is a CRITICAL message")
records = [(m.type, m.message.strip()) for m in qtlog.records]
assert records == [
(qt_api.QtCore.QtMsgType.QtInfoMsg, "this is an INFO message"),

expected += [
(qt_api.QtCore.QtMsgType.QtDebugMsg, "this is a DEBUG message"),
(qt_api.QtCore.QtMsgType.QtWarningMsg, "this is a WARNING message"),
(qt_api.QtCore.QtMsgType.QtCriticalMsg, "this is a CRITICAL message"),
]

records = [(m.type, m.message.strip()) for m in qtlog.records]
assert records == expected
# `records` attribute is read-only
with pytest.raises(AttributeError):
qtlog.records = []
Expand Down
5 changes: 4 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
[tox]
envlist = py{39,310,311,312,313}-{pyqt5,pyside6,pyqt6}
envlist = py{39,310,311,312,313}-{pyqt5,pyside6,pyqt6},py39-pyside60,py39-pyqt61

[testenv]
deps=
pytest
pyside6: pyside6
pyside60: pyside6<6.1
pyqt5: pyqt5
pyqt6: pyqt6
pyqt61: pyqt6<6.2
pyqt61: pyqt6-sip<13.6
commands=
pytest --color=yes {posargs}
setenv=
Expand Down