简体   繁体   English

Django测试:--keepdb重置测试期间所做的更改吗?

[英]Django Testing: Does --keepdb reset changes made during tests?

According to the Django docs regarding tests, the --keepdb flag will preserve the the test database for future runs. 根据有关测试的Django文档, - --keepdb标志将保留测试数据库以供将来运行。

https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-option---keepdb https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-option---keepdb

Just to be clear, will any changes made to the database by the tests (ie: object.save() ) be reset automatically? 为了清楚object.save() ,测试(即: object.save() )对数据库所做的任何更改都会自动重置吗? Or will these changes need to be reversed from within the tests? 或者这些变化是否需要在测试中反转?

If you're using Django's default TestCase , all tests are run in a transaction, which is rolled back when the tests finishes. 如果您使用的是Django的默认TestCase ,则所有测试都在一个事务中运行,该事务在测试完成后回滚。 If your database supports transactions, you won't have to clean up anything. 如果您的数据库支持事务,则不必清理任何内容。

If you're using Django's LiveServerTestCase or TransactionTestCase , all tables are truncated after each test, and the initial data, which is serialized before the test, is reloaded into the test database. 如果您使用的是Django的LiveServerTestCaseTransactionTestCase ,则每次测试后都会截断所有表,并将测试前序列化的初始数据重新加载到测试数据库中。 This will not preserve any data for migrated apps, only for unmigrated apps. 这不会为迁移的应用保留任何数据,仅适用于未迁移的应用。

The --keepdb option will not do anything special with the database. --keepdb选项不会对数据库执行任何特殊操作。 It simply prevents that the test database is destroyed, and if a database is found at the start of the tests, it is used instead of creating a new one. 它只是防止测试数据库被销毁,如果在测试开始时找到数据库,则使用它而不是创建新数据库。 So, any data that is somehow left in the database when the tests finish will be seen as initial data. 因此,在测试完成时以某种方式留在数据库中的任何数据都将被视为初始数据。 This is mostly relevant if some error or a user interrupt prevents tests without transactions from cleaning up the database. 如果某些错误或用户中断阻止没有事务的测试清理数据库,则这通常是相关的。 In that case it may be a good idea to recreate the database. 在这种情况下,重新创建数据库可能是个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM