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

doctest expecting asserts behaves differently than stdlib #12417

Open
isidroas opened this issue Jun 4, 2024 · 1 comment
Open

doctest expecting asserts behaves differently than stdlib #12417

isidroas opened this issue Jun 4, 2024 · 1 comment
Labels
plugin: doctests related to the doctests builtin plugin

Comments

@isidroas
Copy link

isidroas commented Jun 4, 2024

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

@Zac-HD Zac-HD added the plugin: doctests related to the doctests builtin plugin label Jun 24, 2024
@shannoncai
Copy link

I can take a look into this issue! I'll create a pull request soon but let me know if there's anything else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: doctests related to the doctests builtin plugin
Projects
None yet
Development

No branches or pull requests

3 participants