首页 > 解决方案 > Python unittest:使 AssertionError 成为错误而不是失败

问题描述

蟒蛇 2.7。单元测试文档说:

为了使迁移现有测试套件更容易,unittest 支持测试引发 AssertionError 以指示测试失败。但是,建议您改用显式 TestCase.fail*() 和 TestCase.assert*() 方法,因为未来版本的 unittest 可能会以不同的方式处理 AssertionError。

我在测试代码中使用了相当多的assert语句,但这些断言的失败实际上应该是测试错误(即“代码没有在这些输入下正确运行”)而不是失败(即“代码给出了不正确的输出”)。

我可以看到以下可能的解决方案:

  1. 重写测试代码以抛出更好类型的异常
  2. 将测试方法中的所有内容都包装在块中,除了测试断言本身(self.assertSomething(...)try...except AssertionError: raise SomeOtherException
  3. 更改 unittest 的行为,以便它考虑这些错误而不是失败。

选项 1 需要相当长的时间,选项 2 感觉很老套;选项 3 对我来说是最好的,但它可用吗?(以防万一:不,我不能切换到 Python 3。)我在网上看不到任何东西,但很难使用特定的关键字。

MWE:

import unittest


def add_one_to_int(a):
    assert isinstance(a, int)
    return a + 1


class TestAddOne(unittest.TestCase):
    def test_one_plus_one_is_three(self):
        # This tests fails with
        #   AssertionError: 2 != 3
        # which is fine
        self.assertEqual(add_one_to_int(1), 3)  

    def test_add_one_to_str(self):
        # This tests fails with
        #   AssertionError
        # when I would rather have it an error
        add_one_to_int('some string')

if __name__ == '__main__':
    unittest.main(verbosity=2)  # 2 failures instead of 1 failure, 1 error

标签: pythonpython-unittest

解决方案


我认为选项 3 可以通过类属性“failureException”来实现,正如 Python 2.7 的 unittest 文档中所定义的那样:

failureException:此类属性给出了测试方法引发的异常。如果一个测试框架需要使用一个专门的异常,可能携带额外的信息,它必须子类这个异常以便与框架“公平竞争”。该属性的初始值为 AssertionError。

例如:

import unittest

class MyException(Exception): 
    pass

class MyUnitTest(unittest.TestCase):
    failureException = MyException

def add_one_to_int(a):
    assert isinstance(a, int)
    return a + 1


class TestAddOne(MyUnitTest):  # <--------- See above
    def test_one_plus_one_is_three(self):
        # This tests fails with
        #   AssertionError: 2 != 3
        # which is fine
        self.assertEqual(add_one_to_int(1), 3)  

    def test_add_one_to_str(self):
        # This tests fails with
        #   AssertionError
        # when I would rather have it an error
        add_one_to_int('some string')

if __name__ == '__main__':
    unittest.main(verbosity=2)  # ------> FAILED (failures=1, errors=1)

推荐阅读