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

Revisit some tests #507

Open
wants to merge 2 commits into
base: my-master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions testing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from _pytest.pytester import RunResult

if TYPE_CHECKING:
from typing import Generator

from _pytest.config import Config


Expand All @@ -18,19 +20,36 @@ def pytest_addoption(parser):
)


if sys.gettrace():
orig_trace = sys.gettrace()

if orig_trace:

@pytest.fixture(autouse=True)
def restore_tracing():
"""Restore tracing function (when run with Coverage.py).

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):
Expand Down
24 changes: 17 additions & 7 deletions testing/test_debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)<module>()",
"-> x = 5",
"(Pdb) custom_header",
"> */test_pytest_set_trace_used_outside_of_test.py(5)<module>()",
"-> x = 6",
"(Pdb) ",
],
complete=True,
)
assert result.ret == 0

def test_pdb_used_in_generate_tests(self, testdir):
p1 = testdir.makepyfile(
Expand Down