Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add str() support to LineMatcher #8050

Merged
merged 8 commits into from
Nov 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/1265.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added an ``__str__`` implementation to the :class:`~pytest.pytester.LineMatcher` class which is returned from ``pytester.run_pytest().stdout`` and similar. It returns the entire output, like the existing ``str()`` method.
1 change: 1 addition & 0 deletions doc/en/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ To use it, include in your topmost ``conftest.py`` file:

.. autoclass:: LineMatcher()
:members:
:special-members: __str__

.. autoclass:: HookRecorder()
:members:
Expand Down
12 changes: 10 additions & 2 deletions src/_pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ def __init__(
self.stdout = LineMatcher(outlines)
""":class:`LineMatcher` of stdout.

Use e.g. :func:`stdout.str() <LineMatcher.str()>` to reconstruct stdout, or the commonly used
Use e.g. :func:`str(stdout) <LineMatcher.__str__()>` to reconstruct stdout, or the commonly used
:func:`stdout.fnmatch_lines() <LineMatcher.fnmatch_lines()>` method.
"""
self.stderr = LineMatcher(errlines)
Expand Down Expand Up @@ -1707,6 +1707,14 @@ def __init__(self, lines: List[str]) -> None:
self.lines = lines
self._log_output: List[str] = []

def __str__(self) -> str:
"""Return the entire original text.

.. versionadded:: 6.2
You can use :meth:`str` in older versions.
"""
return "\n".join(self.lines)

def _getlines(self, lines2: Union[str, Sequence[str], Source]) -> Sequence[str]:
if isinstance(lines2, str):
lines2 = Source(lines2)
Expand Down Expand Up @@ -1908,4 +1916,4 @@ def _fail(self, msg: str) -> None:

def str(self) -> str:
"""Return the entire original text."""
return "\n".join(self.lines)
return str(self)
5 changes: 5 additions & 0 deletions testing/test_pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,11 @@ def test_linematcher_no_matching_after_match() -> None:
assert str(e.value).splitlines() == ["fnmatch: '*'", " with: '1'"]


def test_linematcher_string_api() -> None:
lm = LineMatcher(["foo", "bar"])
assert str(lm) == "foo\nbar"


def test_pytester_addopts_before_testdir(request, monkeypatch) -> None:
orig = os.environ.get("PYTEST_ADDOPTS", None)
monkeypatch.setenv("PYTEST_ADDOPTS", "--orig-unused")
Expand Down