在Django測試完成之后,如何保存測試數據?

[英]How can I keep test data after Django tests complete?


I am using Django 1.8 and the docs say to use --keepdb to save the test database.

我正在使用Django 1.8和文檔所說的使用——keepdb來保存測試數據庫。

I am doing that and the database is there but every time I see it, it is empty and has no data in it.

我正在這樣做,數據庫就在那里,但每次我看到它時,它都是空的,並且沒有數據。

Is there any way that I can preserve that so that I can see what's in there?

有什么辦法可以保存它,讓我看到里面有什么?

1 个解决方案

#1


9  

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

所有的代碼都運行在數據庫事務中,在每次測試結束時都會回滾這些事務。

From the Django testing docs:

來自Django測試文檔:

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:

下面是一個來自django.test的子類示例。TestCase,它是unittest的一個子類。在事務中運行每個測試以提供隔離的TestCase:

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.

相反,您希望使用Python的class unittest.TestCase。

Another quote from the Django docs:

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.

使用unittest。TestCase避免了在事務中運行每個測試和刷新數據庫的成本,但是如果您的測試與數據庫交互,它們的行為將根據測試運行者執行它們的順序而變化。這可能導致單元測試在單獨運行時通過,但在套件中運行時失敗。

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.

只要您能夠保證您的測試不會破壞彼此的數據,您就可以安全地使用這個類而不是Django的測試用例。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/28/724d962f31129eeb4cbeefd599384919.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com