How can individual unit tests be temporarily disabled when using the unittest
module in Python?
-
1Under the hood the custom exception `unittest.case.SkipTest` is raised to skip a test – Mandera Sep 07 '22 at 17:26
8 Answers
Individual test methods or classes can both be disabled using the unittest.skip
decorator.
@unittest.skip("reason for skipping")
def test_foo():
print('This is foo test case.')
@unittest.skip # no reason needed
def test_bar():
print('This is bar test case.')
For other options, see the docs for Skipping tests and expected failures.
-
12In Python 3, Akif's answer below `@unittest.SkipTest` works and not `@unittest.skip` – lifebalance May 13 '17 at 18:48
-
13
-
2
-
1
-
The annotation would only be applied to a single test method. Do you intend to have a parent class with test methods, and only some child classes have the method disabled? I imagine that's possible, though you might need to design the parent/child classes appropriately. Can you spell out the scenario you're trying to solve with a toy example? – yoni Mar 14 '18 at 04:33
-
To those saying that bare `@unittest.skip` doesn't work: it is not currently a supported feature to use `skip` without calling it and providing a reason, doing so will currently cause the test to be marked as a success without being run. This is currently being discussed in https://bugs.python.org/issue34596. – Zachary Ware Sep 08 '18 at 17:30
-
@yoni Hi, could you please tell how can I prevent `skip()`, `skipIf()` method from escaping the `reason` string. The problem is the output of the `skip()` or `skipIf()` decorator is joined at `\n` which I don't want. Please enlighten me! – Ayush Dec 13 '21 at 05:48
You can use decorators to disable the test that can wrap the function and prevent the googletest or Python unit test to run the testcase.
def disabled(f):
def _decorator():
print f.__name__ + ' has been disabled'
return _decorator
@disabled
def testFoo():
'''Foo test case'''
print 'this is foo test case'
testFoo()
Output:
testFoo has been disabled

- 30,738
- 21
- 105
- 131

- 634
- 4
- 10
-
What is "googletest"? GoogleTest is a ***C++*** test framework. Incl. [the Stack Overflow tag](https://stackoverflow.com/tags/googletest/info). – Peter Mortensen May 10 '21 at 20:27
Simply placing @unittest.SkipTest
decorator above the test is enough.

- 6,018
- 3
- 41
- 44
-
I don't know who did it. But, I was reading the documentation and it says that SkipTest is an exception. Anyhow, it is different from unittest.skip in the sense that skip never executes the test (making it disabled), while SkipTest purpose is to be raised if something not expected occurs while the test is executing. Am I right? Interesting tough. – coelhudo Feb 24 '17 at 08:20
-
`unittest.skip` (without reason) gives me error in Python 2 but not in Python 3. – Akif Feb 27 '17 at 09:35
-
There is also a difference in the output. @unittest.SkipTest is silent, while @unittest.skip('Reason') will print a 's' instead of a '.', so skipped tests are visible and can't be forgotten – Benny Dec 22 '22 at 15:49
The latest version (2.7 - unreleased) supports test skipping/disabling like so. You could just get this module and use it on your existing Python install. It will probably work.
Before this, I used to rename the tests I wanted skipped to xtest_testname
from test_testname
.
Here's a quick Elisp script to do this. My Elisp is a little rusty, so I apologise in advance for any problems it has. Untested.
(defun disable_enable_test ()
(interactive "")
(save-excursion
(beginning-of-line)
(search-forward "def")
(forward-char)
(if (looking-at "disable_")
(zap-to-char 1 ?_)
(insert "disable_"))))

- 30,738
- 21
- 105
- 131

- 71,383
- 13
- 135
- 169
-
+1, but in the whole project that I'm working everyone is using python v2.6.2, and I don't think this will change :/, but it's a solution, thanks – coelhudo Jan 14 '10 at 18:36
-
You could tweak your editor a little to give you a macro to enable/disable a testcase but I speak as an Emacs user so... :) – Noufal Ibrahim Jan 14 '10 at 18:42
-
-
Nope. I just let them fail but it's not that hard. I've updated the answer with a quickie though. – Noufal Ibrahim Jan 14 '10 at 19:01
-
1This accepted answer is not an answer, just a link. The real, current answer is not the top voted one either. Granted, it is 4 years old. The gratuitous elisp merely obfuscates. – jwd630 Nov 03 '14 at 22:09
I just rename a test case method with an underscore: test_myfunc becomes _test_myfunc.

- 1,431
- 13
- 14
-
Yes, as a quick hack this is possible - but if you want the test statistics to be maintained you should use the @unittest.skip attribute. – Matt Jul 13 '23 at 06:45
Focusing on the "temporarily disabled" part of the question, the best answer somewhat depends on the use case. The use case that brought me here is I am doing test driven development on a function. In this process, I successively write tests and often use break points in the function for debugging. If I just run all the tests every time I run the test runner, I end up stopping at break points for tests that already work. Adding "skip" or munging the test name or something like that is not what I want because when I am done writing the function, I want all tests to run. If I used "skip" I would have to go back and "unskip".
For my use case, the solution lies in the test runner, not in the test code. I use pytest. With pytest, it is easy to specify a single test from the command line:
pytest PYTHON_FILENAME.TEST_CLASS.TEST_NAME
(replace the caps with your values).
I understand the that question was for python-unitest. I have not used that in a long time. I would not be surprised if it had something similar to pytest. If not, you can easily switch to pytest. You do not need to modify your code. Just install it and change your test runner command.
Also, I use PyCharm Pro. On the page that shows my test code, there is a small icon next to the def for each test. I can click that icon and run that test individually.

- 1,089
- 1
- 13
- 18
-
It is the right idea, but the use case is probably ***excluding*** one (or a few) test case, not running only one. – Peter Mortensen May 10 '21 at 20:31
-
[The `-k` option can be used from the command line](https://stackoverflow.com/questions/38442897/how-do-i-disable-a-test-using-pytest/61869801#61869801) to suppress a single (or presumably a few) test. – Peter Mortensen May 10 '21 at 20:58
-
-
*unittest* does actually [have the `-k` option](https://docs.python.org/3/library/unittest.html#command-line-options) (from version 3.7). – Peter Mortensen May 10 '21 at 22:06
-
Another option is to raise exception unittest.SkipTest("Resason for skipping")
Can be relevant in case you want to skip the whole file without marking individual tests/classes to be skipped. Just place
import unittest
raise unittest.SkipTest()
On top of the file. (Or inside test that has to be skipped)

- 1,754
- 6
- 22