【发布时间】:2013-12-19 17:42:29
【问题描述】:
我正在使用 NoseGAE 为我的 App Engine 应用程序编写本地单元测试,但是我的一个测试突然出现了问题。我有标准的 setUp 和 tearDown 功能,但一个测试似乎因为我无法辨别的原因而中断。即使是陌生人,setUp 和 tearDown 也不会每次都被调用。我添加了全局变量来计算 setUp/tearDown 调用,在我的第 4 次测试(现在看似损坏的测试)中,setUp 被调用了两次,tearDown 被调用了一次。此外,当我通过 id 查询它时,第三个测试中的一个对象存在,但在其类型的一般查询中不存在。下面是一些给出奇异画面的代码:
class GameTest(unittest.TestCase):
def setUp(self):
self.testapp = webtest.TestApp(application)
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_datastore_v3_stub(
consistency_policy=datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1),
require_indexes=True,
root_path="%s/../../../" % os.path.dirname(__file__)
)
def tearDown(self):
self.testbed.deactivate()
self.testapp.cookies.clear()
def test1(self):
...
def test2(self):
...
def test3(self):
...
# I create a Game object with the id 123 in this particular test
Game(id=123).put()
...
def test4(self):
print "id lookup: ", Game.get_by_id(123)
print "query: ", Game.query().get()
self.assertIsNone(Game.get_by_id(123))
这是测试的抽象,但说明了问题。
第 4 次测试失败,因为它断言具有该 ID 的对象不存在。当我打印出这两个语句时:
id 查找:Game(key=Key('Game', 123))
查询:无
id 查找显示在 test3 中创建的对象,但查询查找为 EMPTY。这对我来说完全没有意义。此外,我 100% 确定该测试在更早之前进行。有谁知道这怎么可能?我可能有一些本地损坏的文件导致问题吗?
【问题讨论】:
-
除非我使用--without-sandbox 运行nosetests,否则我永远无法让nose-gae 与NDB 一起正常工作。它以奇怪和令人困惑的方式失败,(似乎)没有一致的理由......我希望有人能给出一个体面的答案,但同时尝试使用该选项可能会对您有所帮助。
-
谢谢 Greg,但我应该提一下,我已经用那个标志运行它了。
-
这是
PseudoRandomHRConsistencyPolicy集合的概率 -
为了解决您对调用 setUp 和 tearDown 的次数的第二个顾虑,我已将此添加到 setUp 以避免任何不一致,我也遇到过这种情况:
def setUp(self): if not hasattr(self, "_test_setup_complete") or not self._test_setup_complete: self._test_setup_complete = True -
HR 一致性策略的概率设置为 1,所以这不是问题。我注意到并且没想到的是,测试没有按照我在 unittest.TestCase 类上编写的顺序运行。这就是为什么我认为 setUp/tearDown 没有被调用存在问题;我在第 4 次测试中看到了 2 个 setUp 调用,但实际上,第 4 次测试实际上只是称为 2nd。
标签: google-app-engine unit-testing