mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Simplify doctest section
This commit is contained in:
@@ -212,51 +212,9 @@ Then, write a doctest block as follows:
|
||||
>>> lumache.get_random_ingredients()
|
||||
['shells', 'gorgonzola', 'parsley']
|
||||
|
||||
You can now run ``make doctest`` to execute the doctests of your documentation.
|
||||
However, initially this will display an error, since the actual code does not
|
||||
exist yet:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
(.venv) $ make doctest
|
||||
Running Sphinx v4.2.0+
|
||||
loading pickled environment... done
|
||||
...
|
||||
running tests...
|
||||
|
||||
Document: usage
|
||||
---------------
|
||||
**********************************************************************
|
||||
File "usage.rst", line 44, in default
|
||||
Failed example:
|
||||
lumache.get_random_ingredients()
|
||||
Exception raised:
|
||||
Traceback (most recent call last):
|
||||
File "/usr/lib/python3.9/doctest.py", line 1336, in __run
|
||||
exec(compile(example.source, filename, "single",
|
||||
File "<doctest default[1]>", line 1, in <module>
|
||||
lumache.get_random_ingredients()
|
||||
AttributeError: module 'lumache' has no attribute 'get_random_ingredients'
|
||||
**********************************************************************
|
||||
1 items had failures:
|
||||
1 of 2 in default
|
||||
2 tests in 1 items.
|
||||
1 passed and 1 failed.
|
||||
***Test Failed*** 1 failures.
|
||||
|
||||
Doctest summary
|
||||
===============
|
||||
2 tests
|
||||
1 failure in tests
|
||||
0 failures in setup code
|
||||
0 failures in cleanup code
|
||||
build finished with problems.
|
||||
make: *** [Makefile:20: doctest] Error 1
|
||||
|
||||
Therefore, you will need to make adjustments to your ``lumache.py``. To observe
|
||||
how a doctest failure looks like (rather than a code error as above), let's
|
||||
write the return value incorrectly first. Therefore, add a function
|
||||
``get_random_ingredients`` like this:
|
||||
To observe how a doctest failure looks like (rather than a code error as
|
||||
above), let's write the return value incorrectly first. Therefore, add a
|
||||
function ``get_random_ingredients`` like this:
|
||||
|
||||
.. code-block:: python
|
||||
:caption: lumache.py
|
||||
@@ -264,7 +222,9 @@ write the return value incorrectly first. Therefore, add a function
|
||||
def get_random_ingredients():
|
||||
return ["eggs", "bacon", "spam"]
|
||||
|
||||
Now ``make doctest`` will give an output similar to this:
|
||||
You can now run ``make doctest`` to execute the doctests of your documentation.
|
||||
Initially this will display an error, since the actual code does not behave
|
||||
as specified:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
@@ -285,19 +245,7 @@ Now ``make doctest`` will give an output similar to this:
|
||||
Got:
|
||||
['eggs', 'bacon', 'spam']
|
||||
**********************************************************************
|
||||
1 items had failures:
|
||||
1 of 2 in default
|
||||
2 tests in 1 items.
|
||||
1 passed and 1 failed.
|
||||
***Test Failed*** 1 failures.
|
||||
|
||||
Doctest summary
|
||||
===============
|
||||
2 tests
|
||||
1 failure in tests
|
||||
0 failures in setup code
|
||||
0 failures in cleanup code
|
||||
build finished with problems.
|
||||
...
|
||||
make: *** [Makefile:20: doctest] Error 1
|
||||
|
||||
As you can see, doctest reports the expected and the actual results,
|
||||
|
||||
Reference in New Issue
Block a user