A TestCase, on the other hand, does not truncate tables after a test. Instead, it encloses the test code in a database transaction that is rolled back at the end of the test. This guarantees that the rollback at the end of the test restores the database to its initial state.
从documentation开始,底部部分解释了}之间的区别:
TransactionTestCase
和{由于每个单独的测试方法都被包装在一个
atomic
块中,并且每个原子块在测试方法结束时被回滚,这是预期的行为。Django这样做是为了确保测试是孤立的,这样在出现问题时更容易发现问题。在如果需要在多个测试方法中持久化数据,可以在^{} 中创建它们。只需确保不要更改
setUpTestData()
创建的内存中对象。内存中的更改将持续,但数据库更改将回滚。在相关问题 更多 >
编程相关推荐