pytest-dev / pytest-dev/pytest

--doctest-modules ignores module-attribute docstrings

Open
#6,996 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

[edit: mistakenly submitted without description]

Description

Module-level attributes with docstrings underneath (or sphinx-comments prefixed with #:) are ignored when doctesting modules.

  • Standard python's doctest module also ignores module-data docstrings
    (so we can claim this "not a bug"), yet ...
  • Sphinx consider these as valid docstrings, and
  • Sphinx's doctest builder also validates them.
Example

Running pytest --doctest-modules on the module below named foo.py:

def f():
    """
    This doctest works:

    >>> 1 + 1
    1
    """

a = 1
"""
But this doctest is ignored:

>>> 1 + 1
4
"""

#: Also this doctest is ignored:
#:
#: >>> 1 + 1
#: 3
b = 1


if __name__ == "__main__":
    import doctest
    doctest.testmod()
  • raises just 1 error (1 + 1 = 4),
  • python foo.py also raises 1 error, but ...
  • if you setup a small site, include this module with auto-documenter, and launch python setup.py build_sphinx -b doctest (or make html), the doctest sphinx-builder takes both docstring of a and b module data members into account, and screams 3 times!
Versions
$ pip list|grep pytest
pytest                             5.3.5       
pytest-cov                         2.8.1       
pytest-sphinx                      0.2.2       
pytest-sugar                       0.9.2      

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 foo.py example and reproduce the behavior using pytest --doctest-modules, comparing it with doctest.testmod() and the Sphinx doctest builder described in the issue. The work is done when doctests in both ordinary module-attribute docstrings and #: documentation comments are discovered and validated by pytest.

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.