我正在尝试使用他们在Python的本地单元测试页面(https://cloud.google.com/appengine/docs/python/tools/localunittesting)上放置的确切代码来学习使用Google App Engine进行单元测试。但我无法弄清楚这个错误:
ImportError: Start directory is not importable: 'testmem.py'
我只是将他们的简单测试框架用作testrunner.py,并将他们的数据存储和Memcache测试用于名为testmem.py的文件中。我将项目根目录中的测试称为:
<me>$ python testrunner.py ~/google_appengine testmem.py
这符合我的用法:%prog SDK_PATH TEST_PATH。
我的文件结构是:
__init__.py
app.yaml
testrunner.py
testmem.py
helloworld.py
谁能告诉我这里我做错了什么?提前致谢。
完整的错误消息:
Traceback (most recent 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 discover
raise ImportError('Start directory is not importable: %r' % start_dir)
ImportError: Start directory is not importable: 'testmem.py'
testrunner.py:
#!/usr/bin/python
import optparse
import sys
import unittest
USAGE = """%prog SDK_PATH TEST_PATH
Run unit tests for App Engine apps.
SDK_PATH Path to the SDK installation
TEST_PATH Path to package 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: Exactly 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
from google.appengine.api import memcache
from google.appengine.ext import db
from google.appengine.ext import testbed
class TestModel(db.Model):
"""A model class used for testing."""
number = db.IntegerProperty(default=42)
text = db.StringProperty()
class TestEntityGroupRoot(db.Model):
"""Entity group root"""
pass
def GetEntityViaMemcache(entity_key):
"""Get entity from memcache if available, from datastore if not."""
entity = memcache.get(entity_key) # @UndefinedVariable
if entity is not None:
return entity
entity = TestModel.get(entity_key)
if entity is not None:
memcache.set(entity_key, entity) # @UndefinedVariable
return entity
class DemoTestCase(unittest.TestCase):
def setUp(self):
# First, create an instance of the Testbed class.
self.testbed = testbed.Testbed()
# Then activate the testbed, which prepares the service stubs for use.
self.testbed.activate()
# Next, declare which service stubs you want to 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()
答案 0 :(得分:10)
此处,“测试路径”应该是包含要运行的测试的目录,而不是包含单个模块的路径。尝试使用.
作为目录(假设您从顶级项目/ app目录运行它),看看是否有帮助。
我的团队将测试保存在与源代码不同的目录中,因此我们将使用测试目录的路径作为第二个参数。
答案 1 :(得分:0)
确保tests目录的拼写是“ tests”而不是“ test”。这就是我的问题。