Skip to content

Commit 5fcec4f

Browse files
committed
📝 Add testing code to documentation
1 parent c197cab commit 5fcec4f

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

docs/document/test.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,32 @@ format can be checked with `sphinx-lint
157157
Sybil can also check code blocks in the documentation with either
158158
:doc:`../test/pytest/index` or :doc:`../test/unittest`.
159159

160+
.. _test_code:
161+
162+
Code
163+
----
164+
165+
With the built-in Python library :doc:`../test/doctest`, you can also test code
166+
in your documentation with the :func:`doctest.testfile` method:
167+
168+
.. code-block:: Python
169+
170+
import doctest
171+
172+
doctest.testfile("example.rst")
173+
174+
This short script executes and checks all interactive Python examples contained
175+
in the :file:`example.rst` file. The content of the file is treated as if it
176+
were a single huge docstring.
177+
178+
.. seealso::
179+
A simple example can be found in the Python documentation: `Simple Usage:
180+
Checking Examples in a Text File
181+
<https://docs.python.org/3/library/doctest.html#simple-usage-checking-examples-in-a-text-file>`_.
182+
183+
Another way to test code in documentation is
184+
`pytest-doctestplus <https://github.com/scientific-python/pytest-doctestplus>`_.
185+
160186
Code formatting
161187
---------------
162188

docs/test/doctest.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Doctest
22
=======
33

4-
The Python module :doc:`doctest <python3:library/doctest>` checks whether the
5-
tests specified in a docstring are fulfilled.
4+
The Python module :doc:`doctest <python3:library/doctest>` checks whether tests
5+
in a docstring or in another text file are fulfilled.
66

77
#. In :download:`arithmetic.py` you can add the following docstring:
88

@@ -114,3 +114,7 @@ tests specified in a docstring are fulfilled.
114114
:language: python
115115
:lines: 38-
116116
:lineno-start: 38
117+
118+
.. seealso::
119+
:doc:`doctest <python3:library/doctest>` also has the ability to execute
120+
tests in your documentation: :ref:`Document → Testing → Code <test_code>`.

0 commit comments

Comments
 (0)