2

I'm having a strange problem with some unit tests, which are quite simple, but are failing. The strange thing is that this work with runserver, i.e, I can render the perfil_usuario view, but in unit tests this is failing with NoReverseMatch.

First of all, this my main urlconf:

from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', url(r"^$", include("traxx.hotsite.urls", namespace="hotsite")), url(r'^admin/', include(admin.site.urls)), url(r'^imperavi/', include('imperavi.urls')), url(r"^app/", include("traxx.sistema.urls", namespace="app")), url(r"^usuarios/", include("traxx.usuarios.urls", namespace="usuarios")), url(r"^notificacoes/", include("traxx.notificacoes.urls", namespace="notificacoes")), url(r"^clientes/", include("traxx.clientes.urls", namespace="clientes")), ) 

My usuarios (4th url conf in main) urlconf is the following:

# coding: utf-8 from django.conf.urls import patterns, include, url urlpatterns = patterns("traxx.usuarios.views", url(r"^perfil/(?P<id>\d+)/$", "perfil_usuario", name="perfil"), url(r"^configuracoes/(?P<id>\d+)/$", "configuracoes_usuario", name="configuracoes"), url(r"^login/$", "login", name="login"), url(r"^logout/$", "logout", name="logout"), ) 

This is the view:

def perfil_usuario(request, id): usuario = get_object_or_404(Usuario, pk=id) return render(request, "usuarios/perfil_usuario.html", {"usuario": usuario}) 

And this is the failing unit test:

class PerfilUsuarioViewTestCase(TransactionTestCase): """Testes para a view home""" def setUp(self): pass def test_get_perfil_usuario(self): usuario_fake = mommy.make(Usuario, nome_usuario="teste") c = Client() resposta = c.get(reverse("usuarios:perfil", kwargs={"id": usuario_fake.pk})) self.assertEqual(200, resposta.status_code) 

The reverse call reverse("usuarios:perfil", kwargs={"id": usuario_fake.pk}) works just fine, outputing /usuarios/perfil/1/.

Call to reverse

>>> from django.core.urlresolvers import reverse >>> reverse("usuarios:perfil", kwargs={"id":1}) /home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/imperavi/views.py:1: DeprecationWarning: the md5 module is deprecated; use hashlib instead import md5 '/usuarios/perfil/1/' 

I'm using Django 1.6.

Full traceback

Traceback (most recent call last): File "/home/george/projetos/traxx/src/traxx/testes/usuarios/views.py", line 21, in test_get_perfil_usuario resposta = c.get(reverse("usuarios:perfil", kwargs={"id": usuario_fake.pk})) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/test/client.py", line 473, in get response = super(Client, self).get(path, data=data, **extra) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/test/client.py", line 280, in get return self.request(**r) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/test/client.py", line 444, in request six.reraise(*exc_info) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 114, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/george/projetos/traxx/src/traxx/usuarios/views.py", line 12, in perfil_usuario return render(request, "usuarios/perfil_usuario.html", {"usuario": usuario}) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/shortcuts/__init__.py", line 53, in render return HttpResponse(loader.render_to_string(*args, **kwargs), File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/template/loader.py", line 169, in render_to_string return t.render(context_instance) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/template/base.py", line 140, in render return self._render(context) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/test/utils.py", line 85, in instrumented_test_render return self.nodelist.render(context) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/template/base.py", line 840, in render bit = self.render_node(node, context) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/template/debug.py", line 78, in render_node return node.render(context) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 123, in render return compiled_parent._render(context) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/test/utils.py", line 85, in instrumented_test_render return self.nodelist.render(context) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/template/base.py", line 840, in render bit = self.render_node(node, context) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/template/debug.py", line 78, in render_node return node.render(context) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 155, in render return self.render_template(self.template, context) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 137, in render_template output = template.render(context) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/template/base.py", line 140, in render return self._render(context) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/test/utils.py", line 85, in instrumented_test_render return self.nodelist.render(context) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/template/base.py", line 840, in render bit = self.render_node(node, context) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/template/debug.py", line 78, in render_node return node.render(context) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/template/defaulttags.py", line 447, in render six.reraise(*exc_info) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/template/defaulttags.py", line 433, in render url = reverse(view_name, args=args, kwargs=kwargs, current_app=context.current_app) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 509, in reverse return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)) File "/home/george/projetos/.virtualenvs/traxx/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 429, in _reverse_with_prefix (lookup_view_s, args, kwargs, len(patterns), patterns)) NoReverseMatch: Reverse for 'perfil' with arguments '(None,)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'usuarios/perfil/(?P<id>\\d+)/$'] 
2
  • Could you show the full error traceback? Thanks. Commented Jun 24, 2014 at 13:55
  • Sure, I'll post it now. Commented Jun 24, 2014 at 14:00

1 Answer 1

2

As you can see from the traceback, id passed in kwargs is evaluated to None. This is why it is not matching the usuarios/perfil/(?P<id>\\d+)/$ url pattern.

In other words usuario_fake.pk value is None.

UPD:

the problem was actually in the template which the traceback was complaining about.

Sign up to request clarification or add additional context in comments.

6 Comments

I'll check that. Thanks for looking all this code. Sometimes we cannot see the most obvius things.
@George ok, I haven't personally used model_mommy, but a shot in the dark would be to replace usuario_fake.pk with usuario_fake.id..
I've tried just that. I've added print statements to the test, printing pk and id. Both output 1. I even hardcoded "1" for this test. The reverse method works just fine, check the updated question.
@George ok, thanks, so if you hardcode 1 the test works, right?
@George could you also try to use self.client instead of instantiating Client manually? self.client.get(reverse("usuarios:perfil", kwargs={"id": usuario_fake.pk}))
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.