django测试客户端失败,但url工作

时间:2014-06-24 13:41:11

标签: django unit-testing django-testing

我对某些单元测试有一个奇怪的问题,这很简单,但都失败了。奇怪的是,这与runserver一起工作,即我可以渲染perfil_usuario视图,但在单元测试中,这与NoReverseMatch失败。

首先,这是我的主要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")),
)

我的usuarios(主要的第4个url conf)urlconf如下:

# 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"),
)

这是观点:

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

这是失败的单元测试:

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)

反向调用反向(&#34; usuarios:perfil&#34;,kwargs = {&#34; id&#34;:usuario_fake.pk})工作正常,输出/ usuarios / perfil / 1 /。 / p>

致电反向

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

我正在使用Django 1.6。

完整追溯

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+)/$']

1 个答案:

答案 0 :(得分:2)

从回溯中可以看出,id中传递的kwargs被评估为None。这就是它与usuarios/perfil/(?P<id>\\d+)/$ url模式不匹配的原因。

换句话说,usuario_fake.pk值为None

UPD:

问题实际上是在回溯抱怨的模板中。