3
我的测试没有捕获与assertRaises psycopg2.IntegrityError。我正在使用Flask-SQLAlchemy。assertRaises没有捕获IntegrityError,Flask SQLAlchemy
def test_insert_cash_flow(self):
cf = CashFlow()
db.session.add(cf)
self.assertRaises(psycopg2.IntegrityError, db.session.commit)
我的CashFlow SQLAlchemy模型有几个可空= False字段。它说我的测试失败,IntegrityError被打印到屏幕上,但是我的assertRaises没有抓住这一点。有人怀疑为什么?
奇怪...确实。你有没有尝试在with语句中使用self.assertRaises,在文档中有概述? https://docs.python.org/2/library/unittest.html – ohe
你确定你有'db.session.commit'而不是'db.session.commit()'吗? – alecxe
相关或重复:http://stackoverflow.com/q/19289291/771848。 – alecxe