How do you skip a unit test in Django?

旧城冷巷雨未停 提交于 2019-11-28 21:49:48

问题


How do forcibly skip a unit test in Django?

@skipif and @skipunless is all I found, but I just want to skip a test right now for debugging purposes while I get a few things straightened out.


回答1:


Python's unittest module has a few decorators:

There is plain old @skip:

from unittest import skip

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

If you can't use @skip for some reason, @skipIf should work. Just trick it to always skip with the argument True:

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

unittest docs

Docs on skipping tests

If you are looking to simply not run certain test files, the best way is probably to use fab or other tool and run particular tests.




回答2:


Django 1.10 allows use of tags for unit tests. You can then use the --exclude-tag=tag_name flag to exclude certain tags:

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):
        ...

In the above example, to exclude your tests with the "slow" tag you would run:

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


来源:https://stackoverflow.com/questions/17519528/how-do-you-skip-a-unit-test-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!