• 3

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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.

  • 228
Reply Report
    • 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?

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

def testFoo():
    '''Foo test case'''
    print 'this is foo test case'



testFoo has been disabled
  • 26
Reply Report

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 "")
    (search-forward "def")
    (if (looking-at "disable_")
    (zap-to-char 1 ?_)
      (insert "disable_"))))
  • 11
Reply Report
      • 1
    • 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... :)
      • 2
    • 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.
    • +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

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

  • 10
Reply Report
      • 1
    • 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.

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:


(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
Reply Report

Trending Tags