Django单元测试数据库没有被拆除? [英] Django unit test database not being torn down?

查看:159
本文介绍了Django单元测试数据库没有被拆除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一些单元测试来测试我的Django应用程序。一个测试套件在 setUp()函数中有很多代码。所述代码的目的是为数据库创建测试数据。 (是的,我知道灯具,并在这种情况下选择不使用它们)。当我运行单元测试套件时,运行的第一个测试通过,但是套件中的其余测试失败。所有失败的消息是一样的:它提到错误的位置是self.database_object.save(),原因是IntegrityError:列名不唯一。所以,我最好的猜测是,Django在每次测试后都没有正确地拆除数据库。



今天早些时候,它正在工作,但我猜想有一些重构我搞砸了。任何想法,为什么Django在每次测试后都不能正确地拆除数据库?

解决方案

您是否使用TestCase或TransactionTestCase作为您的基础类?有时这种行为与Django对TestCase的优化有利于TransactionTestCase有关。以下是区别:



https://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.TransactionTestCase



< blockquote>

class TransactionTestCase



Django TestCase类使用数据库
事务设施(如果可用),以加快
在每次测试开始时将数据库重置为已知状态。
然而,这样做的结果是事务
提交和回滚的影响不能被Django TestCase类测试。如果
您的测试需要测试这种事务行为,您应该
使用Django TransactionTestCase。



TransactionTestCase和TestCase是完全相同的,除了数据库重置为已知状态的
的方式以及
测试代码测试提交和回滚效果的能力。 A
TransactionTestCase在测试运行
之前重置数据库,截断所有表并重新加载初始数据。 A
TransactionTestCase可以调用提交和回滚,并观察这些调用对数据库的
的影响。



TestCase另一方面,在测试开始时不截断表并重新加载$ ​​b $ b初始数据。而是在
测试结束时回滚的数据库事务中包含测试
代码
。它还防止被测代码在数据库上发出任何commit或
回滚操作,以确保在测试结束时
的回滚将数据库恢复到初始状态。在
中,为了保证所有的TestCase代码以一个干净的
数据库开始,Django测试运行程序首先运行所有TestCase测试,然后才能
任何其他可能会改变数据库的测试(例如doctests)
将其恢复到原始状态。



I have some unit tests I've written to test my Django application. One test suite in particular has a lot of code in its setUp() function. The purpose of said code is to create test data for the database. (Yes I know about fixtures and have chosen not to use them in this case). When I run the unit test suite the first test that is run passes, but then the rest of the tests in the suite fail. The message for all the failures is the same: it mentions that the error's location is "self.database_object.save()" and that the cause is "IntegrityError: column name is not unique". So, my best guess is that Django is not tearing down the database properly after each test.

Earlier today it was working, but I guess some refactoring I did messed it up. Any ideas on why Django is not properly tearing down the database after each test?

解决方案

Do you use TestCase or TransactionTestCase for your base class? Sometimes this behavior is related to the optimization Django makes for TestCase in favor of TransactionTestCase. Here is the difference:

https://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.TransactionTestCase

class TransactionTestCase

Django TestCase classes make use of database transaction facilities, if available, to speed up the process of resetting the database to a known state at the beginning of each test. A consequence of this, however, is that the effects of transaction commit and rollback cannot be tested by a Django TestCase class. If your test requires testing of such transactional behavior, you should use a Django TransactionTestCase.

TransactionTestCase and TestCase are identical except for the manner in which the database is reset to a known state and the ability for test code to test the effects of commit and rollback. A TransactionTestCase resets the database before the test runs by truncating all tables and reloading initial data. A TransactionTestCase may call commit and rollback and observe the effects of these calls on the database.

A TestCase, on the other hand, does not truncate tables and reload initial data at the beginning of a test. Instead, it encloses the test code in a database transaction that is rolled back at the end of the test. It also prevents the code under test from issuing any commit or rollback operations on the database, to ensure that the rollback at the end of the test restores the database to its initial state. In order to guarantee that all TestCase code starts with a clean database, the Django test runner runs all TestCase tests first, before any other tests (e.g. doctests) that may alter the database without restoring it to its original state.

这篇关于Django单元测试数据库没有被拆除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆