Skip to content

Commit 51c341f

Browse files
committed
Mark no_qt_log to disable logging per-test
Fix #56
1 parent 46961ed commit 51c341f

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

pytestqt/_tests/test_logging.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,35 @@ def test_1(qtlog):
119119
res.assertoutcome(passed=passed, failed=int(not passed))
120120

121121

122+
@pytest.mark.parametrize('use_mark', [True, False])
123+
def test_disable_qtlog_mark(testdir, use_mark):
124+
"""
125+
Test mark which disables logging capture for a test.
126+
127+
:type testdir: _pytest.pytester.TmpTestdir
128+
"""
129+
testdir.makeini(
130+
"""
131+
[pytest]
132+
qt_log_level_fail = CRITICAL
133+
"""
134+
)
135+
mark = '@pytest.mark.no_qt_log' if use_mark else ''
136+
137+
testdir.makepyfile(
138+
"""
139+
from pytestqt.qt_compat import qCritical
140+
import pytest
141+
{mark}
142+
def test_1():
143+
qCritical('message')
144+
""".format(mark=mark)
145+
)
146+
res = testdir.inline_run()
147+
passed = 1 if use_mark else 0
148+
res.assertoutcome(passed=passed, failed=int(not passed))
149+
150+
122151
def test_logging_formatting(testdir):
123152
"""
124153
Test custom formatting for logging messages.

pytestqt/plugin.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,8 @@ def __init__(self, config):
631631
self.config = config
632632

633633
def pytest_runtest_setup(self, item):
634+
if item.get_marker('no_qt_log'):
635+
return
634636
m = item.get_marker('qt_log_ignore')
635637
if m:
636638
ignore_regexes = m.args
@@ -644,16 +646,18 @@ def pytest_runtest_makereport(self, item, call):
644646
"""Add captured Qt messages to test item report if the call failed."""
645647

646648
outcome = yield
647-
report = outcome.result
648-
649-
m = item.get_marker('qt_log_level_fail')
650-
if m:
651-
log_fail_level = m.args[0]
652-
else:
653-
log_fail_level = self.config.getini('qt_log_level_fail')
654-
assert log_fail_level in QtLoggingPlugin.LOG_FAIL_OPTIONS
649+
if not hasattr(item, 'qt_log_capture'):
650+
return
655651

656652
if call.when == 'call':
653+
report = outcome.result
654+
655+
m = item.get_marker('qt_log_level_fail')
656+
if m:
657+
log_fail_level = m.args[0]
658+
else:
659+
log_fail_level = self.config.getini('qt_log_level_fail')
660+
assert log_fail_level in QtLoggingPlugin.LOG_FAIL_OPTIONS
657661

658662
# make test fail if any records were captured which match
659663
# log_fail_level

0 commit comments

Comments
 (0)