代码之家  ›  专栏  ›  技术社区  ›  coelhudo

暂时禁用单个Python单元测试

  •  74
  • coelhudo  · 技术社区  · 16 年前

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

    7 回复  |  直到 9 年前
        1
  •  268
  •   Asclepius    9 年前

    可以使用禁用单个测试方法或类 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.')
    

    有关其他选项,请参阅文档以了解更多信息 Skipping tests and expected failures .

        2
  •  27
  •   Peter Mortensen Pieter Jan Bonestroo    5 年前

    您可以使用decorator禁用可以包装函数的测试,并阻止googletest或Python单元测试运行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()
    

    testFoo has been disabled
    
        3
  •  11
  •   Akif    9 年前

    简单放置 @unittest.SkipTest 以上的测试就足够了。

        4
  •  11
  •   Peter Mortensen Pieter Jan Bonestroo    5 年前

    The latest version (2.7 - unreleased) supports test skipping/disabling like so . 您只需获取此模块并在现有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_"))))
    
        5
  •  5
  •   MattK    16 年前

        6
  •  2
  •   Finglas    16 年前

    这个 docs 对于2.1,不要指定忽略或跳过方法。

    但通常情况下,我会在需要时阻止评论。

        7
  •  1
  •   Chuck    7 年前

    对于我的用例,解决方案在于测试运行程序,而不是测试代码。我用 pytest

    pytest PYTHON_FILENAME.TEST_CLASS.TEST_NAME

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