Skip to content Skip to sidebar Skip to footer

Invalidmoduleerror() When Using Testbed To Unit Test Google App Engine

I’ve been wrestling with a couple of errors when trying to write some Python unit tests for a Google App Engine app that consists of a number of modules. I have been following th

Solution 1:

The issue with needing to call init_modules_stub is connected to what release of the SDK (and thus of testbed/__init__.py) you have; that's been added to init_all_stubs in the current release (not sure when exactly) so an upgrade should let you remove the need for the explicit call. But as you say, not the main problem.

But on to the more substantial problem -- by my lights you've done nothing wrong, because no documentation says you should do anything in particular to initialize the modules' stub.

Fortunately, a work-around isn't too terrible. Specifically, you could have, early in your unit-test code, the initialization:

from google.appengine.api import request_info

# edit all_versions per modules & versions thereof needing tests
all_versions = {'default':[1], 'andsome':[2], 'others':[1]}
def_versions = {m:all_versions[m][0] for m in all_versions}
m2h = {m:{def_versions[m]:'localhost:8080'} for m in def_versions}

request_info._local_dispatcher = request_info._LocalFakeDispatcher(
    module_names = list(all_versions),
    module_name_to_versions = all_versions,
    module_name_to_default_versions = def_versions,
    module_name_to_version_to_hostname = m2h)

assuming of course that these are the module names and versions you want!

Yes, it should definitely be easier (the testbed or some module should expose a function doing this -- ideally by parsing appropriate yaml files, but at least with explicit arguments) and, very importantly, it should be well documented.

Having been the first author of a precursor of testbed's first version five years ago, I personally apologize for not keeping an eye on it (sorry -- I was busy with very different jobs in the meantime! -- but, being a fanatic about unit-testing, I should have used some 20% time on this).

Please do open a feature request about exposing this nicely and documenting it, and thanks both for your patience and for your excellent "detective work" identifying the crux of the problem!

Post a Comment for "Invalidmoduleerror() When Using Testbed To Unit Test Google App Engine"