Consider the following function:
# in mymodule.py:
def myfunc(num,mystring):
"""
replicates a string
:param num: a positive integer
:param mystring: a string
:return: string concatenated with itself num times
:Example:
>>> num = 3
>>> mystring = "lol"
>>> myfunc(num, mystring)
"lollollol"
"""
return num*mystring
How can I make Sphinx doctest work, in order to verify the example code in
:Example:´ actually does work?
Whenever I run
make doctest`
it says that 3 tests were run, but 1 fails. Deleting the last two rows, starting with
myfunc(num, mystring)
,
it says that 2 tests were run sucessfully. So I must be doing something wrong with that line. But what?
EDIT Here's the terminal output (traceback) for the full code; it seems that somehow it can't find the function for which I've written the docstring and I'm running the doctest:
Running Sphinx v1.8.3
loading pickled environment... done
building [mo]: targets for 0 po files that are out of date
building [doctest]: targets for 1 source files that are out of date
updating environment: [] 0 added, 1 changed, 0 removed
reading sources... [100%] index
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
running tests...
Document: index
---------------
**********************************************************************
File "index.rst", line ?, in default
Failed example:
myfunc(num, mystring)
Exception raised:
Traceback (most recent call last):
File "/usr/lib/python3.6/doctest.py", line 1330, in __run
compileflags, 1), test.globs)
File "<doctest default[2]>", line 1, in <module>
myfunc(num, mystring)
NameError: name 'myfunc' is not defined
**********************************************************************
1 items had failures:
1 of 3 in default
3 tests in 1 items.
2 passed and 1 failed.
***Test Failed*** 1 failures.
Doctest summary
===============
3 tests
1 failure in tests
0 failures in setup code
0 failures in cleanup code
build finished with problems.
Makefile:19: recipe for target 'doctest' failed
Sphinx+doctest is not importing the module. You can either help him in the docstring:
:Example:
>>> from mymodule import myfunc
>>> myfunc(3, 'lol')
'lollollol'
or add this in conf.py
:
doctest_global_setup = '''
#if required, modif sys.path:
import sys
sys.path.append('../directory_containing_mymodule/')
from mymodule import *
'''
cf. https://www.sphinx-doc.org/en/master/usage/extensions/doctest.html
I'll be interested to hear if there are simpler answers.