【问题标题】:How do you skip a unit test in Django?如何跳过 Django 中的单元测试?
【发布时间】:2013-07-05 08:46:41
【问题描述】:

如何在 Django 中强制跳过单元测试?

我只找到了@skipif 和@skipunless,但我现在只想跳过一个测试以进行调试,同时理顺一些事情。

【问题讨论】:

    标签: django unit-testing skip django-unittest


    【解决方案1】:

    Python 的 unittest 模块有一些装饰器:

    有一个普通的老@skip

    from unittest import skip
    
    @skip("Don't want to test")
    def test_something():
        ...
    

    如果由于某种原因您不能使用@skip@skipIf 应该可以工作。只是欺骗它总是跳过参数True

    @skipIf(True, "I don't want to run this test yet")
    def test_something():
        ...
    

    unittest docs

    Docs on skipping tests

    如果您只想不运行某些测试文件,最好的方法可能是使用fab 或其他工具并运行特定的测试。

    【讨论】:

    • 啊,我不知道你可以用 True 参数欺骗解释器。谢谢!
    • 能否详细说明无法使用@skip的可能原因?
    • 你甚至可以跳过 TestCase 类。
    【解决方案2】:

    Django 1.10 allows use of tags 用于单元测试。然后您可以使用--exclude-tag=tag_name 标志来排除某些标签:

    from django.test import tag
    
    class SampleTestCase(TestCase):
    
        @tag('fast')
        def test_fast(self):
            ...
    
        @tag('slow')
        def test_slow(self):
            ...
    
        @tag('slow', 'core')
        def test_slow_but_core(self):
            ...
    

    在上面的示例中,要排除带有“slow”标签的测试,您将运行:

    $ ./manage.py test --exclude-tag=slow
    

    【讨论】:

    猜你喜欢
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多