Skip to main content
 首页 » 编程设计

未引发 Python unittest AssertionError

2024年12月31日8xiaohuochai

我正在尝试为我的 Python 包编写单元测试,我发现,当我运行测试时,AssertionErrors 没有被引发,而它们应该被引发。这是一个 MWE :

在 exampleModule.py 我有:

#! /usr/bin/env python 
import unittest 
 
class UnitTest(unittest.TestCase): 
 
    def runTest(self): 
        print("Starting test...") 
        a = 4 
        b = 5 
        self.assertEqual(a,b) 
        print("TEST COMPLETE") 
        return 

在 testError.py 我有:

#! /usr/bin/env python 
import unittest 
 
class AllTests(unittest.TestCase): 
 
    def testExample(self): 
        from exampleModule import UnitTest 
        UT = UnitTest() 
        UT.run() 
        return 
 
if __name__ == '__main__': 
    unittest.main() 

当我运行 testError.py 时,我希望在 exampleModule.py 中看到 UnitTest 报告的 AssertionError,但是,我只看到以下内容:

> ./testError.py 
Starting test... 
. 
---------------------------------------------------------------------- 
Ran 1 test in 0.001s 
 
OK 

为什么没有引发 AssertionError?如果我将 UnitTest() 类放在 testError.py 中(即,将所有内容都放在同一个文件中),则会引发 AssertionError。那么,为什么当 UnitTest 存储在不同的文件中时,不会引发错误?

请您参考如下方法:

TestCase.run() 创建或更新一个 TestResult 对象,假设您打算对这些结果做一些有趣的事情。

但是你马上就把它们扔掉了:

    UT.run() 

任何失败或错误(包括引发的异常)都将包含在该结果对象中。

例如:

def testExample(self): 
    from exampleModule import UnitTest 
    UT = UnitTest() 
    result = UT.run() 
    print('Errors:  {!r}'.format(result.errors)) 
    print('Failures:  {!r}'.format(result.failures)) 
    return 

这打印:

Failures: [(<exampleModule.UnitTest testMethod=runTest>, 'Traceback (most recent call last):\n File "/home/kjc/tmp/exampleModule.py", line 11, in runTest\n self.assertEqual(a, b)\nAssertionError: 4 != 5\n')]

它捕获各种异常,例如我在断言之前添加了 sys.exit() 的示例:

Errors: [(<exampleModule.UnitTest testMethod=runTest>, 'Traceback (most recent call last):\n File "/home/kjc/tmp/exampleModule.py", line 11, in runTest\n import sys; sys.exit()\nSystemExit\n')]

郑重声明,您的示例生成的通过测试的是 testExample 本身。 您可以通过在 testExample 中调用 self.fail() 来验证这一点。 (正如一位评论员所说,从单元测试调用单元测试是一件非常奇怪的事情。)

一个解决方案

您似乎想正常运行测试,但要从多个文件运行。 如果是这样,你可以制作一堆典型的 unittest.main() -风格 测试模块,然后手动加载它们。

你的测试一切脚本看起来像这样:

#!/usr/bin/env python 
import unittest 
 
 
# Edit this to include new modules or TestCases to run. 
_NAMES = ['testmoda', 'testmodb']  # testothermodule.SomeTestCase, etc... 
 
 
def _main(): 
    suite = unittest.defaultTestLoader.loadTestsFromNames(_NAMES) 
    unittest.TextTestRunner().run(suite) 
    return  # WARNING: This always returns a successful exit value. 
 
 
if __name__ == '__main__': 
    _main() 

您的测试模块将是通常的一个或多个 TestCase,有条件地调用 unittest.main(),因此每个模块都可以独立运行:

#!/usr/bin/env python 
import unittest 
 
 
class TestModA(unittest.TestCase): 
 
    def testThatPasses(self): 
        self.assertEqual(0, -0) 
        return 
 
    def testThatFails(self): 
        self.assertEqual(0, float('inf')) 
        return 
 
    def testThatBlowsUp(self): 
        raise RuntimeError('OMG!') 
        return 
 
 
if '__main__' == __name__: 
    unittest.main() 

如果这对您来说还不够繁琐,您还可以阅读 discover() method 或模块级 load_tests protocol .