question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PEP 553 interactivity between `IPython.core.debugger.set_trace` and `python -m doctest` causes test failures

See original GitHub issue

The promises of PEP 553 are pretty cool and even mention IPython by name. This and this seem to suggest that IPython.terminal.debugger.set_trace would be a good value for PYTHONBREAKPOINT. Indeed it does work:

File:

# test_case.py
def test_foo():
  one = "1"
  breakpoint()
  assert int(one) == 1

if __name__ == "__main__":
  test_foo()

Run:

% PYTHONBREAKPOINT='IPython.core.debugger.set_trace' python test_case.py
> /Users/matt/Documents/dev/dycelib/test_case.py(5)test_foo()
      3   one = "1"
      4   breakpoint()
----> 5   assert int(one) == 1
      6
      7 if __name__ == "__main__":

ipdb> dir()
['one']
ipdb> one
'1'
ipdb> c

However, breakpoints in doctests don’t work with IPython.core.debugger.set_trace. Consider:

# test_case.txt
>>> one = "1"
>>> breakpoint()
>>> int(one) == 1
True

This works:

% python -m doctest test_case.txt ; echo "exit code: ${?}"
--Return--
> <doctest test_case.txt[1]>(1)<module>()->None
-> breakpoint()
(Pdb) dir()
['__builtins__', '__name__', '__return__', 'one']
(Pdb) one
'1'
(Pdb) c
exit code: 0

Neat! Now try this:

% PYTHONBREAKPOINT='IPython.core.debugger.set_trace' python -m doctest test_case.txt ; echo "exit code: ${?}"
dir()
one
c
**********************************************************************
File "test_case.txt", line 3, in test_case.txt
Failed example:
    breakpoint()
Expected nothing
Got:
    --Return--
    None
    > <doctest test_case.txt[1]>(1)<module>()
    ----> 1 breakpoint()

    ipdb> ['__builtins__', '__name__', '__return__', 'one']
    ipdb> '1'
    ipdb>
**********************************************************************
1 items had failures:
   1 of   3 in test_case.txt
***Test Failed*** 1 failures.
exit code 1

Note that there is no interactivity or output until the end of the run (after typing c<RETURN>). Also, for some reason, IPython’s output is being considered by the test itself, so the test fails.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
MrMinocommented, Aug 31, 2021

Right. I edited my previous response to reflect that.

0reactions
positacommented, Aug 31, 2021

Yup. Looks like prompt-toolkit is messing with doctest somehow: PYTHONBREAKPOINT=ipdb.sset_trace works properly.

It does sort of. EDIT: Removed as redundant.

Read more comments on GitHub >

github_iconTop Results From Across the Web

doctest — Test interactive Python examples — Python 3.11.1 ...
To perform regression testing by verifying that interactive examples from a test file or a test object work as expected. To write tutorial...
Read more >
Python's doctest: Document and Test Your Code at Once
The doctest module is a lightweight testing framework that provides quick and straightforward test automation. It can read the test cases from ......
Read more >
cpython/doctest.py at main - GitHub
line of output is "Test failed.". Run it with the -v switch instead: python M.py -v. and a detailed report of all examples...
Read more >
Summary of Python tracker Issues - Mailing List Archive
#31606: [Windows] Tests failing on installed Python 3.7a1 on Windows ... #31353: Implement PEP 553 - built-in breakpoint()
Read more >
How To Write Doctests in Python - DigitalOcean
Python's standard library comes equipped with a test framework module called doctest. The doctest module programmatically searches Python ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found