暂时禁用单个 Python 单元测试
在 Python 中使用 unittest
模块时,如何暂时禁用单个单元测试?
How can individual unit tests be temporarily disabled when using the unittest
module in Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
可以使用
unittest.skip 禁用单个测试方法或类
装饰器。有关其他选项,请参阅跳过测试和预期的失败。
Individual test methods or classes can both be disabled using the
unittest.skip
decorator.For other options, see the docs for Skipping tests and expected failures.
您可以使用装饰器禁用可以包装函数的测试并阻止 googletest 或 Python 单元测试运行测试用例。
输出:
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.
Output:
只需将
@unittest.SkipTest
装饰器放置在测试上方就足够了。Simply placing
@unittest.SkipTest
decorator above the test is enough.最新版本(2.7 - 未发布)支持测试跳过/禁用,如下< /a>.您可以获取此模块并在现有的 Python 安装中使用它。它可能会起作用。
在此之前,我曾经将想要跳过的测试从
test_testname
重命名为xtest_testname
。这是一个快速的 Elisp 脚本来执行此操作。我的 Elisp 有点生锈了,所以对于它出现的任何问题,我提前表示歉意。未经测试。
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
fromtest_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.
我只是用下划线重命名一个测试用例方法:test_myfunc 变为 _test_myfunc。
I just rename a test case method with an underscore: test_myfunc becomes _test_myfunc.
2.1 的 docs 没有指定忽略或跳过方法。
但通常情况下,我会在需要时阻止评论。
The docs for 2.1 don't specify an ignore or skip method.
Usually though, I block comment when needed.
关注问题的“暂时禁用”部分,最佳答案在某种程度上取决于用例。让我来到这里的用例是我正在对一个函数进行测试驱动开发。在这个过程中,我陆续编写测试,并经常在函数中使用断点进行调试。如果我每次运行测试运行程序时都运行所有测试,那么我最终会在已经工作的测试的断点处停止。添加“skip”或修改测试名称或类似的内容不是我想要的,因为当我编写完函数后,我希望运行所有测试。如果我使用“跳过”,我将不得不返回并“取消跳过”。
对于我的用例,解决方案在于测试运行程序,而不是测试代码。我使用 pytest。使用 pytest,可以轻松地从命令行指定单个测试:
(用您的值替换大写字母)。
我知道这个问题是针对 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:
(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.
另一种选择是引发异常
unittest.SkipTest("Resason forskipping")
如果您想跳过整个文件而不标记要跳过的单个测试/类,则可能是相关的。只需放在
文件顶部即可。 (或者必须跳过的内部测试)
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
On top of the file. (Or inside test that has to be skipped)