暂时禁用单个Python单元测试

问题:暂时禁用单个Python单元测试

unittest在Python中使用模块时,如何暂时禁用各个单元测试?

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


回答 0

单个测试方法或类都可以使用unittest.skip装饰器禁用。

@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.')

有关其他选项,请参阅文档“ 跳过测试和预期的失败”

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.


回答 1

您可以使用装饰器禁用可以包装功能的测试,并阻止googletest或python单元测试运行测试用例。

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()

输出:

testFoo has been disabled

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

回答 2

最新版本(2.7版)支持测试跳过/禁用,就像这样。您可以仅获取此模块并将其用于现有的Python安装中。它可能会工作。

在此之前,我曾经重命名过xtest_testname要从中跳过的测试test_testname


这是一个快速的elisp脚本来执行此操作。我的elisp有点生锈,因此对于出现的任何问题我都表示歉意。未经测试。

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

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

回答 3

只需将@unittest.SkipTest装饰器放在测试之上就足够了。

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


回答 4

我只是用下划线将测试用例方法重命名:test_myfunc变成_test_myfunc。

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


回答 5

2.1 的文档未指定忽略或跳过方法。

不过通常,我会在需要时阻止评论。

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

Usually though, I block comment when needed.


回答 6

专注于问题的“暂时禁用”部分,最佳答案在某种程度上取决于用例。这里带给我的用例是我正在对功能进行测试驱动的开发。在此过程中,我连续编写测试,并经常在函数中使用断点进行调试。如果我每次运行测试运行程序时都运行所有测试,那么最终我会在已经生效的测试的断点处停止。我不需要添加“跳过”或修改测试名称或类似名称,因为当我编写完函数后,我希望所有测试都可以运行。如果使用“跳过”,则必须返回并“跳过”。

对于我的用例,解决方案在于测试运行器,而不是测试代码。我用pytest。使用pytest,可以从命令行轻松指定一个测试:

pytest PYTHON_FILENAME.TEST_CLASS.TEST_NAME

(将大写替换为您的值)。

我了解该问题是针对python-unitest的。我已经很久没有使用它了。如果它与pytest类似,我不会感到惊讶。如果没有,您可以轻松切换到pytest。您无需修改​​代码。只需安装它并更改您的测试运行器命令。

另外,我使用PyCharm Pro。在显示我的测试代码的页面上,每个测试的def旁边都有一个小图标。我可以单击该图标并单独运行该测试。

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.