Python的unittest和PyMongo有一个奇怪的问题.测试随机成功或失败:
import unittest
from pymongo import Connection
from tractor import Tractor
class TestTractor(unittest.TestCase):
def setUp(self):
self.tractor = Tractor(1)
self.mongo = Connection()
self.db = self.mongo.tractor
self.db.classes.remove({'name': {'$regex':'^test_'}})
self.action_class_id = self.db.classes.insert({'name': 'test_action','Metaclass': 'action'})
self.object_class_id = self.db.classes.insert({'name': 'test_object','Metaclass': 'object'})
def tearDown(self):
self.tractor = None
def test_create_class(self):
cid1 = self.tractor.create_action_class('test_create_action_class')
cid2 = self.tractor.create_object_class('test_create_object_class')
self.assertNotEqual(cid1,None)
self.assertNotEqual(cid2,None)
action_obj = self.db.classes.find_one({'_id': cid1})
object_obj = self.db.classes.find_one({'_id': cid2})
self.assertNotEqual(cid1,cid2)
self.assertEqual(action_obj['_id'],cid1)
self.assertEqual(object_obj['_id'],cid2)
self.assertEqual(action_obj['name'],'test_create_action_class')
self.assertEqual(object_obj['name'],'test_create_object_class')
正在测试的类:
from pymongo import Connection
from pymongo.objectid import ObjectId
class Tractor(object):
def __init__(self,uid):
self.uid = uid
self.mongo = Connection()
self.db = self.mongo.tractor
# Classes
def create_action_class(self,name):
return self.db.classes.insert({'name': name,'attributes': [],'Metaclass': 'action'})
def create_object_class(self,'Metaclass': 'object'})
随机行为:
silver@aregh-6930-lnx ~/projects/traction/tractor $python -m unittest discover
......ssEssssssssss
======================================================================
ERROR: test_create_class (tests.test_tractor.TestTractor)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/silver/projects/traction/tractor/tests/test_tractor.py",line 64,in test_create_class
self.assertEqual(action_obj['_id'],cid1)
TypeError: 'NoneType' object is not subscriptable
----------------------------------------------------------------------
Ran 19 tests in 0.023s
Failed (errors=1,skipped=12)
…
silver@aregh-6930-lnx ~/projects/traction/tractor $python -m unittest discover
......ss.ssssssssss
----------------------------------------------------------------------
Ran 19 tests in 0.015s
OK (skipped=12)
这两个结果随机发生在同一测试中,因为我重新运行测试而不改变类中和测试中的任何内容.
所有这些都在我的机器上运行,我确信在运行测试时,没有其他任何东西都没有使用MongoDB和代码.
是什么赋予了?
最佳答案
原文链接:https://www.f2er.com/python/439268.html