Search code examples
pythongoogle-app-engineweb-testing

Handler testing in App Engine fails: webtest import errors


I can't make any sense of this error:

======================================================================
ERROR: test_webtest (unittest2.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_webtest
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/unittest2/loader.py", line 260, in _find_tests
    module = self._get_module_from_name(name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/unittest2/loader.py", line 238, in _get_module_from_name
    __import__(name)
  File "/Users/chris/Documents/gae/tasker/test/test_webtest.py", line 4, in <module>
    import webtest
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/webtest/__init__.py", line 9, in <module>
    from webtest.app import TestApp
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/webtest/app.py", line 65, in <module>
    class TestRequest(webob.BaseRequest):
AttributeError: 'module' object has no attribute 'BaseRequest'

The situation is:

  • I'm trying to set up handler unit tests for my App Engine app.
  • I'm following this tutorial for a unit test framework, including the bash script at the bottom of the page, verbatim.
  • I'm running tests from the terminal with this command:

    ./run_tests.py /usr/local/google_appengine test/

  • I'm using this tutorial for structuring my handler tests. But it never gets past the imports. All I have to do is this in test/test_webtest.py to generate the error:

    import webtest
    
  • I have webtest installed:

    $ pip install WebTest
    Requirement already satisfied (use --upgrade to upgrade): WebTest in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
    Requirement already satisfied (use --upgrade to upgrade): six in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from WebTest)
    Requirement already satisfied (use --upgrade to upgrade): WebOb>=1.2 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from WebTest)
    Requirement already satisfied (use --upgrade to upgrade): waitress in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from WebTest)
    Requirement already satisfied (use --upgrade to upgrade): beautifulsoup4 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from WebTest)
    Requirement already satisfied (use --upgrade to upgrade): distribute in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/distribute-0.6.28-py2.7.egg (from waitress->WebTest)
    
  • I am running all my tests at once, including older ones that pass with no errors. But importing webtest into any of them generates the error at the top. Why?


Solution

  • This turned out to be a silly problem with my different python environments.

    The pip and python in my path were linked to /user/local/bin/python. That's where webtest was, in fact, installed.

    The code I had copied from the tutorial, however, had this hashbang: #!/usr/bin/python so when I ran it with ./run_tests.py, it ran in the environment where webtest wasn't installed.

    Running either python ./run_tests.py or changing the hashbang to #!/user/local/bin/python fixed the problem.