单元测试中的自定义异常

时间:2018-03-19 17:38:15

标签: python python-3.x unit-testing python-unittest assertraises

我已在errors.py

中创建了自定义异常
mapper = {
    'E101':
    'There is no data at all for these constraints',
    'E102':
    'There is no data for these constraints in this market, try changing market',
    'E103':
    'There is no data for these constraints during these dates, try changing dates',
}


class DataException(Exception):
    def __init__(self, code):
        super().__init__()
        self.msg = mapper[code]

    def __str__(self):
        return self.msg

如果DataException数据框中没有足够的数据,代码中其他位置的另一个函数会引发pandas的不同实例。我想使用unittest来确保它返回相应的异常及其相应的消息。

使用一个简单的例子,为什么这不起作用:

from .. import DataException
def foobar():
    raise DataException('E101')

import unittest
with unittest.TestCase.assertRaises(DataException):
    foobar()

正如此处所示:Python assertRaises on user-defined exceptions

我收到此错误:

TypeError: assertRaises() missing 1 required positional argument: 'expected_exception'

或者:

def foobar():
    raise DataException('E101')

import unittest
unittest.TestCase.assertRaises(DataException, foobar)

结果:

TypeError: assertRaises() arg 1 must be an exception type or tuple of exception types

为什么不将DataException识别为Exception?为什么链接的stackoverflow问题回答工作而不向assertRaises提供第二个参数?

1 个答案:

答案 0 :(得分:5)

您正在尝试使用TestCase类的方法而不创建实例;这些方法并非旨在以这种方式使用。

unittest.TestCase.assertRaises是一个未绑定的方法。您可以在定义的TestCase类的测试方法中使用它:

class DemoTestCase(unittest.TestCase):
    def test_foobar(self):
        with self.assertRaises(DataException):
            foobar()

引发错误是因为未绑定的方法未传入self。因为unittest.TestCase.assertRaises需要self和第二个名为expected_exception的参数,所以会得到DataException的异常{1}}作为self的值传入。

您现在必须使用测试运行器来管理您的测试用例;添加

if __name__ == '__main__':
    unittest.main()
在底部

并将您的文件作为脚本运行。然后,您的测试用例将被自动发现并执行。

技术上可以在这样的环境之外使用断言,请参阅Is there a way to use Python unit test assertions outside of a TestCase?,但我建议您坚持创建测试用例。

要进一步验证引发的异常上的代码和消息,请将输入上下文时返回的值指定为with ... as <target>:的新名称;上下文管理器对象捕获引发的异常,以便您可以对其进行断言:

with self.assertRaises(DataException) as context:
    foobar()

self.assertEqual(context.exception.code, 'E101')
self.assertEqual(
    context.exception.msg,
    'There is no data at all for these constraints')

请参阅TestCase.assertRaises() documentation

最后但同样重要的是,请考虑使用DataException子类,而不是使用单独的错误代码。这样,您的API用户可以只捕获其中一个子类来处理特定的错误代码,而不必对代码进行额外的测试,如果不应该在那里处理特定代码则重新加注。

相关问题