From b10ab0211c789ea28bb372fe93fffeeb8cd338ad Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 1 Feb 2020 22:06:15 +0100 Subject: [PATCH] Use TypeError instead of AssertionError for no sequence Improve/extends tests. --- src/_pytest/pytester.py | 3 ++- testing/test_pytester.py | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index cfe1b9a6c..82a7a7bbe 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -1430,7 +1430,8 @@ class LineMatcher: :param str match_nickname: the nickname for the match function that will be logged to stdout when a match occurs """ - assert isinstance(lines2, collections.abc.Sequence) + if not isinstance(lines2, collections.abc.Sequence): + raise TypeError("invalid type for lines2: {}".format(type(lines2).__name__)) lines2 = self._getlines(lines2) lines1 = self.lines[:] nextline = None diff --git a/testing/test_pytester.py b/testing/test_pytester.py index 2e61632fd..da5fb99a1 100644 --- a/testing/test_pytester.py +++ b/testing/test_pytester.py @@ -458,15 +458,26 @@ def test_testdir_run_timeout_expires(testdir) -> None: def test_linematcher_with_nonlist() -> None: """Test LineMatcher with regard to passing in a set (accidentally).""" + from _pytest._code.source import Source + lm = LineMatcher([]) - with pytest.raises(AssertionError): + with pytest.raises(TypeError, match="invalid type for lines2: set"): lm.fnmatch_lines(set()) - with pytest.raises(AssertionError): + with pytest.raises(TypeError, match="invalid type for lines2: dict"): lm.fnmatch_lines({}) + with pytest.raises(TypeError, match="invalid type for lines2: set"): + lm.re_match_lines(set()) + with pytest.raises(TypeError, match="invalid type for lines2: dict"): + lm.re_match_lines({}) + with pytest.raises(TypeError, match="invalid type for lines2: Source"): + lm.fnmatch_lines(Source()) lm.fnmatch_lines([]) lm.fnmatch_lines(()) + lm.fnmatch_lines("") assert lm._getlines({}) == {} assert lm._getlines(set()) == set() + assert lm._getlines(Source()) == [] + assert lm._getlines(Source("pass\npass")) == ["pass", "pass"] def test_linematcher_match_failure() -> None: