Django 测试完成后如何保留测试数据?

How can I keep test data after Django tests complete?

我正在使用 Django 1.8,the docs 说要使用 --keepdb 来保存测试数据库。

我正在这样做,数据库就在那里,但每次我看到它时,它都是空的,里面没有数据。

有什么方法可以保留它以便我可以看到里面有什么?

您的所有代码都在 运行 数据库事务中,在每次测试结束时回滚。

来自Django testing docs

Here is an example which subclasses from django.test.TestCase, which is a subclass of unittest.TestCase that runs each test inside a transaction to provide isolation:

这种“隔离”意味着您在测试内部所做的任何事情都将在下一次测试开始之前回滚。

相反,您想使用 Python 的 class unittest.TestCase

Django 文档中的另一段引述:

Using unittest.TestCase avoids the cost of running each test in a transaction and flushing the database, but if your tests interact with the database their behavior will vary based on the order that the test runner executes them. This can lead to unit tests that pass when run in isolation but fail when run in a suite.

只要你能保证你的测试不会破坏彼此的数据,你就可以安全地使用这个 class 而不是 Django 的测试用例。