Skip to content

Commit 0c41529

Browse files
committed
Add section on doctests
1 parent 196f49d commit 0c41529

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

doc/tutorial/describing-code.rst

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,127 @@ Congratulations! You have created a basic Python library.
182182
An alternative is to not create ``pyproject.toml`` at all,
183183
and setting the :envvar:`PYTHONPATH`, :py:data:`sys.path`, or
184184
equivalent. However, the ``pyproject.toml`` approach is more robust.
185+
186+
Adding some doctests to the documentation
187+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
188+
189+
To add doctests to your documentation, first enable the
190+
:doc:`doctest </usage/extensions/doctest>` extension in ``conf.py``:
191+
192+
.. code-block:: python
193+
:caption: docs/source/conf.py
194+
:emphasize-lines: 3
195+
196+
extensions = [
197+
'sphinx.ext.duration',
198+
'sphinx.ext.doctest',
199+
]
200+
201+
Then, write a doctest block as follows:
202+
203+
.. code-block:: rst
204+
:caption: docs/source/usage.rst
205+
206+
>>> import lumache
207+
>>> lumache.get_random_ingredients()
208+
['shells', 'gorgonzola', 'parsley']
209+
210+
You can now run ``make doctest`` to execute the doctests of your documentation.
211+
However, initially this will display an error, since the actual code does not
212+
exist yet:
213+
214+
.. code-block:: console
215+
216+
(.venv) $ make doctest
217+
Running Sphinx v4.2.0+
218+
loading pickled environment... done
219+
...
220+
running tests...
221+
222+
Document: usage
223+
---------------
224+
**********************************************************************
225+
File "usage.rst", line 44, in default
226+
Failed example:
227+
lumache.get_random_ingredients()
228+
Exception raised:
229+
Traceback (most recent call last):
230+
File "/usr/lib/python3.9/doctest.py", line 1336, in __run
231+
exec(compile(example.source, filename, "single",
232+
File "<doctest default[1]>", line 1, in <module>
233+
lumache.get_random_ingredients()
234+
AttributeError: module 'lumache' has no attribute 'get_random_ingredients'
235+
**********************************************************************
236+
1 items had failures:
237+
1 of 2 in default
238+
2 tests in 1 items.
239+
1 passed and 1 failed.
240+
***Test Failed*** 1 failures.
241+
242+
Doctest summary
243+
===============
244+
2 tests
245+
1 failure in tests
246+
0 failures in setup code
247+
0 failures in cleanup code
248+
build finished with problems.
249+
make: *** [Makefile:20: doctest] Error 1
250+
251+
Therefore, you will need to make adjustments to your ``lumache.py``. To observe
252+
how a doctest failure looks like (rather than a code error as above), let's
253+
write the return value incorrectly first. Therefore, add a function
254+
``get_random_ingredients`` like this:
255+
256+
.. code-block:: python
257+
:caption: lumache.py
258+
259+
def get_random_ingredients():
260+
return ["eggs", "bacon", "spam"]
261+
262+
Now ``make doctest`` will give an output similar to this:
263+
264+
.. code-block:: console
265+
266+
(.venv) $ make doctest
267+
Running Sphinx v4.2.0+
268+
loading pickled environment... done
269+
...
270+
running tests...
271+
272+
Document: usage
273+
---------------
274+
**********************************************************************
275+
File "usage.rst", line 44, in default
276+
Failed example:
277+
lumache.get_random_ingredients()
278+
Expected:
279+
['shells', 'gorgonzola', 'parsley']
280+
Got:
281+
['eggs', 'bacon', 'spam']
282+
**********************************************************************
283+
1 items had failures:
284+
1 of 2 in default
285+
2 tests in 1 items.
286+
1 passed and 1 failed.
287+
***Test Failed*** 1 failures.
288+
289+
Doctest summary
290+
===============
291+
2 tests
292+
1 failure in tests
293+
0 failures in setup code
294+
0 failures in cleanup code
295+
build finished with problems.
296+
make: *** [Makefile:20: doctest] Error 1
297+
298+
As you can see, doctest reports the expected and the actual results,
299+
for easy examination. It is now time to fix the function:
300+
301+
.. code-block:: python
302+
:caption: lumache.py
303+
:emphasize-lines: 3
304+
305+
def get_random_ingredients():
306+
return ["shells", "gorgonzola", "parsley"]
307+
308+
And finally, ``make test`` reports success!

0 commit comments

Comments
 (0)