Skip to content

Add support for doctest groups with isolated namespaces, a la sphinx #5827

Open
@pganssle

Description

@pganssle

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: doctestsrelated to the doctests builtin plugintype: enhancementnew feature or API change, should be merged into features branch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions