Skip to content

doctests has no access to the module globals (c.f. running with python -m doctest) #6590

@skirpichev

Description

@skirpichev

Lets take this simple example:

$ cat docs/conf.py 
import a

extensions = ['sphinx.ext.doctest', 'sphinx.ext.autodoc']
$ cat docs/contents.rst 
.. automodule:: a
    :members:
$ cat a/__init__.py 
def foo():
    """bar

    >>> foo()
    1
    """
    return 1

Testing with the plain doctest module works:

$ python3 -m doctest -v a/__init__.py 
Trying:
    foo()
Expecting:
    1
ok
1 items had no tests:
    __init__
1 items passed all tests:
   1 tests in __init__.foo
1 tests in 2 items.
1 passed and 0 failed.
Test passed.

py.test --doctest-modules works in the same way, it follows same execution context convention.

Yet, apparently doctest code has no access to the module globals (e.g. foo):

$ python3 -m sphinx -b doctest docs/ builds/
Running Sphinx v1.8.4
loading pickled environment... done
building [mo]: targets for 0 po files that are out of date
building [doctest]: targets for 1 source files that are out of date
updating environment: 0 added, 0 changed, 0 removed
looking for now-outdated files... none found
running tests...

Document: contents
------------------
**********************************************************************
File "../a/__init__.py", line ?, in default
Failed example:
    foo()
Exception raised:
    Traceback (most recent call last):
      File "/usr/lib/python3.7/doctest.py", line 1329, in __run
        compileflags, 1), test.globs)
      File "<doctest default[0]>", line 1, in <module>
        foo()
    NameError: name 'foo' is not defined
**********************************************************************
1 items had failures:
   1 of   1 in default
1 tests in 1 items.
0 passed and 1 failed.
***Test Failed*** 1 failures.

Doctest summary
===============
    1 test
    1 failure in tests
    0 failures in setup code
    0 failures in cleanup code
build finished with problems.

Sounds like a bug for me.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions