Django何时使用拆卸方法 [英] Django when to use teardown method
问题描述
根据文档:
另一方面,TestCase不会截断表并重新加载$ b $ b初始数据在测试的开始。而是在
测试结束时回滚的数据库事务中包含测试
代码。它还防止被测代码在数据库上发出任何commit或
回滚操作,以确保在测试结束时
的回滚将数据库恢复到初始状态。在
中,为了保证所有的TestCase代码以一个干净的
数据库开始,Django测试运行程序首先运行所有TestCase测试,然后才能
任何其他可能会改变数据库的测试(例如doctests)
将其恢复到原始状态。
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.
所以如果我有一个如下所示的测试:
So if I have a test that looks like this:
class GeneralUserCreateTest(TestCase):
def setUp(self):
create_roletypes()
create_permissiontypes()
self.client = Client()
self.event = create_event()
def test_create(self):
create_url = reverse('event_user_signup', args=[self.event.slug])
post_data = {
'signup-account-email': 'foo@bar.com',
'signup-account-password': 'foobar',
'signup-account-password2': 'foobar',
'signup-account-first_name': 'Foo',
'signup-account-last_name': 'Bar',
}
response = self.client.post(create_url, data=post_data)
self.assertEqual(response.status_code, 302)
# check creation of user object
self.assertEqual(User.objects.filter(email=post_data['signup-account-email']).count(), 1)
user = User.objects.get(username=post_data['signup-account-email'])
# user and profile objects created
self.assertEqual(User.objects.all().count(), 1)
self.assertEqual(Profile.objects.all().count(), 1)
# get the first user and profile object to test against submitted field
user = User.objects.all()[0]
profile = Profile.objects.all()[0]
role = Role.objects.filter(event=self.event, profiles=profile)[0]
self.assertEqual(role.roletype.name, 'General')
self.assertEqual(user.username, post_data['signup-account-email'])
self.assertEqual(user.email, post_data['signup-account-email'])
self.assertEqual(profile.first_name, post_data['signup-account-first_name'])
self.assertEqual(profile.last_name, post_data['signup-account-last_name'])
仍然需要运行拆卸
方法,或者 TestCase
类照顾它?如果是的话,考虑到$ code> TestCase 类的可用性,应该何时使用拆卸
方法?
Is it still necessary to run a teardown
method or does the TestCase
class take care of it? If so, when should one use the teardown
method given the availability of the TestCase
class?
推荐答案
为了数据库的目的, tearDown
是相当毫无意义的,因为每个测试都运行在一个交易。但是,并非测试中的所有内容都涉及数据库。您可以测试文件创建/阅读,脱离进程,打开网络连接等。这些类型的事情通常需要您在完成后关闭它们。这就是 tearDown
,即清理您的 setUp
方法中与数据库无关的内容。 (虽然,如果你实际上直接连接到一个数据库,就像实际的Django测试必须做的那样,以确保所有的DBAPI的东西都能正常工作,那么你也需要在那里清理。)
For the purposes of the database, tearDown
is pretty pointless, because each test is run in a transaction. However, not everything in a test involves the database. You might test file creation/reading, spin off processes, open network connections, etc. These types of things usually require you to "close" them after you're done. This is what tearDown
is for, i.e. cleaning up stuff from your setUp
method, not related to the database. (Though, if you were actually directly connecting to a database, i.e. as the actual Django tests must do to make sure all the DBAPI stuff works properly, you'd need to do clean up there too.)
这篇关于Django何时使用拆卸方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!