Skip to content

Commit aade7ed

Browse files
authored
pytester: reset log output in _match_lines (#70)
This is necessary for when using e.g. `no_fnmatch_line` after it. Factor it out into `_fail`.
1 parent 017a0dd commit aade7ed

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

src/_pytest/pytester.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,10 +1421,10 @@ def _match_lines(self, lines2, match_func, match_nickname):
14211421
self._log("{:>{width}}".format("and:", width=wnick), repr(nextline))
14221422
extralines.append(nextline)
14231423
else:
1424-
self._log("remains unmatched: {!r}".format(line))
1425-
pytest.fail(
1426-
self._log_text, short_msg="remains unmatched: {!r}".format(line)
1427-
)
1424+
msg = "remains unmatched: {!r}".format(line)
1425+
self._log(msg)
1426+
self._fail(msg)
1427+
self._log_output = []
14281428

14291429
def no_fnmatch_line(self, pat):
14301430
"""Ensure captured lines do not match the given pattern, using ``fnmatch.fnmatch``.
@@ -1450,18 +1450,20 @@ def _no_match_line(self, pat, match_func, match_nickname):
14501450
__tracebackhide__ = True
14511451
nomatch_printed = False
14521452
wnick = len(match_nickname) + 1
1453-
try:
1454-
for line in self.lines:
1455-
if match_func(line, pat):
1456-
self._log("%s:" % match_nickname, repr(pat))
1457-
self._log("{:>{width}}".format("with:", width=wnick), repr(line))
1458-
pytest.fail(self._log_text.lstrip())
1459-
else:
1460-
if not nomatch_printed:
1461-
self._log(
1462-
"{:>{width}}".format("nomatch:", width=wnick), repr(pat)
1463-
)
1464-
nomatch_printed = True
1465-
self._log("{:>{width}}".format("and:", width=wnick), repr(line))
1466-
finally:
1467-
self._log_output = []
1453+
for line in self.lines:
1454+
if match_func(line, pat):
1455+
msg = "{}: {!r}".format(match_nickname, pat)
1456+
self._log(msg)
1457+
self._log("{:>{width}}".format("with:", width=wnick), repr(line))
1458+
self._fail(msg)
1459+
else:
1460+
if not nomatch_printed:
1461+
self._log("{:>{width}}".format("nomatch:", width=wnick), repr(pat))
1462+
nomatch_printed = True
1463+
self._log("{:>{width}}".format("and:", width=wnick), repr(line))
1464+
self._log_output = []
1465+
1466+
def _fail(self, msg):
1467+
log_text = self._log_text
1468+
self._log_output = []
1469+
pytest.fail(log_text, short_msg=msg)

testing/test_pytester.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ def test_no_matching(function):
545545
]
546546
else:
547547
assert obtained == [
548-
"nomatch: '{}'".format(good_pattern),
548+
" nomatch: '{}'".format(good_pattern),
549549
" and: 'cachedir: .pytest_cache'",
550550
" and: 'collecting ... collected 1 item'",
551551
" and: ''",
@@ -557,6 +557,14 @@ def test_no_matching(function):
557557
func(bad_pattern) # bad pattern does not match any line: passes
558558

559559

560+
def test_no_matching_after_match():
561+
lm = LineMatcher(["1", "2", "3"])
562+
lm.fnmatch_lines(["1", "3"])
563+
with pytest.raises(pytest.fail.Exception) as e:
564+
lm.no_fnmatch_line("*")
565+
assert str(e.value).splitlines() == ["fnmatch: '*'", " with: '1'"]
566+
567+
560568
def test_pytester_addopts(request, monkeypatch):
561569
monkeypatch.setenv("PYTEST_ADDOPTS", "--orig-unused")
562570

0 commit comments

Comments
 (0)