Strange error with reverse() when running tests

94 views
Skip to first unread message

João Sampaio

unread,
Feb 5, 2015, 7:34:51 AM2/5/15
to django...@googlegroups.com
There are 3 Python files attached.

I have my URL file attached. I'm using django-rest-framework (in case you are wondering what the routers are). When I use ./manage.py test with the first file (file1.py), the tests succeed. When I do the very same thing with the second file, the tests fail. The only thing changed between the files is the addition of this code:

    def test_update_member_phone_number(self):
        member
= self.create_national_team_member()

        url
= reverse(r'nationalteam-member-phone-number',
                      kwargs
={r'pk': member.id})

The error is this one:

======================================================================
ERROR
: test_update_member_picture (main.tests.test_national_team.NationalTeamTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
 
File "/home/jpmelos/devel/ideation_admin/ideation_admin/main/tests/test_national_team.py", line 177, in test_update_member_picture
    kwargs
={r'pk': member.id})
 
File "/home/jpmelos/devel/ideation_admin/venv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 551, in reverse
   
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
 
File "/home/jpmelos/devel/ideation_admin/venv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 468, in _reverse_with_prefix
   
(lookup_view_s, args, kwargs, len(patterns), patterns))

NoReverseMatch: Reverse for 'nationalteam-member-update-picture' with arguments '()' and keyword arguments '{'pk': 10}' not found. 1 pattern(s) tried: ['national-team/(?P<pk>\\d)/update_picture/$']

As you can see in the exception text, the pattern does seem to match. The error seems to happen in a test method that is not even related to the one added to the tests. What is going on? It doesn't make any sense to me. How come the mere addition of a call to reverse can cause the tests to crash?
file1.py
file2.py
urls.py

Daniel Roseman

unread,
Feb 6, 2015, 10:10:15 AM2/6/15
to django...@googlegroups.com
Your regex does *not* match: it only accepts a single digit for pk, but  you are passing a two-digit number (10). Modify your regexes to `(?P<pk>\d+)`.
--
DR.

João Sampaio

unread,
Feb 6, 2015, 2:43:32 PM2/6/15
to django...@googlegroups.com
Oh, I totally overlooked that! Thank you.
Reply all
Reply to author
Forward
0 new messages