Python Django 单元测试:如何清除固定装置

2024-10-02 16:33:39 发布

您现在位置:Python中文网/ 问答频道 /正文

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

    from django.test import TestCase    

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

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

现在我想知道在我的tearDown方法中写些什么来删除从fixture中生成的组和用户。还是自动删除?在

^{pr2}$

Tags: djangofromtestimportjson应用程序deftestcase
1条回答
网友
1楼 · 发布于 2024-10-02 16:33:39

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

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.

相关问题 更多 >