代码之家  ›  专栏  ›  技术社区  ›  ncopiy

Django decorator在测试中跳过夹具

  •  2
  • ncopiy  · 技术社区  · 6 年前

    Django中是否有允许在不应用fixture的情况下运行测试函数的decorator?
    类似于:

    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
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Alasdair    6 年前

    Django的 TestCase 为类加载一次装置 for performance reasons 因此,不可能在没有夹具的情况下运行测试方法。

    这可能与 TransactionTestCase ,但要做到这一点,您必须深入Django内部,因此我不推荐使用它。