How do you skip a unit test in Django?
Asked Answered
A

2

130

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.

Apc answered 8/7, 2013 at 5:8 Comment(0)
G
218

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.

Gissing answered 8/7, 2013 at 5:25 Comment(4)
Ahh, I didn't know that you could trick the interpreter with that True argument. Thanks!Apc
Could you elaborate on possible reasons for not being able to use @skip?Mallen
You can even skip TestCase classes.Aplanospore
That's great! I did it that way: Put a "SKIP_STRESS_TESTS = True" variable in settings.py and then a "@skipIf(SKIP_STRESS_TESTS, "Will not test this class this time")" on top of the heavy load classes, for me to test once in a while. Thanks.Transatlantic
B
91

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
Brandt answered 21/9, 2016 at 16:27 Comment(2)
Is there an opposite to --exclude-tag, e.g. --include-tag but this command is non-existent.Saturnian
@Saturnian docs.djangoproject.com/en/3.0/topics/testing/tools/…Brandt

© 2022 - 2024 — McMap. All rights reserved.