Monday, 15 September 2014

python - App Engine Unit Testing: ImportError: Start directory is not importable -



python - App Engine Unit Testing: ImportError: Start directory is not importable -

i'm trying larn unit testing google app engine using exact code set on local unit testing python page (https://cloud.google.com/appengine/docs/python/tools/localunittesting). can't figure out error, though:

importerror: start directory not importable: 'testmem.py'

i'm using simple testing framework testrunner.py , datastore , memcache tests in file called testmem.py. phone call test project root directory as:

<me>$ python testrunner.py ~/google_appengine testmem.py

this matches usage far can tell: %prog sdk_path test_path.

my file construction is:

__init__.py app.yaml testrunner.py testmem.py helloworld.py

can tell me i'm doing wrong here? in advance.

complete error message:

traceback (most recent phone call last): file "testrunner.py", line 30, in <module> main(sdk_path, test_path) file "testrunner.py", line 17, in main suite = unittest.loader.testloader().discover(test_path) file "/usr/lib/python2.7/unittest/loader.py", line 204, in find raise importerror('start directory not importable: %r' % start_dir) importerror: start directory not importable: 'testmem.py'

testrunner.py:

#!/usr/bin/python import optparse import sys import unittest usage = """%prog sdk_path test_path run unit tests app engine apps. sdk_path path sdk installation test_path path bundle containing test modules""" def main(sdk_path, test_path): sys.path.insert(0, sdk_path) import dev_appserver dev_appserver.fix_sys_path() suite = unittest.loader.testloader().discover(test_path) unittest.texttestrunner(verbosity=2).run(suite) if __name__ == '__main__': parser = optparse.optionparser(usage) options, args = parser.parse_args() if len(args) != 2: print 'error: 2 arguments required.' parser.print_help() sys.exit(1) sdk_path = args[0] test_path = args[1] main(sdk_path, test_path)

testmem.py:

import unittest google.appengine.api import memcache google.appengine.ext import db google.appengine.ext import testbed class testmodel(db.model): """a model class used testing.""" number = db.integerproperty(default=42) text = db.stringproperty() class testentitygrouproot(db.model): """entity grouping root""" pass def getentityviamemcache(entity_key): """get entity memcache if available, datastore if not.""" entity = memcache.get(entity_key) # @undefinedvariable if entity not none: homecoming entity entity = testmodel.get(entity_key) if entity not none: memcache.set(entity_key, entity) # @undefinedvariable homecoming entity class demotestcase(unittest.testcase): def setup(self): # first, create instance of testbed class. self.testbed = testbed.testbed() # activate testbed, prepares service stubs use. self.testbed.activate() # next, declare service stubs want use. self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() def teardown(self): self.testbed.deactivate() def testinsertentity(self): testmodel().put() self.assertequal(1, len(testmodel.all().fetch(2))) def testfilterbynumber(self): root = testentitygrouproot(key_name="root") testmodel(parent=root.key()).put() testmodel(number=17, parent=root.key()).put() query = testmodel.all().ancestor(root.key()).filter('number =', 42) results = query.fetch(2) self.assertequal(1, len(results)) self.assertequal(42, results[0].number) def testgetentityviamemcache(self): entity_key = str(testmodel(number=18).put()) retrieved_entity = getentityviamemcache(entity_key) self.assertnotequal(none, retrieved_entity) self.assertequal(18, retrieved_entity.number) if __name__ == '__main__': unittest.main()

here, "test path" should directory contains tests want run, not path single module. seek using . directory (assuming you're running top-level project/app directory) , see if helps.

my team keeps our tests in separate directory our source code, utilize path our test directory sec argument.

python unit-testing google-app-engine

No comments:

Post a Comment