简体   繁体   English

计算Python单元测试中的子测试

[英]Count subtests in Python unittests separately

Since version 3.4, Python supports a simple subtest syntax when writing unittests . 从3.4版开始,Python 在编写单元测试时支持简单的子测试语法 A simple example could look like this: 一个简单的例子可能如下所示:

import unittest

class NumbersTest(unittest.TestCase):

    def test_successful(self):
        """A test with subtests that will all succeed."""
        for i in range(0, 6):
            with self.subTest(i=i):
                self.assertEqual(i, i)

if __name__ == '__main__':
    unittest.main()

When running the tests, the output will be 运行测试时,输出将是

python3 test_foo.py --verbose
test_successful (__main__.NumbersTest)
A test with subtests that will all succeed. ... ok

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

However, in my real world use cases, the subtests will depend on a more complex iterable and check something which is very different for each subtest. 但是,在我的实际用例中,子测试将依赖于更复杂的迭代,并检查每个子测试的非常不同的东西。 Consequently, I would rather have each subtest counted and listed as a separated test case in the output ( Ran 6 tests in ... in this example) to get the full picture. 因此,我宁愿让每个子测试计数并在输出中列出一个单独的测试用例(在本例中为Ran 6 tests in ... )以获得完整的图像。

Is this somehow possible with the plain unittest module in Python? 这在Python中使用plain unittest模块是否可行? The nose test generator feature would output each test separately but I would like to stay compatible with the standard library if possible. 鼻子测试生成器功能将单独输出每个测试,但如果可能,我希望保持与标准库兼容。

You could subclass unittest.TestResult : 你可以unittest.TestResult

class NumbersTestResult(unittest.TestResult):
    def addSubTest(self, test, subtest, outcome):
        # handle failures calling base class
        super(NumbersTestResult, self).addSubTest(test, subtest, outcome)
        # add to total number of tests run
        self.testsRun += 1

Then in NumbersTest override the run function: 然后在NumbersTest覆盖run函数:

def run(self, test_result=None):
    return super(NumbersTest, self).run(NumbersTestResult())

Sorry I cannot test this in a fully working environment right now, but this should do the trick. 对不起,我现在无法在完全正常工作的环境中对此进行测试,但这应该可以解决问题。

Using python 3.5.2, themiurge's answer didn't work out-of-the-box for me but a little tweaking got it to do what I wanted. 使用python 3.5.2,他们的回答对我来说并不是开箱即用的,但是稍作调整就可以做到我想要的。

I had to specifically get the test runner to use this new class as follows: 我必须专门让测试运行器使用这个新类,如下所示:

if __name__ == '__main__': 
    unittest.main(testRunner=unittest.TextTestRunner(resultclass=NumbersTestResult))

However this didn't print the details of the test failures to the console as in the default case. 但是,这并没有像在默认情况下那样将测试失败的细节打印到控制台。 To restore this behaviour I had to change the class NumbersTestResult inherited from to unittest.TextTestResult . 要恢复这种行为,我不得不改变类NumbersTestResult从继承的unittest.TextTestResult

class NumbersTestResult(unittest.TextTestResult):
    def addSubTest(self, test, subtest, outcome):
        # handle failures calling base class
        super(NumbersTestResult, self).addSubTest(test, subtest, outcome)
        # add to total number of tests run
        self.testsRun += 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM