The Python Oracle

Disable individual Python unit tests temporarily

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Horror Game Menu Looping

--

Chapters
00:00 Question
00:21 Accepted answer (Score 329)
00:47 Answer 2 (Score 27)
01:15 Answer 3 (Score 18)
01:30 Answer 4 (Score 10)
02:21 Thank you

--

Full question
https://stackoverflow.com/questions/2066...

Accepted answer links:
[unittest.skip]: https://docs.python.org/2/library/unitte...
[Skipping tests and expected failures]: https://docs.python.org/2/library/unitte...

Answer 4 links:
[The latest version (2.7 - unreleased) supports test skipping/disabling like so]: http://docs.python.org/dev/library/unitt...
[Elisp]: https://en.wikipedia.org/wiki/Emacs_Lisp

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pythonunittest

#avk47



ACCEPTED ANSWER

Score 354


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.




ANSWER 2

Score 27


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



ANSWER 3

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



ANSWER 4

Score 6


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