Import path for unit tests
Duncan Booth
duncan.booth at invalid.invalid
Fri Dec 2 08:48:37 EST 2005
Ben Finney wrote:
> This works, so long as the foomodule is *not* in the path before the
> appended '..' directory. When writing unit tests for a development
> version of a package that is already installed at an older version in
> the Python path, this fails: the unit tests are not importing the
> development version of foomodule.
Why not just insert foomodule's directory in front of the other entries in
sys.path instead of at the end?
>
> What is the common idiom here? I can conceive of several possible ways
> to get around it, all of which seem hackish to some degree.
I don't know if it is the common idiom, but I tend to write:
TESTDIR = os.path.dirname(os.path.abspath(__file__))
PROJECTDIR = os.path.dirname(TESTDIR)
if not TESTDIR in sys.path:
sys.path.insert(1, TESTDIR)
if not PROJECTDIRDIR in sys.path:
sys.path.insert(1, PROJECTDIR)
That gets put in a framework.py file in the test directory. run_tests.py
(in the same folder) looks like:
import unittest, os, sys
if __name__ == '__main__':
execfile(os.path.join(sys.path[0], 'framework.py'))
if __name__=='__main__':
suite = unittest.TestSuite()
for testfile in os.listdir(TESTDIR):
if testfile.startswith('test_') and testfile.endswith('.py'):
testfile = os.path.splitext(testfile)[0]
module = __import__(testfile)
suite.addTest(module.suite())
unittest.TextTestRunner(verbosity=2).run(suite)
and all the test files import framework. That way I can run an individual
test_xxx.py, or use run_tests.py to run all test files, and I can start
them from the test directory, the project directory, or any other
directory.
More information about the Python-list
mailing list