暂时禁用单个Python单元测试

coe*_*udo 111 python python-unittest

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

yon*_*oni 202

可以使用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.')
Run Code Online (Sandbox Code Playgroud)

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

  • 在Python 3中,Akif在`@ unittest.SkipTest`下面的答案有效,而不是`@ unittest.skip` (7认同)
  • 我正在使用Python 3.6.1和`@ unittest.skip`工作也很好. (5认同)
  • @Pete,在Python 3.4.0中,`@ unittest.skip`不起作用. (2认同)

小智 26

您可以使用装饰器来禁用可以包装该函数的测试,并阻止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()
Run Code Online (Sandbox Code Playgroud)

输出:

testFoo has been disabled
Run Code Online (Sandbox Code Playgroud)


Nou*_*him 11

最新版本(2.7 - 未发布)支持测试跳过/禁用.您可以获得此模块并在现有的Python安装上使用它.它可能会奏效.

在此之前,我曾经改名,我想跳过来测试xtest_testnametest_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_"))))
Run Code Online (Sandbox Code Playgroud)


Aki*_*kif 9

只需将@unittest.SkipTest装饰器放在测试之上即可.


Mat*_*ttK 5

我只是使用下划线重命名测试用例方法:test_myfunc变为_test_myfunc.