gpt4 book ai didi

python - 从 Nose2 插件跳过单元测试

转载 作者:行者123 更新时间:2023-11-28 17:40:47 30 4
gpt4 key购买 nike

我在实际跳过 Nose2 插件的单元测试时遇到了问题。我能够将测试标记为已跳过并在最终结果中查看原因,但测试仍在运行。只要插件处于事件状态,此示例代码基本上应该跳过任何测试。

from nose2.events import Plugin

class SkipAllTests(Plugin):
def startTest(self, event):
event.result.addSkip(event.test, 'skip it')
event.handled = True

如果我调用 event.test.skipTest('reason') 它实际上会像它应该的那样引发 SkipTest 异常,只是异常没有被捕获测试运行器,它只是在我的 startTest 钩子(Hook)方法中引发。有什么想法吗?

最佳答案

我认为您实际上无法使用 startTest Hook 停止测试的运行。 nose2 docs建议使用 matchPathgetTestCaseNames去做这个。下面是一个使用 matchPath 的工作示例:

from nose2.events import Plugin

class SkipAllTests(Plugin):
configSection = "skipper"
commandLineSwitch = (None, 'skipper', "Skip all tests")

def matchPath(self, event):
event.handled = True
return False

matchPath 文档实际上明确解释了如何使用它来停止测试运​​行:

Plugins can use this hook to prevent python modules from being loaded by the test loader or force them to be loaded by the test loader. Set event.handled to True and return False to cause the loader to skip the module.

使用此方法将阻止加载测试用例。如果您希望测试以跳过的形式实际显示在列表中,而不是根本不显示在测试列表中,您可以使用 StartTestEvent 进行一些黑客操作:

def dummy(*args, **kwargs):
pass

class SkipAllTests(Plugin):
configSection = "skipper"
commandLineSwitch = (None, 'skipper', "Skip all tests")
def startTest(self, event):
event.test._testFunc = dummy
event.result.addSkip(event.test, 'skip it')
event.handled = True

在这里,我们用一个什么都不做的虚拟函数替换了测试将要运行的实际函数。这样,当测试执行时,它不会执行任何操作,然后报告它已被跳过。

关于python - 从 Nose2 插件跳过单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24639671/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com