How can I keep test data after Django tests complete?

ぃ、小莉子 提交于 2019-12-05 01:00:59

All of your code is running within database transactions, which get rolled back at the end of each test.

From the 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:

This "isolation" means that anything you do inside of the test will be rolled back before the next test starts.

Instead, you want to use Python's class unittest.TestCase.

Another quote from the Django docs:

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.

As long as you can guarantee that your tests won't clobber each other's data, you can safely use this class instead of Django's test case.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!