如何测试禁用 autocommit 的代码并在 Django 单元测试中使用保存点?
默认的 Django unittest 类将所有测试包装在 @atomic
装饰器中,这通常正是您想要的,以确保在测试之间重置 sqlite 数据库。但是,任何从测试中触及 transaction.set_autocommit()
的代码都会抛出错误:
TransactionManagementError: This is forbidden when an 'atomic' block is active.
即使它在单元测试之外工作正常。
如何在单元测试中暂时禁用事务自动提交,以便测试手动提交?
最佳答案
TransactionTestCase
andTestCase
are identical except for the manner in which the database is reset to a known state and the ability for test code to test the effects of commit and rollback: ATransactionTestCase
resets the database after the test runs by truncating all tables. ATransactionTestCase
may call commit and rollback and observe the effects of these calls on the database.
关于python - 在 Django 单元测试中测试手动事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51164697/