From b8f76c16c21821dc457b246222e02e52880d4e38 Mon Sep 17 00:00:00 2001 From: Pey Lian Lim <2090236+pllim@users.noreply.github.com> Date: Wed, 15 Sep 2021 17:23:25 -0400 Subject: [PATCH] ENH: Support testcleanup --- README.rst | 15 +++++++++++++++ pytest_doctestplus/sphinx/doctestplus.py | 1 + tests/docs/skip_some.rst | 20 ++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/README.rst b/README.rst index 78cb5d6..08394ce 100644 --- a/README.rst +++ b/README.rst @@ -101,6 +101,21 @@ of the directives that this plugin defines are described in the sections below. .. _doctest directives: https://docs.python.org/3/library/doctest.html#directives .. _documentation: https://docs.python.org/3/library/doctest.html#directives +Sphinx Doctest Directives +~~~~~~~~~~~~~~~~~~~~~~~~~ + +You can use ``testsetup`` and ``testcleanup`` in Sphinx RST to run code that is +not visible in rendered document. However, due to how ``pytest-doctestplus`` +works, the code within needs to be prepended by ``>>>``. For example:: + + .. testsetup:: + + >>> x = 42 + + .. testcleanup:: + + >>> del x + Floating Point Comparison ~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/pytest_doctestplus/sphinx/doctestplus.py b/pytest_doctestplus/sphinx/doctestplus.py index 8ff5863..ed161f9 100644 --- a/pytest_doctestplus/sphinx/doctestplus.py +++ b/pytest_doctestplus/sphinx/doctestplus.py @@ -52,6 +52,7 @@ def setup(app): # useful for testing documentation using doctest, but does not actually # belong in the documentation itself. app.add_directive('testsetup', DoctestOmitDirective, override=True) + app.add_directive('testcleanup', DoctestOmitDirective, override=True) return {'parallel_read_safe': True, 'parallel_write_safe': True} diff --git a/tests/docs/skip_some.rst b/tests/docs/skip_some.rst index ef0db54..deceff0 100644 --- a/tests/docs/skip_some.rst +++ b/tests/docs/skip_some.rst @@ -8,6 +8,26 @@ if they run. Some are not. The intent of this file is to test the directives that are provided by the `--doctest-rst` option to make sure that the bad ones get skipped. +.. This will not show in doc but it will run in doctestplus. + +.. testsetup:: + + >>> x = 42 + +This code will show the value of ``x`` that is set within ``testsetup``:: + + >>> x + 42 + +.. testcleanup:: + + >>> x = x + 1 + +This code will show the value of ``x`` that is set within ``testcleanup``:: + + >>> x + 43 + Here's One That Works =====================