From c4272fd3e53a25c713a0fc0f41afaaafd29826d9 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 8 Mar 2020 02:15:15 +0100 Subject: [PATCH] SysCaptureBinary: decode in writeorg Fixes https://github.com/pytest-dev/pytest/issues/6871. --- src/_pytest/capture.py | 7 ++++++- testing/test_capture.py | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/_pytest/capture.py b/src/_pytest/capture.py index 50c74427670..ec503638b7d 100644 --- a/src/_pytest/capture.py +++ b/src/_pytest/capture.py @@ -687,7 +687,8 @@ def resume(self): setattr(sys, self.name, self.tmpfile) self._state = "resumed" - def writeorg(self, data): + def writeorg(self, data: str) -> None: + data = data.decode(self._old.encoding) self._old.write(data) self._old.flush() @@ -701,6 +702,10 @@ def snap(self): self.tmpfile.truncate() return res + def writeorg(self, data: str) -> None: + self._old.write(data) + self._old.flush() + class TeeSysCapture(SysCapture): def __init__(self, fd, tmpfile=None): diff --git a/testing/test_capture.py b/testing/test_capture.py index a6703c1c5db..67e5fcb7433 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -19,6 +19,7 @@ from _pytest.capture import CaptureResult from _pytest.capture import MultiCapture from _pytest.config import ExitCode +from _pytest.pytester import Testdir # note: py.io capture tests where copied from # pylib 1.4.20.dev2 (rev 13d9af95547e) @@ -62,6 +63,23 @@ def test_CaptureResult() -> None: assert str(cr) == repr_ +def test_capsysbinary(testdir: Testdir) -> None: + p1 = testdir.makepyfile( + """ + def test(capsysbinary): + print("hello") + assert False, "trigger_failure" + """ + ) + result = testdir.runpytest(str(p1)) + result.stdout.fnmatch_lines([ + 'E assert False', + '*- Captured stdout call -*', + 'hello', + ]) + assert result.ret == 1 + + class TestCaptureManager: def test_getmethod_default_no_fd(self, monkeypatch): from _pytest.capture import pytest_addoption