How do I unit test django urls? -
i have achieved 100% test coverage in application everywhere except urls.py
. have recommendations how write meaningful unit tests urls?
fwiw question has arisen experimenting test-driven development , want failing tests before write code fix them.
one way reverse
url names , validate
example
urlpatterns = [ url(r'^archive/(\d{4})/$', archive, name="archive"), url(r'^archive-summary/(\d{4})/$', archive, name="archive-summary"), ]
now, in test
from django.urls import reverse url = reverse('archive', args=[1988]) assertequal(url, '/archive/1988/') url = reverse('archive-summary', args=[1988]) assertequal(url, '/archive-summary/1988/')
you testing views anyways.
now, test url connect right view, use resolve
from django.urls import resolve resolver = resolve('/summary/') assertequal(resolver.view_name, 'summary')
now in variable resolver
(resolvermatch
class instance), have following options
'app_name', 'app_names', 'args', 'func', 'kwargs', 'namespace', 'namespaces', 'url_name', 'view_name'
Comments
Post a Comment