Skip to content

Commit 81b5b66

Browse files
committed
Fix line number reported when failing by Qt logging
Fix #55
1 parent 7341315 commit 81b5b66

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

pytestqt/_tests/test_logging.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,27 @@ def test1():
291291
res = testdir.inline_run()
292292
passed = 1 if apply_mark else 0
293293
res.assertoutcome(passed=passed, failed=int(not passed))
294+
295+
296+
def test_logging_fails_lineno(testdir):
297+
"""
298+
Test that tests when failing because log messages were emitted report
299+
the correct line number.
300+
301+
:type testdir: _pytest.pytester.TmpTestdir
302+
"""
303+
testdir.makeini(
304+
"""
305+
[pytest]
306+
qt_log_level_fail = WARNING
307+
"""
308+
)
309+
testdir.makepyfile(
310+
"""
311+
from pytestqt.qt_compat import qWarning
312+
def test_foo():
313+
qWarning('this is a WARNING message')
314+
"""
315+
)
316+
res = testdir.runpytest()
317+
res.stdout.fnmatch_lines('*test_logging_fails_lineno.py:2: Failure:*')

pytestqt/plugin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,8 +799,9 @@ class _QtLogLevelErrorRepr(TerminalRepr):
799799

800800
def __init__(self, item, level):
801801
msg = 'Failure: Qt messages with level {0} or above emitted'
802-
path, line, _ = item.location
803-
self.fileloc = ReprFileLocation(path, line, msg.format(level.upper()))
802+
path, line_index, _ = item.location
803+
self.fileloc = ReprFileLocation(path, lineno=line_index + 1,
804+
message=msg.format(level.upper()))
804805
self.sections = []
805806

806807
def addsection(self, name, content, sep="-"):

0 commit comments

Comments
 (0)