pytest-dev / pytest-dev/pytest
Add support for doctest groups with isolated namespaces, a la sphinx
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
Description
The sphinx doctest extension supports a "group" argument, where each doctest group has a separate namespace. This is used pretty heavily in dateutil, and you can see why it would be useful: a global namespace for all tests is undesirable, but you may have multiple doctest blocks that are part of the same example, and you don't want to re-define every variable and import in every code block, hence named groups.
It seems that pytest's support for doctests does not have this feature, and uses a single namespace per file or document, which is not ideal. It also doesn't seem to have any support for the associated testsetup and testcleanup directives, but those are less important as long as there's some per-group fixtures or per-group equivalent of doctest_namespace.
As a demonstration:
.. doctest:: grp1
>>> from datetime import datetime
>>> datetime(2019, 1, 1)
datetime.datetime(2019, 1, 1, 0, 0)
.. doctest:: grp1
>>> datetime(2019, 1, 1)
datetime.datetime(2019, 1, 1, 0, 0)
.. THIS SHOULD FAIL
.. doctest:: grp2
>>> datetime(2019, 1, 1, 0, 0)
datetime.datetime(2019, 1, 1, 0, 0)
This succeeds in pytest but rightly fails in sphinx.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading pytest's doctest support and the existing doctest_namespace behavior. Reproduce the RST example from the issue, then add coverage showing that blocks with the same group share a namespace while different groups remain isolated; the demonstrated grp2 case should fail as it does in Sphinx.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100