I am sure this is simple to solve, but I have been staring at it for so long that I am staring straight past the solution. Any tips/hints/suggestions/solutions appreciated!
I am trying to add some unittests to my app engine app. I am using the testrunner.py example from http://code.google.com/appengine/docs/python/tools/localunittesting.html (bottom of the page). This works fine if I put the unittest file (named ‘test_lib.py’) in the root of the app (myapp), however I want to move the tests to a separate subdirectory (named ‘tests’) within the app. Now I need to import some of the modules from the app, however since the working directory of the unittest file is now one level deeper, it does not sees the actual modules from my app engine app.
I tried adding a __ init__.py to the tests and added the following code to the it:
import os
import sys
sys.path.append(os.path.dirname(os.getcwd()))
I hoped this would find the current working directory and go one level up and add this to the sys.path and from that I would be able to import ‘util.lib’ in the unittest file (‘test_lib.py’). However when I run the testrunner.py I still get error “ImportError: No module named util.lib” -> I am trying here to import a module called lib within a subdirectory called util within the root ‘myapp’. My directory structure is as follows:
testrunner.py
|- myapp
|- __init__.py
|- util
|- __init__.py
|- lib.py
|- tests
|- __init__.py ## this file has the import mentioned above.
|- test_lib.py
I also tried adding the import of the root of the app to the testrunner but this returns the same error.
def main(sdk_path, test_path):
sys.path.append(os.path.dirname(test_path)) ## This line I added to the testrunner.
sys.path.insert(0, sdk_path)
import dev_appserver
dev_appserver.fix_sys_path()
suite = unittest2.loader.TestLoader().discover(test_path)
unittest2.TextTestRunner(verbosity=2).run(suite)
And I am calling the test with the following command:
./testrunner.py ~/sdk/google_appengine/ myapp/tests/
Any suggestions what I am missing here?
Have you tried to use an absolute path?