1👍
why not use reverse: https://docs.djangoproject.com/en/1.10/ref/urlresolvers/#reverse
from django.core.urlresolvers import reverse
....
resp = self.client.get(reverse('campaigns', args=[1]))
where args is the id you need to pass in.
EDIT: since django 1.10 reverse imports from django.urls
0👍
from django.test import TestCase
from django.test import Client
from django.core.urlresolvers import reverse
client = Client()
class MainTest(TestCase):
##user login in django
def user_login(self, username, password):
response = self.client.login(username=username, password=username)
return self.assertEqual(resp.status_code, 200)
## first case
def detail(self):
response = self.client.get('/ad_accounts/<test_num>/') ## url regex
self.assertEquals(response.status_code, 200)
def detail2(self):
response = self.client.get(reverse('campaigns'))
self.assertEqual(response.status_code, 200)
- How to organize data after using filter
- Converting time in django
- Python VBA-like left()
- Signals VS Celery task
0👍
For the test failure, you should first login with some test user then make a request, otherwise the page will be redirected to the login page and thus you will get a 302 status code.
Also you can test the status code of the redirected page with client.get('/foo', follow=True)
which returns the status code of the sign in page. (In your example)
Source:stackexchange.com