I have a folder ‘pyMS’ that contains scripts with doctests. Also in pyMS is a folder ‘test’ which has both unit tests and a folder ‘testfiles’ that contains files with data that I use for testing.
pyMS/
├── __init__.py
├── baseFunctions.py
└── test
├── __init__.py
├── test_baseFunctions.py
└── testfiles
└── testfile.txt
I want to use the unittest to run the doctest with
suite.addTest(doctest.DocTestSuite("baseFunctions")
However, I’m running in the problem when I try to use files in the doctest.
This would work when doing the doctest from the commandline (python baseFunctions -v):
testfile = open('test/testfiles/testfile.txt')
but when running it from the unittest it gives an IOError because the test_baseFunctions.py is in the test folder, so the correct location for the file is /testfiles/testfile.txt instead of test/testfiles/testfile.txt.
My very ugly solution now is
import os
testfile = open(os.abspath('').split('pyMS')[0]+'pyMS'+os.sep+'test'+os.sep+'testfiles'+os.sep+'testfile.txt'
What would be a better way to handle this?
You can standardize by doing an
os.chdir()to the appropriate place. For running tests, I can’t think of any reason not to.