暂时禁用单个 Python 单元测试

发布于 2024-08-17 20:35:38 字数 59 浏览 5 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

智商已欠费 2024-08-24 20:35:38

可以使用 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.

许仙没带伞 2024-08-24 20:35:38

您可以使用装饰器禁用可以包装函数的测试并阻止 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
ゝ杯具 2024-08-24 20:35:38

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

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

聚集的泪 2024-08-24 20:35:38

最新版本(2.7 - 未发布)支持测试跳过/禁用,如下< /a>.您可以获取此模块并在现有的 Python 安装中使用它。它可能会起作用。

在此之前,我曾经将想要跳过的测试从 test_testname 重命名为 xtest_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_"))))
天暗了我发光 2024-08-24 20:35:38

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

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

萌辣 2024-08-24 20:35:38

2.1 的 docs 没有指定忽略或跳过方法。

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

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

Usually though, I block comment when needed.

稚然 2024-08-24 20:35:38

关注问题的“暂时禁用”部分,最佳答案在某种程度上取决于用例。让我来到这里的用例是我正在对一个函数进行测试驱动开发。在这个过程中,我陆续编写测试,并经常在函数中使用断点进行调试。如果我每次运行测试运行程序时都运行所有测试,那么我最终会在已经工作的测试的断点处停止。添加“skip”或修改测试名称或类似的内容不是我想要的,因为当我编写完函数后,我希望运行所有测试。如果我使用“跳过”,我将不得不返回并“取消跳过”。

对于我的用例,解决方案在于测试运行程序,而不是测试代码。我使用 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.

撩发小公举 2024-08-24 20:35:38

另一种选择是引发异常 unittest.SkipTest("Resason forskipping")

如果您想跳过整个文件而不标记要跳过的单个测试/类,则可能是相关的。只需放在

import unittest

raise unittest.SkipTest()

文件顶部即可。 (或者必须跳过的内部测试)

Another option is to raise exception unittest.SkipTest("Resason for skipping")

Can be relevant in case you want to skip the whole file without marking individual tests/classes to be skipped. Just place

import unittest

raise unittest.SkipTest()

On top of the file. (Or inside test that has to be skipped)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文