I have a package mypackage where I have foo.py
# mypackage/foo.py
def foo():
"""
>>> 1
1
>>> bar()
'bar'
"""
pass
def bar():
return "bar"
If I run python -m doctest mypackage/foo.py it works like a charm, but. If I create a test file with ...
import doctest
doctest.testfile("foo.py", package="mypackage")
... in the same folder as mypackage, and run this file, it fails with
**********************************************************************
File "/Users/gecko/code/python/doctesttest/mypackage/foo.py", line 6, in foo.py
Failed example:
bar()
Exception raised:
Traceback (most recent call last):
File "/Users/gecko/.pyenv/versions/3.8.2/lib/python3.8/doctest.py", line 1329, in __run
exec(compile(example.source, filename, "single",
File "<doctest foo.py[1]>", line 1, in <module>
bar()
NameError: name 'bar' is not defined
**********************************************************************
1 items had failures:
1 of 2 in foo.py
***Test Failed*** 1 failures.
ā doctesttest
My question is why, and how to fix it?