Disable individual Python unit tests temporarily

How can individual unit tests be temporarily disabled when using the unittest module in Python?

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.

How can individual unit tests be temporarily disabled when using the unittest module in Python? 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.

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

220 Disable individual Python unit tests temporarily Apr 22 '13 70 Integration testing Hive jobs May 23 '13 35 Find out the number of days of a month in R Sep 26 '11

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_"))))

I am aware of running multiple tests defined as individual tests inside a unittest Class but I am trying to see how I can run the unit tests that exist as individual script files as a test suite. My scripts are in Python. Thanks!

Simply placing @unittest.SkipTest decorator above the test is enough.

Define a nose test method 'test_circlearea_with_min_radius', which creates a circle 'c2' with radius 0, and check if its computed area is 0. Define a nose test method 'test_circlearea_with_max_radius', which creates a circle 'c3' with radius 1000, and check if its computed area is 3141592.65. I have written the below nose test methods.

I just rename a test case method with an underscore: test_myfunc becomes _test_myfunc.

You may test, that the path is absolute, the path leads to a directory and you have a R/W access to the directory (including directory deletion). However, your function just delegates actual work to the mkdtemp - are you sure you don't have more suitable function for testing? – awesoon Feb 19 '16 at 7:32

The Python unit testing framework, sometimes referred to as “PyUnit,” is a Python language version of JUnit, by Kent Beck and Erich Gamma. JUnit is, in turn, a Java version of Kent’s Smalltalk testing framework. Each is the de facto standard unit testing framework for its respective language.

python.testing.autoTestDiscoverOnSaveEnabled is set to true by default, meaning test discovery is performed automatically whenever you save a test file. To disable this feature, set the value to false. Test discovery applies the discovery patterns for the current framework (which can be customized using the Test configuration settings).

Created on 2018-09-06 13:05 by Naitree Zhu, last changed 2019-09-09 15:01 by python-dev.This issue is now closed.

Comments
  • In Python 3, Akif's answer below @unittest.SkipTest works and not @unittest.skip
  • I'm using Python 3.6.1 and @unittest.skip works also fine.
  • @Pete, in Python 3.4.0, @unittest.skip does not work.
  • How to prevent the child classes from getting skipped?
  • 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?
  • +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
  • 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... :)
  • I'm a Emacs user too, did you made a macro?
  • Nope. I just let them fail but it's not that hard. I've updated the answer with a quickie though.
  • This 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.
  • 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.
  • unittest.skip (without reason) gives me error in Python 2 but not in Python 3.