Temporarily disable individual Python test tests

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

+80
python python-unittest
Jan 14 '10 at 18:26
source share
8 answers

Individual test methods or classes can be disabled using 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 .

+155
Apr 22 '13 at 2:30
source share

You can use decorators to disable a test that can wrap a function and prevent googletest or python unit test from running to run a test file.

 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 
+24
Jan 14 '10 at 18:59
source share

The latest version (2.7 - unreleased) supports skipping / disabling the test this way . You can simply get this module and use it on an existing Python installation. This will probably work.

Before that, I used to rename the tests that I wanted to skip to xtest_testname from test_testname .




Here for quick removal of the script. My elisp is a little rusty, so I apologize in advance for any problems that it has. Unverified.

  (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_")))) 
+11
Jan 14 '10 at 18:30
source share

Just placing the @unittest.SkipTest decorator over the test is enough.

+8
Feb 24 '17 at 3:18
source share

I just rename the test case method with an underline: test_myfunc becomes _test_myfunc.

+6
Jan 14 '10 at 19:05
source share

docs for 2.1 does not indicate a method of ignoring or skipping.

Usually, I block the comment if necessary.

+3
Jan 14 '10 at 18:30
source share

By focusing on the “temporarily disabled” part of the question, the best answer is to some extent dependent on the use case. The use case that brought me here is that I am doing a test development of the function. In this process, I write tests sequentially and often use breakpoints in functions for debugging. If I just run all the tests every time I run the tester, I stop at breakpoints for tests that are already running. Adding a “skip” or changing the name of the test or something like that is not what I want, because when I finish writing the function, I want all the tests to run. If I used to skip, I would have to go back and cancel.

For my use case, the solution is a test run, not a test code. I am using Pytest . With pytest, you can easily determine one test from the command line:

pytest PYTHON_FILENAME.TEST_CLASS.TEST_NAME

(replace the uppercase letters with your values).

I understand that this question was for python-unitest. I have not used this for a long time. I would not be surprised if he had something like pytest. If not, you can easily switch to pytest. You do not need to change your code. Just install it and change the command to run the test.

I also use PyCharm Pro. There is a small icon on the page with my test code next to the definition for each test. I can click on this icon and run this test separately.

0
Feb 06 '19 at 19:11
source share

if it is pytest, then you can skip it like this: @ pytest.mark.skip ("skip reason")

0
Apr 12 '19 at 10:00
source share



All Articles