我正在尝试使用TDD构建一个简单的Google App Engine应用程序。
当我尝试导入我的Customer模型时,出现错误:
$ python functional_tests.py
Traceback (most recent call last):
File "functional_tests.py", line 4, in <module>
from models import Customer
File "/Users/Bryan/work/GoogleAppEngine/dermalfillersecrets/models.py", line 1, in <module>
from google.appengine.ext import ndb
ImportError: No module named google.appengine.ext
这是model.py中的代码:
from google.appengine.ext import ndb
class Customer(ndb.Model):
email = ndb.StringProperty()
name = ndb.StringProperty(indexed=True)
这是来自functional_tests.py的代码:
from selenium import webdriver
import unittest
from models import Customer
class NewVisitorTest(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
def tearDown(self):
self.browser.quit()
def test_quest_can_submit_contact_info(self):
self.browser.get('http://localhost:9080')
self.browser.find_element_by_name('id_name').send_keys("Kallie Wheelock")
self.browser.find_element_by_name('id_email').send_keys("kallie@gmail.com")
self.browser.find_element_by_name('submit').submit()
Customer.query(Customer.name("Kallie Wheelock")).delete()
答案 0 :(得分:1)
您的具体问题是您没有将应用引擎SDK导入到您的sys路径。 sys.path.append("/usr/local/google_appengine")
解决了ImportError问题。
但要完全解决您的TDD挑战,您必须使用dev_appserver和testbed与selenium一起使用。我采用了https://stackoverflow.com/a/20986246/710851中提出的解决方案并根据您的用例进行了配置。
import sys
sys.path.append("/usr/local/google_appengine")
sys.path.append("/usr/local/google_appengine/lib/yaml/lib")
sys.path.append("/usr/local/google_appengine/lib/webapp2-2.5.2")
sys.path.append("/usr/local/google_appengine/lib/django-1.5")
sys.path.append("/usr/local/google_appengine/lib/cherrypy")
sys.path.append("/usr/local/google_appengine/lib/concurrent")
sys.path.append("/usr/local/google_appengine/lib/docker")
sys.path.append("/usr/local/google_appengine/lib/requests")
sys.path.append("/usr/local/google_appengine/lib/websocket")
sys.path.append("/usr/local/google_appengine/lib/fancy_urllib")
sys.path.append("/usr/local/google_appengine/lib/antlr3")
def setUp(self):
# setup dev_appserver
APP_CONFIGS = ['app.yaml']
python_runtime._RUNTIME_ARGS = [
sys.executable,
os.path.join(os.path.dirname(dev_appserver.__file__),
'_python_runtime.py')
]
options = devappserver2.PARSER.parse_args([
'--admin_port', '0',
'--port', '9080',
'--datastore_path', ':memory:',
'--logs_path', ':memory:',
'--skip_sdk_update_check',
'--',
] + APP_CONFIGS)
server = devappserver2.DevelopmentServer()
server.start(options)
self.server = server
# setup selenium
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
# setup the testbed
self.tb = testbed.Testbed()
self.tb.setup_env(current_version_id='testbed.version')
self.tb.activate()
self.tb.init_user_stub()
self.tb.init_datastore_v3_stub()
self.tb.init_memcache_stub()
def tearDown(self):
self.browser.quit()
self.tb.deactivate()
self.server.stop()
import os
import sys
sys.path.append("/usr/local/google_appengine")
sys.path.append("/usr/local/google_appengine/lib/yaml/lib")
sys.path.append("/usr/local/google_appengine/lib/webapp2-2.5.2")
sys.path.append("/usr/local/google_appengine/lib/django-1.5")
sys.path.append("/usr/local/google_appengine/lib/cherrypy")
sys.path.append("/usr/local/google_appengine/lib/concurrent")
sys.path.append("/usr/local/google_appengine/lib/docker")
sys.path.append("/usr/local/google_appengine/lib/requests")
sys.path.append("/usr/local/google_appengine/lib/websocket")
sys.path.append("/usr/local/google_appengine/lib/fancy_urllib")
sys.path.append("/usr/local/google_appengine/lib/antlr3")
import dev_appserver
from google.appengine.tools.devappserver2 import devappserver2
from google.appengine.tools.devappserver2 import python_runtime
from google.appengine.ext import testbed
from selenium import webdriver
import unittest
from models import Customer
class NewVisitorTest(unittest.TestCase):
def setUp(self):
# setup the dev_appserver
APP_CONFIGS = ['app.yaml']
python_runtime._RUNTIME_ARGS = [
sys.executable,
os.path.join(os.path.dirname(dev_appserver.__file__),
'_python_runtime.py')
]
options = devappserver2.PARSER.parse_args([
'--admin_port', '0',
'--port', '9080',
'--datastore_path', ':memory:',
'--logs_path', ':memory:',
'--skip_sdk_update_check',
'--',
] + APP_CONFIGS)
server = devappserver2.DevelopmentServer()
server.start(options)
self.server = server
# setup selenium
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
# setup the testbed
self.tb = testbed.Testbed()
self.tb.setup_env(current_version_id='testbed.version')
self.tb.activate()
self.tb.init_user_stub()
self.tb.init_datastore_v3_stub()
self.tb.init_memcache_stub()
def tearDown(self):
self.browser.quit()
self.tb.deactivate()
self.server.stop()
def test_quest_can_submit_contact_info(self):
self.browser.get('http://localhost:9080')
self.browser.find_element_by_name('id_name').send_keys("Kallie Wheelock")
self.browser.find_element_by_name('id_email').send_keys("kallie@gmail.com")
self.browser.find_element_by_name('submit').submit()
Customer.query(Customer.name("Kallie Wheelock")).delete()
答案 1 :(得分:0)
在最新版本中,请务必添加此行
if 'google' in sys.modules:
del sys.modules['google']
以下完整示例
import sys
sys.path.append('/usr/local/google_appengine/')
sys.path.append('/usr/local/google_appengine/lib/yaml/lib/')
if 'google' in sys.modules:
del sys.modules['google']