Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test url in django

I would like to test my url in django but I got error message.

here are my codes:

urls.py

url(r'^create/(?P<userName>[0-9a-zA-Z-]+)/$', views.create, name='create'),

test.py

from django.test import TestCase, Client

client = Client()


def test_create(self):
    """Test the url for "create"
    """

    response = self.client.get('/mission/create/' + str(self.userName))
    self.assertEqual(response.status_code, 200)

and i would get this result:

FAIL: test_create (mission.tests.MissionUrlTests)
Test the url for "create"
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/actinbox/mission/tests.py", line 122, in test_create
    self.assertEqual(response.status_code, 200)
AssertionError: 301 != 200

Please help me with my codes here. Thank you in advance.

like image 997
James Reid Avatar asked Mar 02 '16 07:03

James Reid


People also ask

How do I test my Django site?

The preferred way to write tests in Django is using the unittest module built-in to the Python standard library. This is covered in detail in the Writing and running tests document. You can also use any other Python test framework; Django provides an API and tools for that kind of integration.

How does URL work in Django?

Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info . Once one of the URL patterns matches, Django imports and calls the given view, which is a Python function (or a class-based view).

What is Django test client?

The test client is a Python class that acts as a dummy web browser, allowing you to test your views and interact with your Django-powered application programmatically.

Is testing important in Django?

Testing is vital. Without properly testing your code, you will never know if the code works as it should, now or in the future when the codebase changes.


2 Answers

As @Selcuk said, you are missing the trailing slash in the URL. In order to avoid those typos and in case your URL will change some time, you should use django's reverse function to get URLs:

# django 1.x
from django.core.urlresolvers import reverse
# django 2.x
from django.urls import reverse

def test_create(self):
    response = self.client.get(reverse('create', args=[self.userName]))
    self.assertEqual(response.status_code, 200)
like image 93
ilse2005 Avatar answered Oct 16 '22 12:10

ilse2005


Your URL configuration requires a trailing slash (/) after userName. Since you did not include it in your test URL, Django first returns a redirect (301) to the correct URL.

Change the line

response = self.client.get('/mission/create/' + str(self.userName))

to

response = self.client.get('/mission/create/' + str(self.userName) + '/')

or better

response = self.client.get('/mission/create/{0}/'.format(self.userName))
like image 45
Selcuk Avatar answered Oct 16 '22 11:10

Selcuk