diff options
Diffstat (limited to 'testing/test_capture.py')
-rw-r--r-- | testing/test_capture.py | 388 |
1 files changed, 201 insertions, 187 deletions
diff --git a/testing/test_capture.py b/testing/test_capture.py index e6bbc9a5d..3a5c617fe 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -13,11 +13,13 @@ from typing import TextIO import pytest from _pytest import capture from _pytest.capture import _get_multicapture +from _pytest.capture import CaptureFixture from _pytest.capture import CaptureManager from _pytest.capture import CaptureResult from _pytest.capture import MultiCapture from _pytest.config import ExitCode -from _pytest.pytester import Testdir +from _pytest.monkeypatch import MonkeyPatch +from _pytest.pytester import Pytester # note: py.io capture tests where copied from # pylib 1.4.20.dev2 (rev 13d9af95547e) @@ -55,7 +57,7 @@ def TeeStdCapture( class TestCaptureManager: @pytest.mark.parametrize("method", ["no", "sys", "fd"]) - def test_capturing_basic_api(self, method): + def test_capturing_basic_api(self, method) -> None: capouter = StdCaptureFD() old = sys.stdout, sys.stderr, sys.stdin try: @@ -96,9 +98,9 @@ class TestCaptureManager: @pytest.mark.parametrize("method", ["fd", "sys"]) -def test_capturing_unicode(testdir, method): +def test_capturing_unicode(pytester: Pytester, method: str) -> None: obj = "'b\u00f6y'" - testdir.makepyfile( + pytester.makepyfile( """\ # taken from issue 227 from nosetests def test_unicode(): @@ -108,24 +110,24 @@ def test_capturing_unicode(testdir, method): """ % obj ) - result = testdir.runpytest("--capture=%s" % method) + result = pytester.runpytest("--capture=%s" % method) result.stdout.fnmatch_lines(["*1 passed*"]) @pytest.mark.parametrize("method", ["fd", "sys"]) -def test_capturing_bytes_in_utf8_encoding(testdir, method): - testdir.makepyfile( +def test_capturing_bytes_in_utf8_encoding(pytester: Pytester, method: str) -> None: + pytester.makepyfile( """\ def test_unicode(): print('b\\u00f6y') """ ) - result = testdir.runpytest("--capture=%s" % method) + result = pytester.runpytest("--capture=%s" % method) result.stdout.fnmatch_lines(["*1 passed*"]) -def test_collect_capturing(testdir): - p = testdir.makepyfile( +def test_collect_capturing(pytester: Pytester) -> None: + p = pytester.makepyfile( """ import sys @@ -134,7 +136,7 @@ def test_collect_capturing(testdir): import xyz42123 """ ) - result = testdir.runpytest(p) + result = pytester.runpytest(p) result.stdout.fnmatch_lines( [ "*Captured stdout*", @@ -146,8 +148,8 @@ def test_collect_capturing(testdir): class TestPerTestCapturing: - def test_capture_and_fixtures(self, testdir): - p = testdir.makepyfile( + def test_capture_and_fixtures(self, pytester: Pytester) -> None: + p = pytester.makepyfile( """ def setup_module(mod): print("setup module") @@ -161,7 +163,7 @@ class TestPerTestCapturing: assert 0 """ ) - result = testdir.runpytest(p) + result = pytester.runpytest(p) result.stdout.fnmatch_lines( [ "setup module*", @@ -173,8 +175,8 @@ class TestPerTestCapturing: ) @pytest.mark.xfail(reason="unimplemented feature") - def test_capture_scope_cache(self, testdir): - p = testdir.makepyfile( + def test_capture_scope_cache(self, pytester: Pytester) -> None: + p = pytester.makepyfile( """ import sys def setup_module(func): @@ -188,7 +190,7 @@ class TestPerTestCapturing: print("in teardown") """ ) - result = testdir.runpytest(p) + result = pytester.runpytest(p) result.stdout.fnmatch_lines( [ "*test_func():*", @@ -200,8 +202,8 @@ class TestPerTestCapturing: ] ) - def test_no_carry_over(self, testdir): - p = testdir.makepyfile( + def test_no_carry_over(self, pytester: Pytester) -> None: + p = pytester.makepyfile( """ def test_func1(): print("in func1") @@ -210,13 +212,13 @@ class TestPerTestCapturing: assert 0 """ ) - result = testdir.runpytest(p) + result = pytester.runpytest(p) s = result.stdout.str() assert "in func1" not in s assert "in func2" in s - def test_teardown_capturing(self, testdir): - p = testdir.makepyfile( + def test_teardown_capturing(self, pytester: Pytester) -> None: + p = pytester.makepyfile( """ def setup_function(function): print("setup func1") @@ -228,7 +230,7 @@ class TestPerTestCapturing: pass """ ) - result = testdir.runpytest(p) + result = pytester.runpytest(p) result.stdout.fnmatch_lines( [ "*teardown_function*", @@ -240,8 +242,8 @@ class TestPerTestCapturing: ] ) - def test_teardown_capturing_final(self, testdir): - p = testdir.makepyfile( + def test_teardown_capturing_final(self, pytester: Pytester) -> None: + p = pytester.makepyfile( """ def teardown_module(mod): print("teardown module") @@ -250,7 +252,7 @@ class TestPerTestCapturing: pass """ ) - result = testdir.runpytest(p) + result = pytester.runpytest(p) result.stdout.fnmatch_lines( [ "*def teardown_module(mod):*", @@ -260,8 +262,8 @@ class TestPerTestCapturing: ] ) - def test_capturing_outerr(self, testdir): - p1 = testdir.makepyfile( + def test_capturing_outerr(self, pytester: Pytester) -> None: + p1 = pytester.makepyfile( """\ import sys def test_capturing(): @@ -273,7 +275,7 @@ class TestPerTestCapturing: raise ValueError """ ) - result = testdir.runpytest(p1) + result = pytester.runpytest(p1) result.stdout.fnmatch_lines( [ "*test_capturing_outerr.py .F*", @@ -289,8 +291,8 @@ class TestPerTestCapturing: class TestLoggingInteraction: - def test_logging_stream_ownership(self, testdir): - p = testdir.makepyfile( + def test_logging_stream_ownership(self, pytester: Pytester) -> None: + p = pytester.makepyfile( """\ def test_logging(): import logging @@ -300,11 +302,11 @@ class TestLoggingInteraction: stream.close() # to free memory/release resources """ ) - result = testdir.runpytest_subprocess(p) + result = pytester.runpytest_subprocess(p) assert result.stderr.str().find("atexit") == -1 - def test_logging_and_immediate_setupteardown(self, testdir): - p = testdir.makepyfile( + def test_logging_and_immediate_setupteardown(self, pytester: Pytester) -> None: + p = pytester.makepyfile( """\ import logging def setup_function(function): @@ -321,7 +323,7 @@ class TestLoggingInteraction: ) for optargs in (("--capture=sys",), ("--capture=fd",)): print(optargs) - result = testdir.runpytest_subprocess(p, *optargs) + result = pytester.runpytest_subprocess(p, *optargs) s = result.stdout.str() result.stdout.fnmatch_lines( ["*WARN*hello3", "*WARN*hello1", "*WARN*hello2"] # errors show first! @@ -329,8 +331,8 @@ class TestLoggingInteraction: # verify proper termination assert "closed" not in s - def test_logging_and_crossscope_fixtures(self, testdir): - p = testdir.makepyfile( + def test_logging_and_crossscope_fixtures(self, pytester: Pytester) -> None: + p = pytester.makepyfile( """\ import logging def setup_module(function): @@ -347,7 +349,7 @@ class TestLoggingInteraction: ) for optargs in (("--capture=sys",), ("--capture=fd",)): print(optargs) - result = testdir.runpytest_subprocess(p, *optargs) + result = pytester.runpytest_subprocess(p, *optargs) s = result.stdout.str() result.stdout.fnmatch_lines( ["*WARN*hello3", "*WARN*hello1", "*WARN*hello2"] # errors come first @@ -355,8 +357,8 @@ class TestLoggingInteraction: # verify proper termination assert "closed" not in s - def test_conftestlogging_is_shown(self, testdir): - testdir.makeconftest( + def test_conftestlogging_is_shown(self, pytester: Pytester) -> None: + pytester.makeconftest( """\ import logging logging.basicConfig() @@ -364,20 +366,20 @@ class TestLoggingInteraction: """ ) # make sure that logging is still captured in tests - result = testdir.runpytest_subprocess("-s", "-p", "no:capturelog") + result = pytester.runpytest_subprocess("-s", "-p", "no:capturelog") assert result.ret == ExitCode.NO_TESTS_COLLECTED result.stderr.fnmatch_lines(["WARNING*hello435*"]) assert "operation on closed file" not in result.stderr.str() - def test_conftestlogging_and_test_logging(self, testdir): - testdir.makeconftest( + def test_conftestlogging_and_test_logging(self, pytester: Pytester) -> None: + pytester.makeconftest( """\ import logging logging.basicConfig() """ ) # make sure that logging is still captured in tests - p = testdir.makepyfile( + p = pytester.makepyfile( """\ def test_hello(): import logging @@ -385,14 +387,14 @@ class TestLoggingInteraction: assert 0 """ ) - result = testdir.runpytest_subprocess(p, "-p", "no:capturelog") + result = pytester.runpytest_subprocess(p, "-p", "no:capturelog") assert result.ret != 0 result.stdout.fnmatch_lines(["WARNING*hello433*"]) assert "something" not in result.stderr.str() assert "operation on closed file" not in result.stderr.str() - def test_logging_after_cap_stopped(self, testdir): - testdir.makeconftest( + def test_logging_after_cap_stopped(self, pytester: Pytester) -> None: + pytester.makeconftest( """\ import pytest import logging @@ -406,7 +408,7 @@ class TestLoggingInteraction: """ ) # make sure that logging is still captured in tests - p = testdir.makepyfile( + p = pytester.makepyfile( """\ def test_hello(log_on_teardown): import logging @@ -415,7 +417,7 @@ class TestLoggingInteraction: raise KeyboardInterrupt() """ ) - result = testdir.runpytest_subprocess(p, "--log-cli-level", "info") + result = pytester.runpytest_subprocess(p, "--log-cli-level", "info") assert result.ret != 0 result.stdout.fnmatch_lines( ["*WARNING*hello433*", "*WARNING*Logging on teardown*"] @@ -428,8 +430,8 @@ class TestLoggingInteraction: class TestCaptureFixture: @pytest.mark.parametrize("opt", [[], ["-s"]]) - def test_std_functional(self, testdir, opt): - reprec = testdir.inline_runsource( + def test_std_functional(self, pytester: Pytester, opt) -> None: + reprec = pytester.inline_runsource( """\ def test_hello(capsys): print(42) @@ -440,8 +442,8 @@ class TestCaptureFixture: ) reprec.assertoutcome(passed=1) - def test_capsyscapfd(self, testdir): - p = testdir.makepyfile( + def test_capsyscapfd(self, pytester: Pytester) -> None: + p = pytester.makepyfile( """\ def test_one(capsys, capfd): pass @@ -449,7 +451,7 @@ class TestCaptureFixture: pass """ ) - result = testdir.runpytest(p) + result = pytester.runpytest(p) result.stdout.fnmatch_lines( [ "*ERROR*setup*test_one*", @@ -460,11 +462,11 @@ class TestCaptureFixture: ] ) - def test_capturing_getfixturevalue(self, testdir): + def test_capturing_getfixturevalue(self, pytester: Pytester) -> None: """Test that asking for "capfd" and "capsys" using request.getfixturevalue in the same test is an error. """ - testdir.makepyfile( + pytester.makepyfile( """\ def test_one(capsys, request): request.getfixturevalue("capfd") @@ -472,7 +474,7 @@ class TestCaptureFixture: request.getfixturevalue("capsys") """ ) - result = testdir.runpytest() + result = pytester.runpytest() result.stdout.fnmatch_lines( [ "*test_one*", @@ -483,21 +485,23 @@ class TestCaptureFixture: ] ) - def test_capsyscapfdbinary(self, testdir): - p = testdir.makepyfile( + def test_capsyscapfdbinary(self, pytester: Pytester) -> None: + p = pytester.makepyfile( """\ def test_one(capsys, capfdbinary): pass """ ) - result = testdir.runpytest(p) + result = pytester.runpytest(p) result.stdout.fnmatch_lines( ["*ERROR*setup*test_one*", "E*capfdbinary*capsys*same*time*", "*1 error*"] ) @pytest.mark.parametrize("method", ["sys", "fd"]) - def test_capture_is_represented_on_failure_issue128(self, testdir, method): - p = testdir.makepyfile( + def test_capture_is_represented_on_failure_issue128( + self, pytester: Pytester, method + ) -> None: + p = pytester.makepyfile( """\ def test_hello(cap{}): print("xxx42xxx") @@ -506,11 +510,11 @@ class TestCaptureFixture: method ) ) - result = testdir.runpytest(p) + result = pytester.runpytest(p) result.stdout.fnmatch_lines(["xxx42xxx"]) - def test_stdfd_functional(self, testdir): - reprec = testdir.inline_runsource( + def test_stdfd_functional(self, pytester: Pytester) -> None: + reprec = pytester.inline_runsource( """\ def test_hello(capfd): import os @@ -523,13 +527,13 @@ class TestCaptureFixture: reprec.assertoutcome(passed=1) @pytest.mark.parametrize("nl", ("\n", "\r\n", "\r")) - def test_cafd_preserves_newlines(self, capfd, nl): + def test_cafd_preserves_newlines(self, capfd, nl) -> None: print("test", end=nl) out, err = capfd.readouterr() assert out.endswith(nl) - def test_capfdbinary(self, testdir): - reprec = testdir.inline_runsource( + def test_capfdbinary(self, pytester: Pytester) -> None: + reprec = pytester.inline_runsource( """\ def test_hello(capfdbinary): import os @@ -542,8 +546,8 @@ class TestCaptureFixture: ) reprec.assertoutcome(passed=1) - def test_capsysbinary(self, testdir): - p1 = testdir.makepyfile( + def test_capsysbinary(self, pytester: Pytester) -> None: + p1 = pytester.makepyfile( r""" def test_hello(capsysbinary): import sys @@ -567,7 +571,7 @@ class TestCaptureFixture: print("stderr after", file=sys.stderr) """ ) - result = testdir.runpytest(str(p1), "-rA") + result = pytester.runpytest(str(p1), "-rA") result.stdout.fnmatch_lines( [ "*- Captured stdout call -*", @@ -578,18 +582,18 @@ class TestCaptureFixture: ] ) - def test_partial_setup_failure(self, testdir): - p = testdir.makepyfile( + def test_partial_setup_failure(self, pytester: Pytester) -> None: + p = pytester.makepyfile( """\ def test_hello(capsys, missingarg): pass """ ) - result = testdir.runpytest(p) + result = pytester.runpytest(p) result.stdout.fnmatch_lines(["*test_partial_setup_failure*", "*1 error*"]) - def test_keyboardinterrupt_disables_capturing(self, testdir): - p = testdir.makepyfile( + def test_keyboardinterrupt_disables_capturing(self, pytester: Pytester) -> None: + p = pytester.makepyfile( """\ def test_hello(capfd): import os @@ -597,26 +601,28 @@ class TestCaptureFixture: raise KeyboardInterrupt() """ ) - result = testdir.runpytest_subprocess(p) + result = pytester.runpytest_subprocess(p) result.stdout.fnmatch_lines(["*KeyboardInterrupt*"]) assert result.ret == 2 - def test_capture_and_logging(self, testdir): + def test_capture_and_logging(self, pytester: Pytester) -> None: """#14""" - p = testdir.makepyfile( + p = pytester.makepyfile( """\ import logging def test_log(capsys): logging.error('x') """ ) - result = testdir.runpytest_subprocess(p) + result = pytester.runpytest_subprocess(p) assert "closed" not in result.stderr.str() @pytest.mark.parametrize("fixture", ["capsys", "capfd"]) @pytest.mark.parametrize("no_capture", [True, False]) - def test_disabled_capture_fixture(self, testdir, fixture, no_capture): - testdir.makepyfile( + def test_disabled_capture_fixture( + self, pytester: Pytester, fixture: str, no_capture: bool + ) -> None: + pytester.makepyfile( """\ def test_disabled({fixture}): print('captured before') @@ -632,7 +638,7 @@ class TestCaptureFixture: ) ) args = ("-s",) if no_capture else () - result = testdir.runpytest_subprocess(*args) + result = pytester.runpytest_subprocess(*args) result.stdout.fnmatch_lines(["*while capture is disabled*", "*= 2 passed in *"]) result.stdout.no_fnmatch_line("*captured before*") result.stdout.no_fnmatch_line("*captured after*") @@ -641,12 +647,12 @@ class TestCaptureFixture: else: result.stdout.no_fnmatch_line("*test_normal executed*") - def test_disabled_capture_fixture_twice(self, testdir: Testdir) -> None: + def test_disabled_capture_fixture_twice(self, pytester: Pytester) -> None: """Test that an inner disabled() exit doesn't undo an outer disabled(). Issue #7148. """ - testdir.makepyfile( + pytester.makepyfile( """ def test_disabled(capfd): print('captured before') @@ -659,7 +665,7 @@ class TestCaptureFixture: assert capfd.readouterr() == ('captured before\\ncaptured after\\n', '') """ ) - result = testdir.runpytest_subprocess() + result = pytester.runpytest_subprocess() result.stdout.fnmatch_lines( [ "*while capture is disabled 1", @@ -670,10 +676,10 @@ class TestCaptureFixture: ) @pytest.mark.parametrize("fixture", ["capsys", "capfd"]) - def test_fixture_use_by_other_fixtures(self, testdir, fixture): + def test_fixture_use_by_other_fixtures(self, pytester: Pytester, fixture) -> None: """Ensure that capsys and capfd can be used by other fixtures during setup and teardown.""" - testdir.makepyfile( + pytester.makepyfile( """\ import sys import pytest @@ -700,15 +706,17 @@ class TestCaptureFixture: fixture=fixture ) ) - result = testdir.runpytest_subprocess() + result = pytester.runpytest_subprocess() result.stdout.fnmatch_lines(["*1 passed*"]) result.stdout.no_fnmatch_line("*stdout contents begin*") result.stdout.no_fnmatch_line("*stderr contents begin*") @pytest.mark.parametrize("cap", ["capsys", "capfd"]) - def test_fixture_use_by_other_fixtures_teardown(self, testdir, cap): + def test_fixture_use_by_other_fixtures_teardown( + self, pytester: Pytester, cap + ) -> None: """Ensure we can access setup and teardown buffers from teardown when using capsys/capfd (##3033)""" - testdir.makepyfile( + pytester.makepyfile( """\ import sys import pytest @@ -730,13 +738,13 @@ class TestCaptureFixture: cap=cap ) ) - reprec = testdir.inline_run() + reprec = pytester.inline_run() reprec.assertoutcome(passed=1) -def test_setup_failure_does_not_kill_capturing(testdir): - sub1 = testdir.mkpydir("sub1") - sub1.join("conftest.py").write( +def test_setup_failure_does_not_kill_capturing(pytester: Pytester) -> None: + sub1 = pytester.mkpydir("sub1") + sub1.joinpath("conftest.py").write_text( textwrap.dedent( """\ def pytest_runtest_setup(item): @@ -744,26 +752,26 @@ def test_setup_failure_does_not_kill_capturing(testdir): """ ) ) - sub1.join("test_mod.py").write("def test_func1(): pass") - result = testdir.runpytest(testdir.tmpdir, "--traceconfig") + sub1.joinpath("test_mod.py").write_text("def test_func1(): pass") + result = pytester.runpytest(pytester.path, "--traceconfig") result.stdout.fnmatch_lines(["*ValueError(42)*", "*1 error*"]) -def test_capture_conftest_runtest_setup(testdir): - testdir.makeconftest( +def test_capture_conftest_runtest_setup(pytester: Pytester) -> None: + pytester.makeconftest( """ def pytest_runtest_setup(): print("hello19") """ ) - testdir.makepyfile("def test_func(): pass") - result = testdir.runpytest() + pytester.makepyfile("def test_func(): pass") + result = pytester.runpytest() assert result.ret == 0 result.stdout.no_fnmatch_line("*hello19*") -def test_capture_badoutput_issue412(testdir): - testdir.makepyfile( +def test_capture_badoutput_issue412(pytester: Pytester) -> None: + pytester.makepyfile( """ import os @@ -773,7 +781,7 @@ def test_capture_badoutput_issue412(testdir): assert 0 """ ) - result = testdir.runpytest("--capture=fd") + result = pytester.runpytest("--capture=fd") result.stdout.fnmatch_lines( """ *def test_func* @@ -784,21 +792,21 @@ def test_capture_badoutput_issue412(testdir): ) -def test_capture_early_option_parsing(testdir): - testdir.makeconftest( +def test_capture_early_option_parsing(pytester: Pytester) -> None: + pytester.makeconftest( """ def pytest_runtest_setup(): print("hello19") """ ) - testdir.makepyfile("def test_func(): pass") - result = testdir.runpytest("-vs") + pytester.makepyfile("def test_func(): pass") + result = pytester.runpytest("-vs") assert result.ret == 0 assert "hello19" in result.stdout.str() -def test_capture_binary_output(testdir): - testdir.makepyfile( +def test_capture_binary_output(pytester: Pytester) -> None: + pytester.makepyfile( r""" import pytest @@ -814,13 +822,13 @@ def test_capture_binary_output(testdir): test_foo() """ ) - result = testdir.runpytest("--assert=plain") + result = pytester.runpytest("--assert=plain") result.assert_outcomes(passed=2) -def test_error_during_readouterr(testdir): +def test_error_during_readouterr(pytester: Pytester) -> None: """Make sure we suspend capturing if errors occur during readouterr""" - testdir.makepyfile( + pytester.makepyfile( pytest_xyz=""" from _pytest.capture import FDCapture @@ -831,26 +839,26 @@ def test_error_during_readouterr(testdir): FDCapture.snap = bad_snap """ ) - result = testdir.runpytest_subprocess("-p", "pytest_xyz", "--version") + result = pytester.runpytest_subprocess("-p", "pytest_xyz", "--version") result.stderr.fnmatch_lines( ["*in bad_snap", " raise Exception('boom')", "Exception: boom"] ) class TestCaptureIO: - def test_text(self): + def test_text(self) -> None: f = capture.CaptureIO() f.write("hello") s = f.getvalue() assert s == "hello" f.close() - def test_unicode_and_str_mixture(self): + def test_unicode_and_str_mixture(self) -> None: f = capture.CaptureIO() f.write("\u00f6") pytest.raises(TypeError, f.write, b"hello") - def test_write_bytes_to_buffer(self): + def test_write_bytes_to_buffer(self) -> None: """In python3, stdout / stderr are text io wrappers (exposing a buffer property of the underlying bytestream). See issue #1407 """ @@ -860,7 +868,7 @@ class TestCaptureIO: class TestTeeCaptureIO(TestCaptureIO): - def test_text(self): + def test_text(self) -> None: sio = io.StringIO() f = capture.TeeCaptureIO(sio) f.write("hello") @@ -871,14 +879,14 @@ class TestTeeCaptureIO(TestCaptureIO): f.close() sio.close() - def test_unicode_and_str_mixture(self): + def test_unicode_and_str_mixture(self) -> None: sio = io.StringIO() f = capture.TeeCaptureIO(sio) f.write("\u00f6") pytest.raises(TypeError, f.write, b"hello") -def test_dontreadfrominput(): +def test_dontreadfrominput() -> None: from _pytest.capture import DontReadFromInput f = DontReadFromInput() @@ -923,8 +931,8 @@ def test_captureresult() -> None: @pytest.fixture -def tmpfile(testdir) -> Generator[BinaryIO, None, None]: - f = testdir.makepyfile("").open("wb+") +def tmpfile(pytester: Pytester) -> Generator[BinaryIO, None, None]: + f = pytester.makepyfile("").open("wb+") yield f if not f.closed: f.close() @@ -946,7 +954,7 @@ def lsof_check(): class TestFDCapture: - def test_simple(self, tmpfile): + def test_simple(self, tmpfile: BinaryIO) -> None: fd = tmpfile.fileno() cap = capture.FDCapture(fd) data = b"hello" @@ -960,22 +968,22 @@ class TestFDCapture: cap.done() assert s == "hello" - def test_simple_many(self, tmpfile): + def test_simple_many(self, tmpfile: BinaryIO) -> None: for i in range(10): self.test_simple(tmpfile) - def test_simple_many_check_open_files(self, testdir): + def test_simple_many_check_open_files(self, pytester: Pytester) -> None: with lsof_check(): - with testdir.makepyfile("").open("wb+") as tmpfile: + with pytester.makepyfile("").open("wb+") as tmpfile: self.test_simple_many(tmpfile) - def test_simple_fail_second_start(self, tmpfile): + def test_simple_fail_second_start(self, tmpfile: BinaryIO) -> None: fd = tmpfile.fileno() cap = capture.FDCapture(fd) cap.done() pytest.raises(AssertionError, cap.start) - def test_stderr(self): + def test_stderr(self) -> None: cap = capture.FDCapture(2) cap.start() print("hello", file=sys.stderr) @@ -983,14 +991,14 @@ class TestFDCapture: cap.done() assert s == "hello\n" - def test_stdin(self): + def test_stdin(self) -> None: cap = capture.FDCapture(0) cap.start() x = os.read(0, 100).strip() cap.done() assert x == b"" - def test_writeorg(self, tmpfile): + def test_writeorg(self, tmpfile: BinaryIO) -> None: data1, data2 = b"foo", b"bar" cap = capture.FDCapture(tmpfile.fileno()) cap.start() @@ -1004,7 +1012,7 @@ class TestFDCapture: stmp = stmp_file.read() assert stmp == data2 - def test_simple_resume_suspend(self): + def test_simple_resume_suspend(self) -> None: with saved_fd(1): cap = capture.FDCapture(1) cap.start() @@ -1038,7 +1046,7 @@ class TestFDCapture: ) ) - def test_capfd_sys_stdout_mode(self, capfd): + def test_capfd_sys_stdout_mode(self, capfd) -> None: assert "b" not in sys.stdout.mode @@ -1064,7 +1072,7 @@ class TestStdCapture: finally: cap.stop_capturing() - def test_capturing_done_simple(self): + def test_capturing_done_simple(self) -> None: with self.getcapture() as cap: sys.stdout.write("hello") sys.stderr.write("world") @@ -1072,7 +1080,7 @@ class TestStdCapture: assert out == "hello" assert err == "world" - def test_capturing_reset_simple(self): + def test_capturing_reset_simple(self) -> None: with self.getcapture() as cap: print("hello world") sys.stderr.write("hello error\n") @@ -1080,7 +1088,7 @@ class TestStdCapture: assert out == "hello world\n" assert err == "hello error\n" - def test_capturing_readouterr(self): + def test_capturing_readouterr(self) -> None: with self.getcapture() as cap: print("hello world") sys.stderr.write("hello error\n") @@ -1091,7 +1099,7 @@ class TestStdCapture: out, err = cap.readouterr() assert err == "error2" - def test_capture_results_accessible_by_attribute(self): + def test_capture_results_accessible_by_attribute(self) -> None: with self.getcapture() as cap: sys.stdout.write("hello") sys.stderr.write("world") @@ -1099,13 +1107,13 @@ class TestStdCapture: assert capture_result.out == "hello" assert capture_result.err == "world" - def test_capturing_readouterr_unicode(self): + def test_capturing_readouterr_unicode(self) -> None: with self.getcapture() as cap: print("hxąć") out, err = cap.readouterr() assert out == "hxąć\n" - def test_reset_twice_error(self): + def test_reset_twice_error(self) -> None: with self.getcapture() as cap: print("hello") out, err = cap.readouterr() @@ -1113,7 +1121,7 @@ class TestStdCapture: assert out == "hello\n" assert not err - def test_capturing_modify_sysouterr_in_between(self): + def test_capturing_modify_sysouterr_in_between(self) -> None: oldout = sys.stdout olderr = sys.stderr with self.getcapture() as cap: @@ -1129,7 +1137,7 @@ class TestStdCapture: assert sys.stdout == oldout assert sys.stderr == olderr - def test_capturing_error_recursive(self): + def test_capturing_error_recursive(self) -> None: with self.getcapture() as cap1: print("cap1") with self.getcapture() as cap2: @@ -1139,7 +1147,7 @@ class TestStdCapture: assert out1 == "cap1\n" assert out2 == "cap2\n" - def test_just_out_capture(self): + def test_just_out_capture(self) -> None: with self.getcapture(out=True, err=False) as cap: sys.stdout.write("hello") sys.stderr.write("world") @@ -1147,7 +1155,7 @@ class TestStdCapture: assert out == "hello" assert not err - def test_just_err_capture(self): + def test_just_err_capture(self) -> None: with self.getcapture(out=False, err=True) as cap: sys.stdout.write("hello") sys.stderr.write("world") @@ -1155,14 +1163,14 @@ class TestStdCapture: assert err == "world" assert not out - def test_stdin_restored(self): + def test_stdin_restored(self) -> None: old = sys.stdin with self.getcapture(in_=True): newstdin = sys.stdin assert newstdin != sys.stdin assert sys.stdin is old - def test_stdin_nulled_by_default(self): + def test_stdin_nulled_by_default(self) -> None: print("XXX this test may well hang instead of crashing") print("XXX which indicates an error in the underlying capturing") print("XXX mechanisms") @@ -1173,7 +1181,7 @@ class TestStdCapture: class TestTeeStdCapture(TestStdCapture): captureclass = staticmethod(TeeStdCapture) - def test_capturing_error_recursive(self): + def test_capturing_error_recursive(self) -> None: r"""For TeeStdCapture since we passthrough stderr/stdout, cap1 should get all output, while cap2 should only get "cap2\n".""" @@ -1190,8 +1198,8 @@ class TestTeeStdCapture(TestStdCapture): class TestStdCaptureFD(TestStdCapture): captureclass = staticmethod(StdCaptureFD) - def test_simple_only_fd(self, testdir): - testdir.makepyfile( + def test_simple_only_fd(self, pytester: Pytester) -> None: + pytester.makepyfile( """\ import os def test_x(): @@ -1199,7 +1207,7 @@ class TestStdCaptureFD(TestStdCapture): assert 0 """ ) - result = testdir.runpytest_subprocess() + result = pytester.runpytest_subprocess() result.stdout.fnmatch_lines( """ *test_x* @@ -1231,8 +1239,8 @@ class TestStdCaptureFD(TestStdCapture): class TestStdCaptureFDinvalidFD: - def test_stdcapture_fd_invalid_fd(self, testdir): - testdir.makepyfile( + def test_stdcapture_fd_invalid_fd(self, pytester: Pytester) -> None: + pytester.makepyfile( """ import os from fnmatch import fnmatch @@ -1270,11 +1278,11 @@ class TestStdCaptureFDinvalidFD: cap.stop_capturing() """ ) - result = testdir.runpytest_subprocess("--capture=fd") + result = pytester.runpytest_subprocess("--capture=fd") assert result.ret == 0 assert result.parseoutcomes()["passed"] == 3 - def test_fdcapture_invalid_fd_with_fd_reuse(self, testdir): + def test_fdcapture_invalid_fd_with_fd_reuse(self, pytester: Pytester) -> None: with saved_fd(1): os.close(1) cap = capture.FDCaptureBinary(1) @@ -1289,7 +1297,7 @@ class TestStdCaptureFDinvalidFD: with pytest.raises(OSError): os.write(1, b"done") - def test_fdcapture_invalid_fd_without_fd_reuse(self, testdir): + def test_fdcapture_invalid_fd_without_fd_reuse(self, pytester: Pytester) -> None: with saved_fd(1), saved_fd(2): os.close(1) os.close(2) @@ -1306,12 +1314,14 @@ class TestStdCaptureFDinvalidFD: os.write(2, b"done") -def test_capture_not_started_but_reset(): +def test_capture_not_started_but_reset() -> None: capsys = StdCapture() capsys.stop_capturing() -def test_using_capsys_fixture_works_with_sys_stdout_encoding(capsys): +def test_using_capsys_fixture_works_with_sys_stdout_encoding( + capsys: CaptureFixture[str], +) -> None: test_text = "test text" print(test_text.encode(sys.stdout.encoding, "replace")) @@ -1320,7 +1330,7 @@ def test_using_capsys_fixture_works_with_sys_stdout_encoding(capsys): assert err == "" -def test_capsys_results_accessible_by_attribute(capsys): +def test_capsys_results_accessible_by_attribute(capsys: CaptureFixture[str]) -> None: sys.stdout.write("spam") sys.stderr.write("eggs") capture_result = capsys.readouterr() @@ -1340,8 +1350,8 @@ def test_fdcapture_tmpfile_remains_the_same() -> None: assert capfile2 == capfile -def test_close_and_capture_again(testdir): - testdir.makepyfile( +def test_close_and_capture_again(pytester: Pytester) -> None: + pytester.makepyfile( """ import os def test_close(): @@ -1351,7 +1361,7 @@ def test_close_and_capture_again(testdir): assert 0 """ ) - result = testdir.runpytest_subprocess() + result = pytester.runpytest_subprocess() result.stdout.fnmatch_lines( """ *test_capture_again* @@ -1365,9 +1375,9 @@ def test_close_and_capture_again(testdir): @pytest.mark.parametrize( "method", ["SysCapture(2)", "SysCapture(2, tee=True)", "FDCapture(2)"] ) -def test_capturing_and_logging_fundamentals(testdir, method: str) -> None: +def test_capturing_and_logging_fundamentals(pytester: Pytester, method: str) -> None: # here we check a fundamental feature - p = testdir.makepyfile( + p = pytester.makepyfile( """ import sys, os import py, logging @@ -1392,7 +1402,7 @@ def test_capturing_and_logging_fundamentals(testdir, method: str) -> None: """ % (method,) ) - result = testdir.runpython(p) + result = pytester.runpython(p) result.stdout.fnmatch_lines( """ suspend, captured*hello1* @@ -1407,8 +1417,8 @@ def test_capturing_and_logging_fundamentals(testdir, method: str) -> None: assert "atexit" not in result.stderr.str() -def test_error_attribute_issue555(testdir): - testdir.makepyfile( +def test_error_attribute_issue555(pytester: Pytester) -> None: + pytester.makepyfile( """ import sys def test_capattr(): @@ -1416,7 +1426,7 @@ def test_error_attribute_issue555(testdir): assert sys.stderr.errors == "replace" """ ) - reprec = testdir.inline_run() + reprec = pytester.inline_run() reprec.assertoutcome(passed=1) @@ -1438,8 +1448,8 @@ def test_py36_windowsconsoleio_workaround_non_standard_streams() -> None: _py36_windowsconsoleio_workaround(stream) -def test_dontreadfrominput_has_encoding(testdir): - testdir.makepyfile( +def test_dontreadfrominput_has_encoding(pytester: Pytester) -> None: + pytester.makepyfile( """ import sys def test_capattr(): @@ -1448,12 +1458,14 @@ def test_dontreadfrominput_has_encoding(testdir): assert sys.stderr.encoding """ ) - reprec = testdir.inline_run() + reprec = pytester.inline_run() reprec.assertoutcome(passed=1) -def test_crash_on_closing_tmpfile_py27(testdir): - p = testdir.makepyfile( +def test_crash_on_closing_tmpfile_py27( + pytester: Pytester, monkeypatch: MonkeyPatch +) -> None: + p = pytester.makepyfile( """ import threading import sys @@ -1480,19 +1492,19 @@ def test_crash_on_closing_tmpfile_py27(testdir): """ ) # Do not consider plugins like hypothesis, which might output to stderr. - testdir.monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1") - result = testdir.runpytest_subprocess(str(p)) + monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1") + result = pytester.runpytest_subprocess(str(p)) assert result.ret == 0 assert result.stderr.str() == "" result.stdout.no_fnmatch_line("*OSError*") -def test_global_capture_with_live_logging(testdir): +def test_global_capture_with_live_logging(pytester: Pytester) -> None: # Issue 3819 # capture should work with live cli logging # Teardown report seems to have the capture for the whole process (setup, capture, teardown) - testdir.makeconftest( + pytester.makeconftest( """ def pytest_runtest_logreport(report): if "test_global" in report.nodeid: @@ -1504,7 +1516,7 @@ def test_global_capture_with_live_logging(testdir): """ ) - testdir.makepyfile( + pytester.makepyfile( """ import logging import sys @@ -1526,7 +1538,7 @@ def test_global_capture_with_live_logging(testdir): print("end test") """ ) - result = testdir.runpytest_subprocess("--log-cli-level=INFO") + result = pytester.runpytest_subprocess("--log-cli-level=INFO") assert result.ret == 0 with open("caplog") as f: @@ -1546,11 +1558,13 @@ def test_global_capture_with_live_logging(testdir): @pytest.mark.parametrize("capture_fixture", ["capsys", "capfd"]) -def test_capture_with_live_logging(testdir, capture_fixture): +def test_capture_with_live_logging( + pytester: Pytester, capture_fixture: CaptureFixture[str] +) -> None: # Issue 3819 # capture should work with live cli logging - testdir.makepyfile( + pytester.makepyfile( """ import logging import sys @@ -1575,21 +1589,21 @@ def test_capture_with_live_logging(testdir, capture_fixture): ) ) - result = testdir.runpytest_subprocess("--log-cli-level=INFO") + result = pytester.runpytest_subprocess("--log-cli-level=INFO") assert result.ret == 0 -def test_typeerror_encodedfile_write(testdir): +def test_typeerror_encodedfile_write(pytester: Pytester) -> None: """It should behave the same with and without output capturing (#4861).""" - p = testdir.makepyfile( + p = pytester.makepyfile( """ def test_fails(): import sys sys.stdout.write(b"foo") """ ) - result_without_capture = testdir.runpytest("-s", str(p)) - result_with_capture = testdir.runpytest(str(p)) + result_without_capture = pytester.runpytest("-s", str(p)) + result_with_capture = pytester.runpytest(str(p)) assert result_with_capture.ret == result_without_capture.ret out = result_with_capture.stdout.str() @@ -1598,7 +1612,7 @@ def test_typeerror_encodedfile_write(testdir): ) -def test_stderr_write_returns_len(capsys): +def test_stderr_write_returns_len(capsys: CaptureFixture[str]) -> None: """Write on Encoded files, namely captured stderr, should return number of characters written.""" assert sys.stderr.write("Foo") == 3 @@ -1623,9 +1637,9 @@ def test__get_multicapture() -> None: ) -def test_logging_while_collecting(testdir): +def test_logging_while_collecting(pytester: Pytester) -> None: """Issue #6240: Calls to logging.xxx() during collection causes all logging calls to be duplicated to stderr""" - p = testdir.makepyfile( + p = pytester.makepyfile( """\ import logging @@ -1636,7 +1650,7 @@ def test_logging_while_collecting(testdir): assert False """ ) - result = testdir.runpytest_subprocess(p) + result = pytester.runpytest_subprocess(p) assert result.ret == ExitCode.TESTS_FAILED result.stdout.fnmatch_lines( [ |