pytest-dev / pytest-dev/pytest

doctest expecting asserts behaves differently than stdlib

Open
#12,417 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

plugin: doctests
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

The following file,

import math

def square_root(n):
    """
    >>> square_root(4)
    2.0

    >>> square_root(-1)
    Traceback (most recent call last):
        ...
    AssertionError: number must not be negative

    """
    assert n>= 0, 'number must not be negative'
    return math.sqrt(n)

works well with the doctest of stdlib,

$ python -m doctest test.py

$ echo $?
0

but it fails using pytest:

$ python -m doctest test.py
$ pytest --doctest-modules test.py
============================= test session starts ==============================
platform linux -- Python 3.11.6, pytest-8.2.1, pluggy-1.5.0
rootdir: /tmp
collected 1 item

test.py F                                                                [100%]

=================================== FAILURES ===================================
__________________________ [doctest] test.square_root __________________________
004 
005     >>> square_root(4)
006     2.0
007 
008     >>> square_root(-1)
Differences (unified diff with -expected +actual):
    @@ -1,3 +1,9 @@
     Traceback (most recent call last):
    -    ...
    +  File "/usr/lib/python3.11/doctest.py", line 1351, in __run
    +    exec(compile(example.source, filename, "single",
    +  File "<doctest test.square_root[1]>", line 1, in <module>
    +    square_root(-1)
    +  File "/tmp/test.py", line 14, in square_root
    +    assert n>= 0, 'number must not be negative'
     AssertionError: number must not be negative
    +assert -1 >= 0

/tmp/test.py:8: DocTestFailure
=========================== short test summary info ============================
FAILED test.py::test.square_root
============================== 1 failed in 0.01s ===============================

due to that assert -1 >= 0 extra line

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal test.py reproducer and run both python -m doctest test.py and pytest --doctest-modules test.py to compare the traceback output. Trace how pytest handles the doctest in test.square_root; done means the documented AssertionError example passes under pytest without the extra assertion-rewriting line, with regression coverage for this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.