revsys / django-test-plus

Useful additions to Django's default TestCase

Home Page:https://django-test-plus.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Failing request with plain url produces excess backtraces

ttolv opened this issue · comments

commented

BaseTestCase.request allows e.g. self.get() with a named url, or a plain url as a fallback where the method is called within exception handling for reverse().

        try:
            self.last_response = method(reverse(url_name, args=args, kwargs=kwargs), data=data, follow=follow, **extra)
        except NoReverseMatch:
            self.last_response = method(url_name, data=data, follow=follow, **extra)

In the latter case, if the method fails, the exception chain will include less relevant traces:

Traceback (most recent call last):
  File "[...]/site-packages/django/urls/base.py", line 75, in reverse
    extra, resolver = resolver.namespace_dict[ns]
KeyError: 'https'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "[...]/site-packages/test_plus/test.py", line 133, in request
    self.last_response = method(reverse(url_name, args=args, kwargs=kwargs), data=data, follow=follow, **extra)
  File "[...]/site-packages/django/urls/base.py", line 86, in reverse
    raise NoReverseMatch("%s is not a registered namespace" % key)
django.urls.exceptions.NoReverseMatch: 'https' is not a registered namespace

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  [the actual test exception goes here]

Probably it would be preferable to call the method outside exception handling for reverse(), i.e.

        try:
            url = reverse(url_name, args=args, kwargs=kwargs)
        except NoReverseMatch:
            if args or kwargs:
                raise # unpopped (kw)args implies named url
            url = url_name
        self.last_response = method(url, data=data, follow=follow, **extra)

😃 No reverse()-related traces in failing tests when plain url used
😢 NoReverseMatch lost if invalid named url used without (kw)args, will barf only in fallback instead
🤔 Plain url with discarded (kw)args will barf non-backwards-compleniently