如何测试 Python/Django 中引发的异常?

kra*_*r65 0 python django unit-testing exception

我有一个 Django 项目,其中有一个我想测试的函数。简化后,该函数如下所示:

class InvalidUrlError(Exception):
    pass

def get_info_from_url(url):
    try:
        return url.split(':')[1].split('/')[0]
    except Exception:
        raise InvalidUrlError(f"Invalid url: {url}")
Run Code Online (Sandbox Code Playgroud)

我的测试如下所示:

class ToolsTestCase(TestCase):
    def test_get_info_from_url_wrong_formatted_url(self):
        self.assertRaises(InvalidUrlError, get_info_from_url("https://acc.images.data.ourcompany.com/"))
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到以下输出:

$ ./manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
....E
======================================================================
ERROR: test_get_info_from_url_wrong_formatted_url (checker.tests.ToolsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/kramer65/repos/auth-proxy/app/checker/tools.py", line 10, in get_info_from_url
    return url.split(':')[1].split('/')[0]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/kramer65/repos/auth-proxy/app/checker/tests.py", line 57, in test_get_info_from_url_wrong_formatted_url
    self.assertRaises(InvalidUrlError, get_info_from_url("https://acc.images.data.ourcompany.com/"))
  File "/home/kramer65/repos/auth-proxy/app/checker/tools.py", line 15, in get_info_from_url
    raise InvalidUrlError(f"Invalid url: {url}")
checker.tools.InvalidUrlError: Invalid url: https://acc.images.data.ourcompany.com/

----------------------------------------------------------------------
Ran 5 tests in 0.037s

FAILED (errors=1)
Destroying test database for alias 'default'...
Run Code Online (Sandbox Code Playgroud)

为什么它会引发异常,而不是通过测试?我想我在另一个测试中做了类似的事情,效果很好。

有人知道我在这里做错了什么吗?

Gas*_*nov 6

您需要传递可调用函数而不是调用函数本身。assertRaises 的单元测试文档

所以将其更改为:

class ToolsTestCase(TestCase):
    def test_get_info_from_url_wrong_formatted_url(self):
        self.assertRaises(InvalidUrlError, get_info_from_url, "https://acc.images.data.ourcompany.com/")
Run Code Online (Sandbox Code Playgroud)

其他选项是用作assertRaises上下文管理器,如下所示:

class ToolsTestCase(TestCase):
    def test_get_info_from_url_wrong_formatted_url(self):
        with self.assertRaises(InvalidUrlError):
            get_info_from_url("https://acc.images.data.ourcompany.com/")
Run Code Online (Sandbox Code Playgroud)