我正在尝试对一个模型做一个简单的测试。我插入并检索模型,并检查插入的所有数据是否存在。我预计这个测试会在一个简单的空白模型中失败,但它通过了。这是我不得不接受的测试框架的一种怪癖吗?我可以设置一个选项来阻止它保留对python对象的引用吗?
在下面的代码中,我预计它会在第30行失败,但它没有失败。它在裁判比较中失败了,因为我坚持认为裁判是不同的,而他们并不是。
import unittest
from google.appengine.ext import ndb
from google.appengine.ext import testbed
class Action(ndb.Model): pass
class ActionTestCase(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()
self.testbed.init_datastore_v3_stub()
self.testbed.init_memcache_stub()
def tearDown(self):
self.testbed.deactivate()
def testFetchRedirectAttribute(self):
act = Action()
act.attr = 'test phrase'
act.put()
self.assertEquals(1, len(Action.query().fetch(2)))
fetched = Action.query().fetch(2)[0]
self.assertEquals(fetched.attr, act.attr)
self.assertTrue(act != fetched)
if __name__ == '__main__':
unittest.main()发布于 2014-06-29 18:46:43
如果模型的所有属性都相等,则模型被定义为相等。如果您关心的是身份(您可能不应该……),那么您可以在测试中使用assertIs。
发布于 2014-07-01 01:06:48
事实证明,存储引用是存根的行为。然而,出于TDD的目的,我们确实需要检查是否在模型中定义了属性。这样做的简单方法是使用关键字参数。如果我按照下面的方式编写测试,那么它会像预期的那样失败。
def testFetchRedirectAttribute(self):
act = Action(attr='test phrase)
act.put()这解决了我的当务之急,那就是有一个失败的地方,我可以针对它进行编码。
https://stackoverflow.com/questions/24473574
复制相似问题