Disable individual Python unit tests temporarily

PythonPython Unittest

Python Problem Overview


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

Python Solutions


Solution 1 - 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.

Solution 2 - Python

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

Solution 3 - Python

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

Solution 4 - 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.

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

Solution 5 - Python

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

Solution 6 - Python

The docs for 2.1 don't specify an ignore or skip method.

Usually though, I block comment when needed.

Solution 7 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncoelhudoView Question on Stackoverflow
Solution 1 - PythonyoniView Answer on Stackoverflow
Solution 2 - PythonGTMView Answer on Stackoverflow
Solution 3 - PythonAkifView Answer on Stackoverflow
Solution 4 - PythonNoufal IbrahimView Answer on Stackoverflow
Solution 5 - PythonMattKView Answer on Stackoverflow
Solution 6 - PythonFinglasView Answer on Stackoverflow
Solution 7 - PythonChuckView Answer on Stackoverflow