DocTests in seperate file not working
Hi, I'm having issues getting the DocTests to run as a seperate file using the built in DocTests feature (creating a file name.doctest file and pressing play). The DocTest file works when calling existing packages, but if I do the following:
New top-level folder called "HelloWorld"
New script called "helloworld.py" with the following code:
def get_hello(name): return "Hello " + name
Then create a "helloworld.doctest" with the following:
>>> import helloworld >>> helloworld.get_hello("Richard") 'Hello Richard'
Then run the DocTest by pressing play (pressing play using the default template passes), I get the following error:
ImportError: No module named 'helloworld' NameError: name 'helloworld' is not defined
If I add the DocTest into the comment of the method, running DocTest works on that script. But I want to remove clutter and have the tests in a seperate file and use this feature.
def get_hello(name): """ >>> get_hello("Richard") 'Hello Richard' """ return "Hello " + name
Is there something I'm missing? Any help will be most appreciated
IIRC, the issue is that doctests don't get sys.path modified the way real scripts do.
You should be able to add sys.path.append(yourpathname) and it should work.
Unfortunately, file is also not defined, so you cannot easily just use relative paths.
Thanks JonB. The testing of the get_hello function works. However the test fails on the line of the
import helloworld. It says
Got: (the info of sys.path). This does not stop me testing my code now, however it just has one failure due to this. Any thoughts? Thanks again.
>>> import sys; _path = '/private/var/mobile/Containers/Shared/AppGroup/.../Pythonista3/Documents/HelloWorld'; sys.path.append(_path) >>> import helloworld >>> helloworld.get_hello("Richard") 'Hello Richard'
I created a DocTest file for the actual module I want to test and weirdly I don't get the import issue... I don't see any difference except the module I'm testing is two folder layers deep (
.../HelloWorld/helloworld.py). But as it's fully working with the module I actually want to test, that's fine for me for now.
If you just ran the .py file, the folder will already be in sys.path.