Skip to content

Commit b1bed1c

Browse files
committed
pytester: LineMatcher: assert Sequence when matching in order
This can be helpful when passing a set accidentally.
1 parent 33d4c96 commit b1bed1c

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

changelog/4931.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytester's ``LineMatcher`` asserts that the passed lines are a sequence.

src/_pytest/pytester.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from __future__ import print_function
55

66
import codecs
7+
import collections
78
import gc
89
import os
910
import platform
@@ -1325,6 +1326,7 @@ def _match_lines(self, lines2, match_func, match_nickname):
13251326
will be logged to stdout when a match occurs
13261327
13271328
"""
1329+
assert isinstance(lines2, collections.abc.Sequence)
13281330
lines2 = self._getlines(lines2)
13291331
lines1 = self.lines[:]
13301332
nextline = None

testing/test_pytester.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from _pytest.main import EXIT_TESTSFAILED
1818
from _pytest.pytester import CwdSnapshot
1919
from _pytest.pytester import HookRecorder
20+
from _pytest.pytester import LineMatcher
2021
from _pytest.pytester import SysModulesSnapshot
2122
from _pytest.pytester import SysPathsSnapshot
2223

@@ -453,3 +454,15 @@ def test_timeout():
453454
)
454455
with pytest.raises(testdir.TimeoutExpired):
455456
testdir.runpytest_subprocess(testfile, timeout=1)
457+
458+
459+
def test_linematcher_with_set():
460+
"""Test LineMatcher with regard to passing in a set (accidentally)."""
461+
lm = LineMatcher([])
462+
463+
with pytest.raises(AssertionError):
464+
lm.fnmatch_lines({})
465+
lm.fnmatch_lines([])
466+
lm.fnmatch_lines(())
467+
468+
assert lm._getlines({}) == {}

0 commit comments

Comments
 (0)