1

I've got a Django project in which I have a function which I want to test. Simplified, the function looks like this:

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}")

And my test looks like this:

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/"))

When I run it though, I get the following output:

$ ./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'...

Why does it raise the exceptions, instead of passing the tests? I think I do a comparable thing in another test, which works great.

Does anybody know what I'm doing wrong here?

1 Answer 1

7

You need to pass a callable instead of calling the function itself. Unittest docs for assertRaises

So change it to:

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/")

Other option is to use assertRaises as context manager like this:

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/")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.