python - 如何多次运行相同的TestSuite unittest texttestrunner

标签 python python-3.x

我想对运行一次的产品的多个版本运行测试。以下是代码示例:

import unittest

suite = unittest.TestLoader().discover("./tests")
runner = unittest.TextTestRunner()

for build in [build1, build2]:
    get_the_build(build)
    runner.run(suite)

第一次迭代运行良好,但在第二次迭代开始时出现错误:

Traceback (most recent call last):
  File "D:/Path/to/my/folder/run_tests.py", line 9, in <module>
    runner.run(suite)
  File "C:\Program Files (x86)\Python36-32\lib\unittest\runner.py", line 176, in run
    test(result)
  File "C:\Program Files (x86)\Python36-32\lib\unittest\suite.py", line 84, in __call__
    return self.run(*args, **kwds)
  File "C:\Program Files (x86)\Python36-32\lib\unittest\suite.py", line 122, in run
    test(result)
TypeError: 'NoneType' object is not callable

发生了什么事? runner 调用的结果是什么?为什么会失败?有什么解决问题的想法吗?

最佳答案

好吧好吧。我花了我生命的最后一个小时在 GitHub 中查看 unittest 的代码,可以找到 here .我刚刚转到 suite.py ( here ) 的代码,这是您遇到的错误中的文件之一。这是 TestSuite.run 的实际代码:

def run(self, result, debug=False):
    topLevel = False
    if getattr(result, '_testRunEntered', False) is False:
        result._testRunEntered = topLevel = True

    for index, test in enumerate(self):
        if result.shouldStop:
            break

        if _isnotsuite(test):
            self._tearDownPreviousClass(test, result)
            self._handleModuleFixture(test, result)
            self._handleClassSetUp(test, result)
            result._previousTestClass = test.__class__

            if (getattr(test.__class__, '_classSetupFailed', False) or
                getattr(result, '_moduleSetUpFailed', False)):
                continue

        if not debug:
            test(result)
        else:
            test.debug()

        if self._cleanup:
            self._removeTestAtIndex(index)

    if topLevel:
        self._tearDownPreviousClass(None, result)
        self._handleModuleTearDown(result)
        result._testRunEntered = False
    return result

所以,基本上,这段代码所做的是迭代套件中的每个测试并调用它:

for index, test in enumerate(self):
    ...
    if not debug:
        test(result)  # This is the line throwing the error
    ...

如您所见,该循环遍历套件本身,因此它必须在某处 定义了一个__iter__ 方法。在类 TestSuite 中找不到它 5 分钟后,我意识到是父类有这样的方法。这是我在 BaseTestSuite 中找到的:

def __iter__(self):
    return iter(self._tests)

基本上,它只返回测试的迭代器。那一刻,这行代码,就是我无法跨过的一堵高墙。但我并没有放弃,而是回到了 TestSuite.run 定义,奇迹般地,我发现了下一行:

...
if self._cleanup:
    self._removeTestAtIndex(index)
...

这让我想知道:“是否删除了测试?让我调查一下”。然后我就豁然开朗了,因为在 _removeTestAtIndex 里面我发现了这一行:

self._tests[index] = None

故事结束。因此,在第一次运行所有测试后,它们只转换为 None:套件中的测试列表最终变成了 None 的列表([无,无,...,无])。

那么,您如何防止此类行为?只需关闭套件内的 _cleanup 标志即可。这应该有效:

回答

import unittest

suite = unittest.TestLoader().discover("./tests")
suite._cleanup = False  # Prevent such cleanup

runner = unittest.TextTestRunner()

for build in [build1, build2]:
    get_the_build(build)
    runner.run(suite)

抱歉说来话长,但除了向您展示如何解决您的问题外,我还想教您如何调试任何东西。

让我知道这是否对您有用。否则,告诉我哪里出了问题。

关于python - 如何多次运行相同的TestSuite unittest texttestrunner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54278700/

相关文章:

python - 如何使用服务帐户 oauth2 Python 客户端通过 Google 电子邮件设置 API 进行身份验证?

linux - Cd 到虚拟环境而不是采购它

python - ASCII 字符串作为 Python 3 中 numpy 字符串数组的 dtype

python - 我如何将字符串索引转换为整数索引

python - 如何获取 QTreeWidget 中被单击的项目?

python - MaxProductOfThree 如何提高性能

python - 是否可以创建受正则表达式约束的类型提示?

python - 如何创建多列索引数据框以及如何为每组值绘制图表

python - 有没有办法在决策树的每个叶子下获取样本?

python - 使用f-strings python格式化具有相同宽度的数字