Django 装饰器在测试中跳过固定装置

标签 django django-testing django-fixtures

Django 中是否有一个装饰器可以允许在不应用固定装置的情况下运行测试函数?
类似于:

from django.test import TestCase

class TestSomething(TestCase):
    fixtures = ['test_fixture.json']

    def test_with_fixture(self):
        # test something with fixtures 

    @do_not_use_fixtures
    def test_without_fixtures(self): 
        # test something without fixtures

最佳答案

Django 的 TestCase 为类 for performance reasons 加载一次固定装置。 。因此,没有固定装置就不可能运行测试方法。

使用 TransactionTestCase 可能是可行的,但您必须深入了解 Django 内部结构才能做到这一点,所以我不推荐它。

关于Django 装饰器在测试中跳过固定装置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49798524/

相关文章:

Django Rest Framework 测试失败 : Expected view to be called with a URL keyword argument

DjangoModelFactory JsonField 返回 Unicode 数据而不是字典数据

django - 在 Django 中获取模型的字段

css - Django 国际化 : How to translate css content?

python - 在 Django 中测试特定模型

django - 如何从所有应用程序加载 Django 装置?

python - 不使用固定装置的 Django 1.10 种子数据库

python - django 在 syncdb 时不加载初始固定装置

Django model.save() 不使用 loaddata

python - Django 对 3 个相关表的查询优化