【问题标题】:Google app engine: unitest works nosetest fails谷歌应用引擎:unitest works nosetest 失败
【发布时间】:2014-10-20 14:33:03
【问题描述】:

我编写了这个简单的代码来测试我的模型:

class NDBTestCase(unittest.TestCase):
    def setUp(self):
        logging.getLogger().setLevel(logging.INFO)
        # 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()
        self.owner = m_User(username="owner")
        self.owner.put()


    def tearDown(self):
        self.testbed.deactivate()


    def testClub(self):
        # this id is a manually assigned
        club = m_Club(id="1", name="test", email="test@test.com", description="desc", url="example.com",
                      owners=[self.owner.key], training_type=["balance", "stability"], tags=["test", "trento"])
        club.put()

在模型中owners 是这样的owners = ndb.KeyProperty(kind="User", repeated=True)

如果我使用 unittest 运行此代码,它会完美运行。 我尝试使用unitestnosegae 运行它,但由于Key 的问题而失败

Traceback (most recent call last):
  File "/Users/stefano/Documents/SW/gymcentral/tester_ndb.py", line 25, in setUp
    self.owner.put()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 3379, in _put
    return self._put_async(**ctx_options).get_result()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/tasklets.py", line 325, in get_result
    self.check_success()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/tasklets.py", line 368, in _help_tasklet_along
    value = gen.throw(exc.__class__, exc, tb)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/context.py", line 810, in put
    key = yield self._put_batcher.add(entity, options)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/tasklets.py", line 371, in _help_tasklet_along
    value = gen.send(val)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/context.py", line 350, in _put_tasklet
    ent._key = key
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 1363, in __set__
    self._set_value(entity, value)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 1513, in _set_value
    value = _validate_key(value, entity=entity)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 1481, in _validate_key
    raise datastore_errors.BadValueError('Expected Key, got %r' % value)
BadValueError: Expected Key, got Key('User', 1)

知道为什么吗? 我使用此命令从控制台运行测试nosetests tester_ndb.py --with-gae

【问题讨论】:

    标签: python unit-testing google-app-engine nosetests nose-gae


    【解决方案1】:

    你能不能用--without-sandbox的旗子流鼻涕?

    以前的跟踪器上似乎有一个关于同一件事的旧问题。

    https://code.google.com/p/nose-gae/issues/detail?id=60

    我最近接管了 NoseGAE 的维护工作,因此我也会调查根本原因,但我假设这是 App Engine SDK 内部的问题。

    编辑:当我将 --without-sandbox 迁移到 dev_appserver2 时,在 NoseGAE 0.4.0 中删除了它

    【讨论】:

    • 以防万一有人需要它nosetests --logging-level=ERROR --with-gae --without-sandbox --no-path-adjustment --gae-application=.
    【解决方案2】:

    我也遇到过类似的奇怪错误。我认为谷歌代码中的某个地方存在错误。我通过调整我的代码以稍微不同的方式做同样的事情来解决它。

    尝试改变

    self.owner.put()
    

    ndb.put(self.owner)
    

    【讨论】:

    • 嗯,很有趣。我会避免“破解”以使其正常工作,并且我想了解为什么正常代码不起作用。另外,谷歌已经在管道中使用鼻子“测试和部署”测试用例,所以我想知道如果它无法通过这样的简单测试,它会如何工作..
    • 你导入什么来使用ndb.put?我导入了from google.appengine.ext import ndbAttributeError: 'module' object has no attribute 'put'
    • 我同意这不是一个理想的解决方案(如果它甚至适合您)。我仍在使用db,那里有一个db.put()。我认为ndb 的情况相同,但您必须查看文档以查看是否有类似的替代方法。
    • 另一个答案解决了这个问题。检查一下,它应该也适合你。
    猜你喜欢
    • 2014-03-02
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 2011-05-06
    相关资源
    最近更新 更多