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

Coverage causes test to fail with pexpect and pdb.set_trace. #1402

Open
tobiasraabe opened this issue Jun 12, 2022 · 3 comments
Open

Coverage causes test to fail with pexpect and pdb.set_trace. #1402

tobiasraabe opened this issue Jun 12, 2022 · 3 comments
Labels
bug Something isn't working next

Comments

@tobiasraabe
Copy link

tobiasraabe commented Jun 12, 2022

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 pytask-dev/pytask#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.

@tobiasraabe tobiasraabe added bug Something isn't working needs triage labels Jun 12, 2022
@nedbat
Copy link
Owner

nedbat commented Jun 13, 2022

Hmm, this is because of 4b592fe#diff-68a483552b5cf650f43e371e28ef86ba13f3958e93bc1587c3cb7afad0397f24R517, which was done to get a slight performance improvement. I guess there isn't a way to keep it?

@nedbat
Copy link
Owner

nedbat commented Jun 13, 2022

CPython has an open issue to set the flag when using bdb: python/cpython#80675

@tobiasraabe
Copy link
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working next
Projects
None yet
Development

No branches or pull requests

2 participants