简体   繁体   English

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

[英]Python Django Unittests: How to tear down fixtures

I need to write some tests for my Django-App. 我需要为Django-App编写一些测试。 For that I am using fixtures which I load like that: 为此,我使用的是这样加载的灯具:

    from django.test import TestCase    

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

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

Now I am wondering what to write in my tearDown-Method to delete the groups and users generated from my fixtures. 现在,我想知道在我的tearDown方法中应该写些什么,以删除从我的灯具生成的组和用户。 Or are they deleted automatically? 还是被自动删除?

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

Deletion of the loaded fixtures will be taken care of by django's testing framework before running each test. 在运行每个测试之前,django的测试框架将负责删除已加载的灯具。 So you don't need to handle this. 因此,您无需进行处理。

From the 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. 在每个测试用例的开始处,在运行setUp()之前,Django将刷新数据库,将数据库恢复为调用迁移后直接处于的状态。

Then, all the named fixtures are installed. 然后,安装所有命名的灯具。 In this example, Django will install any JSON fixture named mammals, followed by any fixture named birds. 在此示例中,Django将安装任何名为哺乳动物的JSON固定装置,然后安装任何名为bird的固定装置。 See the loaddata documentation for more details on defining and installing fixtures. 有关定义和安装固定装置的更多详细信息,请参见loaddata文档。

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. 对于测试用例中的每个测试,都会重复执行此刷新/加载过程,因此可以确定测试的结果不会受到其他测试或测试执行顺序的影响。

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

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