# ... only if version >= 2.7
with self.assertRaises(IntegrityError):
db.session.commit()
或者:
^{pr2}$
您的示例与正确方法的区别在于:
# Your example: You call db.session.commit(), this will raise an exception before
# assertRaises is called
self.assertRaises(IntegrityError, db.session.commit())
# Correct way: Pass what should be called to assertRaises,
# let assertRaises invoke it and check for exception
self.assertRaises(IntegrityError, db.session.commit)
其中一个将是关键:
或者:
^{pr2}$您的示例与正确方法的区别在于:
我更喜欢使用assertRaises作为上下文管理器(使用
with
)。在相关问题 更多 >
编程相关推荐