gpt4 book ai didi

Python Django 单元测试 : How to tear down fixtures

转载 作者:太空宇宙 更新时间:2023-11-03 18:36:13 25 4
gpt4 key购买 nike

我需要为我的 Django 应用程序编写一些测试。为此,我使用的是这样加载的装置:

    from django.test import TestCase    

class PermissionTest(TestCase):
fixtures = ['test_groups.json','test_users.json']

def setUp(self):
... some other stuff

现在我想知道在我的tearDown-Method 中写什么来删除从我的装置生成的组和用户。或者它们会被自动删除?

        def tearDown(self):
... what has to go here?

最佳答案

在运行每个测试之前,django 的测试框架将删除已加载的装置。所以你不需要处理这个。

来自docs :

Here’s specifically what will happen:

At the start of each test case, before setUp() is run, Django will flush the database, returning the database to the state it was in directly after migrate was called.

Then, all the named fixtures are installed. In this example, Django will install any JSON fixture named mammals, followed by any fixture named birds. See the loaddata documentation for more details on defining and installing fixtures.

This flush/load procedure is repeated for each test in the test case, so you can be certain that the outcome of a test will not be affected by another test, or by the order of test execution.

关于Python Django 单元测试 : How to tear down fixtures,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21550032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com