coveragepy
coveragepy copied to clipboard
Coverage causes test to fail with pexpect and `pdb.set_trace`.
Hi,
First of all, thanks for the great library!
Describe the bug
I have a test that spawns a child process with pexpect and runs my application. Then, it checks whether some output is displayed and interacts with the program in the child process. The test looks like this.
def test_pdb_interaction_capturing_simple(tmp_path):
source = """
import pdb
def task_1():
i = 0
print("hello17")
pdb.set_trace()
i == 1
assert 0
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
child = pexpect.spawn(f"pytask {tmp_path.as_posix()}")
child.expect(r"task_1\(\)")
child.expect("i == 1") # The error happens here.
...
You can think of the task function defined in the string as a test function from pytest. Like pytest, my program would also stop at pdb.set_trace and allow the user to debug the function.
When I run the test with the --cov option from pytest-cov and coverage==6.4.1, the test fails after the statement with pdb.set_trace on Ubuntu and macOS. It seems like the content i == 1 is not displayed in the child process, but it is hard to tell since pexpect is hard to debug.
The test succeeds when I remove the --cov option. The test is also OK on Ubuntu and MacOS with coverage<=6.4.0.
To Reproduce
I reproduced and fixed the error by pinning coverage in https://github.com/pytask-dev/pytask/pull/283.
- Create a conda environment from
environment.yml. (If it helps reproduce the bug, I can add a requirement.txt.) - Run the failing tests with
pytest tests/test_debugging.py --cov=src. - The tests will fail on Linux and macOS with Python 3.7-10 and coverage==6.4.1.
Expected behavior
The tests should run fine as before.
Additional context
Since my package is a fork of pytest and very similar regarding this feature, I would not be surprised if they encounter the same issue. I saw some failing jobs in their PRs, which seem to have the same problem: https://github.com/pytest-dev/pytest/runs/6852766697?check_suite_focus=true#step:6:201.
Hmm, this is because of https://github.com/nedbat/coveragepy/commit/4b592fe30a7ebf871ba9a3d883ec955dae198c15#diff-68a483552b5cf650f43e371e28ef86ba13f3958e93bc1587c3cb7afad0397f24R517, which was done to get a slight performance improvement. I guess there isn't a way to keep it?
CPython has an open issue to set the flag when using bdb: https://github.com/python/cpython/issues/80675
I guess there isn't a way to keep it?
Maybe this question was rhetorical 😅, but if not, I want to mention that I cannot judge whether the changes justify breaking some code -- probably not many people are affected?. Of course, it would be more convenient for me if you would revert the patch. Still, the alternatives are only minor nuisances one can quickly work around (pinning coverage, verifying it locally, etc.).
Would the changes to CPython be backported to 3.7 and the other versions, or would it only affect 3.10 and 3.11?