views:

31

answers:

4

How to disable individual tests temporarily using unittest module in python?

Like googletest does.

Thanks.

+1  A: 

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

Usually though, I block comment when needed.

Finglas
Noufal makes a good point, mangaling the name works too.
Finglas
+2  A: 

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_"))))
Noufal Ibrahim
+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
Renaming for now. thanks
coelhudo
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
I'm a Emacs user too, did you made a macro?
coelhudo
Nope. I just let them fail but it's not that hard. I've updated the answer with a quickie though.
Noufal Ibrahim
+3  A: 

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
GTM
+1 for the extra output and the custom decorator.
Noufal Ibrahim
A: 

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

MattK