Skip to content

Commit 40d237a

Browse files
committed
tests: Make qInfo tests work with PySide6 6.8.2+
qInfo() has been added: https://codereview.qt-project.org/c/pyside/pyside-setup/+/605100 Follow-up / alternative to #593 without breaking compatibility with older PySide6 versions. See #232.
1 parent a26733c commit 40d237a

File tree

2 files changed

+22
-33
lines changed

2 files changed

+22
-33
lines changed

src/pytestqt/qt_compat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def _import_module(module_name):
111111

112112
self._check_qt_api_version()
113113

114-
# qInfo is not exposed in PySide6 (#232)
114+
# qInfo is not exposed in PySide6 < 6.8.2 (#232)
115115
if hasattr(QtCore, "QMessageLogger"):
116116
self.qInfo = lambda msg: QtCore.QMessageLogger().info(msg)
117117
elif hasattr(QtCore, "qInfo"):

tests/test_logging.py

+21-32
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
from pytestqt.qt_compat import qt_api
66

77

8+
# qInfo is not exposed by PySide6 < 6.8.2 (#225)
9+
HAS_QINFO = qt_api.qInfo is not None
10+
11+
812
@pytest.mark.parametrize("test_succeeds", [True, False])
913
@pytest.mark.parametrize("qt_log", [True, False])
1014
def test_basic_logging(testdir, test_succeeds, qt_log):
@@ -14,7 +18,7 @@ def test_basic_logging(testdir, test_succeeds, qt_log):
1418
:type testdir: _pytest.pytester.TmpTestdir
1519
"""
1620
testdir.makepyfile(
17-
"""
21+
f"""
1822
import sys
1923
from pytestqt.qt_compat import qt_api
2024
@@ -26,15 +30,13 @@ def print_msg(msg_type, context, message):
2630
qt_api.QtCore.qInstallMessageHandler(print_msg)
2731
2832
def test_types():
29-
# qInfo is not exposed by the bindings yet (#225)
30-
# qt_api.qInfo('this is an INFO message')
33+
if {HAS_QINFO}:
34+
qt_api.qInfo('this is an INFO message')
3135
qt_api.qDebug('this is a DEBUG message')
3236
qt_api.qWarning('this is a WARNING message')
3337
qt_api.qCritical('this is a CRITICAL message')
34-
assert {}
35-
""".format(
36-
test_succeeds
37-
)
38+
assert {test_succeeds}
39+
"""
3840
)
3941
res = testdir.runpytest(*(["--no-qt-log"] if not qt_log else []))
4042
if test_succeeds:
@@ -45,8 +47,7 @@ def test_types():
4547
res.stdout.fnmatch_lines(
4648
[
4749
"*-- Captured Qt messages --*",
48-
# qInfo is not exposed by the bindings yet (#232)
49-
# '*QtInfoMsg: this is an INFO message*',
50+
*(["*QtInfoMsg: this is an INFO message*"] if HAS_QINFO else []),
5051
"*QtDebugMsg: this is a DEBUG message*",
5152
"*QtWarningMsg: this is a WARNING message*",
5253
"*QtCriticalMsg: this is a CRITICAL message*",
@@ -56,47 +57,35 @@ def test_types():
5657
res.stdout.fnmatch_lines(
5758
[
5859
"*-- Captured stderr call --*",
59-
# qInfo is not exposed by the bindings yet (#232)
60-
# '*QtInfoMsg: this is an INFO message*',
61-
# 'this is an INFO message*',
60+
*(["this is an INFO message*"] if HAS_QINFO else []),
6261
"this is a DEBUG message*",
6362
"this is a WARNING message*",
6463
"this is a CRITICAL message*",
6564
]
6665
)
6766

6867

69-
def test_qinfo(qtlog):
70-
"""Test INFO messages when we have means to do so. Should be temporary until bindings
71-
catch up and expose qInfo (or at least QMessageLogger), then we should update
72-
the other logging tests properly. #232
73-
"""
74-
75-
if qt_api.is_pyside:
76-
assert (
77-
qt_api.qInfo is None
78-
), "pyside6 does not expose qInfo. If it does, update this test."
79-
return
80-
81-
qt_api.qInfo("this is an INFO message")
82-
records = [(m.type, m.message.strip()) for m in qtlog.records]
83-
assert records == [(qt_api.QtCore.QtMsgType.QtInfoMsg, "this is an INFO message")]
84-
85-
8668
def test_qtlog_fixture(qtlog):
8769
"""
8870
Test qtlog fixture.
8971
"""
90-
# qInfo is not exposed by the bindings yet (#232)
72+
expected = []
73+
if HAS_QINFO:
74+
qt_api.qInfo("this is an INFO message")
75+
expected.append((qt_api.QtCore.QtMsgType.QtInfoMsg, "this is an INFO message"))
76+
9177
qt_api.qDebug("this is a DEBUG message")
9278
qt_api.qWarning("this is a WARNING message")
9379
qt_api.qCritical("this is a CRITICAL message")
94-
records = [(m.type, m.message.strip()) for m in qtlog.records]
95-
assert records == [
80+
81+
expected += [
9682
(qt_api.QtCore.QtMsgType.QtDebugMsg, "this is a DEBUG message"),
9783
(qt_api.QtCore.QtMsgType.QtWarningMsg, "this is a WARNING message"),
9884
(qt_api.QtCore.QtMsgType.QtCriticalMsg, "this is a CRITICAL message"),
9985
]
86+
87+
records = [(m.type, m.message.strip()) for m in qtlog.records]
88+
assert records == expected
10089
# `records` attribute is read-only
10190
with pytest.raises(AttributeError):
10291
qtlog.records = []

0 commit comments

Comments
 (0)