From acbdc5f18fdd86c8bd735dd3eacaa51e988ffdc9 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 7 Sep 2020 23:51:45 +0200 Subject: [PATCH 1/2] tests: revisit test_pytest_set_trace_used_outside_of_test{,_header} --- testing/test_debugging.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/testing/test_debugging.py b/testing/test_debugging.py index e3818a1809a..6efce0d9a01 100644 --- a/testing/test_debugging.py +++ b/testing/test_debugging.py @@ -776,19 +776,29 @@ def do_continue(self, arg): assert "> PDB continue >" in rest assert "= 1 passed in" in rest - def test_pytest_set_trace_used_outside_of_test(self, testdir): + def test_pytest_set_trace_used_outside_of_test(self, testdir: "Testdir") -> None: p1 = testdir.makepyfile( """ import pytest pytest.set_trace() x = 5 - """ + pytest.set_trace(header="custom_header") + x = 6 + """ ) - child = testdir.spawn("{} {}".format(sys.executable, p1)) - child.expect("x = 5") - child.expect("Pdb") - child.sendeof() - self.flush(child) + result = testdir.run(sys.executable, p1, stdin="c\nc\n") + result.stdout.fnmatch_lines( + [ + "> */test_pytest_set_trace_used_outside_of_test.py(3)()", + "-> x = 5", + "(Pdb) custom_header", + "> */test_pytest_set_trace_used_outside_of_test.py(5)()", + "-> x = 6", + "(Pdb) ", + ], + complete=True, + ) + assert result.ret == 0 def test_pdb_used_in_generate_tests(self, testdir): p1 = testdir.makepyfile( From 8dedf33fa84a26f32aa9109bd1214d741f7665ff Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 8 Sep 2020 13:19:31 +0200 Subject: [PATCH 2/2] testing/conftest.py: wrap_sys_settrace_for_pdb_with_coverage --- testing/conftest.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/testing/conftest.py b/testing/conftest.py index 9a30253079b..9abd1aac788 100644 --- a/testing/conftest.py +++ b/testing/conftest.py @@ -9,6 +9,8 @@ from _pytest.pytester import RunResult if TYPE_CHECKING: + from typing import Generator + from _pytest.config import Config @@ -18,7 +20,9 @@ def pytest_addoption(parser): ) -if sys.gettrace(): +orig_trace = sys.gettrace() + +if orig_trace: @pytest.fixture(autouse=True) def restore_tracing(): @@ -26,11 +30,26 @@ def restore_tracing(): https://bugs.python.org/issue37011 """ - orig_trace = sys.gettrace() + assert orig_trace == sys.gettrace(), (orig_trace, sys.gettrace()) yield if sys.gettrace() != orig_trace: sys.settrace(orig_trace) + orig_settrace = sys.settrace + + def wrapped_settrace(tracer): + if tracer is None: + orig_settrace(orig_trace) + else: + orig_settrace(tracer) + + @pytest.fixture(autouse=True) + def wrap_sys_settrace_for_pdb_with_coverage( + monkeypatch, + ) -> "Generator[None, None, None]": + monkeypatch.setattr("sys.settrace", wrapped_settrace) + yield + @pytest.hookimpl def pytest_runtest_setup(item):