[英]How do you assert database error using Python's unittest?
I am running Python 2.6.5, so I use unittest2
module, which is the future port of the unittest module in 2.7 and 3.X. 我正在运行Python 2.6.5,所以我使用
unittest2
模块,它是2.7和3.X中unittest模块的未来端口。 I am performing the following integration test: 我正在执行以下集成测试:
def test_hits_constraint_raise(self):
obj = Table1(...)
self.sess.add(obj)
self.sess.flush()
# assert condition raises
self.assertFailure(IntegrityError, sess.add, obj)
Instead, it hits error. 相反,它会出错。
======================================================================
ERROR: test_hits_constraint_raise (__main__.TestModels)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_models.py", line 76, in test4_create_exercise_same_id_constraint_raise
self.sess.flush()
File "build/bdist.linux-i686/egg/sqlalchemy/orm/scoping.py", line 114, in do
return getattr(self.registry(), name)(*args, **kwargs)
.........
File "build/bdist.linux-i686/egg/sqlalchemy/engine/default.py", line 331, in do_execute
cursor.execute(statement, parameters)
IntegrityError: (IntegrityError) column exercise_id is not unique u'INSERT INTO TABLE1 (attribute1_name, attribute2_name) VALUES (?, ?)' ('value1', 'value2')
----------------------------------------------------------------------
Ran 4 tests in 0.531s
FAILED (errors=1)
Is the only way to do is use @unittest2.expectedFailure
? 唯一的方法是使用
@unittest2.expectedFailure
吗? That one will run fine. 那一个会运行正常。 But I want to know exactly what kind of error was raised.
但我想确切地知道引起了什么样的错误。
----------------------------------------------------------------------
Ran 4 tests in 0.513s
OK (expected failures=1)
You can use assertRaises
as a with
statement context: 您可以将
assertRaises
用作with
语句上下文:
with unittest.assertRaises(IntegrityError):
sess.add(obj)
More details . 更多细节 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.