This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Doctest capture only AssertionError but not printed text
Type: enhancement Stage: needs patch
Components: Library (Lib) Versions: Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: osantana, steven.daprano, tim.peters
Priority: normal Keywords:

Created on 2014-04-09 02:13 by osantana, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
doctest_bug.py osantana, 2014-04-09 02:13 Example that reproduces error
Messages (5)
msg215796 - (view) Author: Osvaldo Santana Neto (osantana) * Date: 2014-04-09 02:13
The following doctest should pass, but it fails:

    >>> def spam(): print("eggs")
    ...
    >>> assert spam()
    eggs
    Traceback (most recent call last):
    AssertionError

But if we remove the print output from printed results the test pass:

    >>> def spam(): print("eggs")
    ...
    >>> assert spam()
    Traceback (most recent call last):
    AssertionError

I'm writing the 2nd edition of a book about Python (covering python3) and Django and I'm using doctest to run the interactive sessions that I use as examples in book.
msg215797 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2014-04-09 02:26
The first footnote in the docs explain this:

    Examples containing both expected output and an
    exception are not supported. Trying to guess where
    one ends and the other begins is too error-prone,
    and that also makes for a confusing test.

So, sorry, but this is intentionally not supported.
msg215798 - (view) Author: Osvaldo Santana Neto (osantana) * Date: 2014-04-09 02:31
Hi Tim,

I tried to find more information in documentation before opening this bug but I need to confess that I didn't read the footnote.

Thanks and sorry for the invalid report.
msg215799 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2014-04-09 02:41
Personally, I think that the second reason given in the footnote, that it makes for confusing tests, is bogus, or at least it's a matter of opinion. I for one don't think they are confusing, and would like to see mixed output/tracebacks supported. Tim, would you accept this being reopened as an Enhancement request for 3.5 rather than a bug fix?
msg215800 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2014-04-09 02:51
Steven, no objection here.
History
Date User Action Args
2022-04-11 14:58:01adminsetgithub: 65382
2014-04-09 03:18:12steven.dapranosetstatus: closed -> open
type: behavior -> enhancement
stage: needs patch
resolution: not a bug ->
versions: + Python 3.5, - Python 2.7, Python 3.4
2014-04-09 02:51:49tim.peterssetmessages: + msg215800
2014-04-09 02:41:12steven.dapranosetnosy: + steven.daprano
messages: + msg215799
2014-04-09 02:31:23osantanasetstatus: open -> closed
resolution: not a bug
messages: + msg215798
2014-04-09 02:26:03tim.peterssetnosy: + tim.peters
messages: + msg215797
2014-04-09 02:13:51osantanacreate