unit test: AssertionError: 302 != 200

1,324 views
Skip to first unread message

Kim

unread,
Jun 20, 2014, 10:47:24 PM6/20/14
to django...@googlegroups.com
Hi everyone, 
I'm new to Django and am running a test. 

For my admintest in tests.py, I have the following:
--------------
class AdminTest(LiveServerTestCase):
    def test_login(self):
        c = Client()

        response = c.get('/admin/')
        self.assertEquals(response.status_code,200)
        self.assertTrue('Log in' in response.content)
        c.login(username = 'username',password='password')
        response = c.get('/admin/')
        self.assertEquals(response.status_code,200)
        self.assertTrue('Log out' in response.content)

-------------
And I get this error. 

-------------
$ python manage.py test
Creating test database for alias 'default'...
.F
======================================================================
FAIL: test_login (blogengine.tests.AdminTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/Desktop/Python/1.7/src/blogengine/tests.py", line 35, in test_login
    self.assertEquals(response.status_code,200)
AssertionError: 302 != 200

----------------------------------------------------------------------
Ran 2 tests in 0.510s

FAILED (failures=1)
Destroying test database for alias 'default'...
-------------

Since I checked the admin is actually working(status 200) in my localhost, I thought I should give 200 status instead of 302. 

Please give me some advice to fix this.

Many thanks,
Kim

marcow...@gmail.com

unread,
Jun 21, 2014, 4:05:56 AM6/21/14
to django...@googlegroups.com
Hi,

possibly just your expectation about the response status code is too
much focused on 200.

See e.g. https://en.wikipedia.org/wiki/HTTP_302


HTH, Marco
--
Marco Wahl -- Freelancer


Kimitaka Nakazawa

unread,
Jun 21, 2014, 6:19:53 AM6/21/14
to django...@googlegroups.com, marcow...@gmail.com
Thanks, Marco!
It solved the question.

Kim

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/84vbrud7up.fsf%40tm6592.fritz.box.
For more options, visit https://groups.google.com/d/optout.

kenju254

unread,
Oct 23, 2014, 11:28:35 AM10/23/14
to django...@googlegroups.com
Hi Kim,

I was going through Django 1.7 and noticed that you have to force the redirect on this line

response = c.get('/admin/' follow=True)

Regards
Kenneth Kinyanjui
Reply all
Reply to author
Forward
0 new messages