[Solved]-How to test url in django

24👍

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)

9👍

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))
👤Selcuk

Leave a comment