[英]Django Tests Login/Logout
I'm testing a django app from this tutorial: http://tutorial.djangogirls.org/en/django_admin/README.html 我正在测试本教程中的django应用程序: http : //tutorial.djangogirls.org/en/django_admin/README.html
I've created a test from an example: 我从一个例子中创建了一个测试:
from django.test import TestCase, Client, LiveServerTestCase
from django.utils import timezone
from .models import Post
from django.contrib.auth.models import User
####Lines removed for brevity####
class AdminTest(LiveServerTestCase):
fixtures = ['users.json']
def setUp(self):
self.client = Client()
def test_login(self):
# Get login page
response = self.client.get('/admin/')
# Check response code
self.assertEquals(response.status_code, 200)
# Check 'Log in' in response
self.assertTrue('Log in' in response.content)
# Log the user in
self.client.login(username='XXX', password="XXX")
# Check response code
response = self.client.get('/admin/')
self.assertEquals(response.status_code, 200)
# Check 'Log out' in response
self.assertTrue('Log out' in response.content)
def test_logout(self):
# Log in
self.client.login(username='XXX', password="XXX")
# Check response code
response = self.client.get('/admin/')
self.assertEquals(response.status_code, 200)
# Check 'Log out' in response
self.assertTrue('Log out' in response.content)
# Log out
self.client.logout()
# Check response code
response = self.client.get('/admin/')
self.assertEquals(response.status_code, 200)
# Check 'Log in' in response
self.assertTrue('Log in' in response.content)
however when running python manage.py test
I get this output: 但是在运行python manage.py test
我得到了这个输出:
Creating test database for alias 'default'...
.FE
======================================================================
ERROR: test_logout (blog.tests.AdminTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\shenk\Documents\Programming\django_projects\djangogirls\blog\tests.py", line 80, in test_logout
self.assertTrue('Log out' in response.content)
TypeError: Type str doesn't support the buffer API
======================================================================
FAIL: test_login (blog.tests.AdminTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\shenk\Documents\Programming\django_projects\djangogirls\blog\tests.py", line 56, in test_login
self.assertEquals(response.status_code, 200)
AssertionError: 302 != 200
----------------------------------------------------------------------
I get this 302 response code aka redirect, how can I adjust these two tests to make them pass and be actually usable? 我得到这个302响应代码又名重定向,如何调整这两个测试以使它们通过并实际可用?
And a side-question... how can I print/examine the data in response.content as printing to STDOUT doesn't seem to work in a test? 还有一个侧面问题......如何打印/检查response.content中的数据,因为打印到STDOUT似乎不能在测试中起作用?
I found a similar question where the person was testing logins, but the problem turned out to be that they were creating the user without a password, my user is already created via createsuperuser. 我发现了一个类似的问题,该人正在测试登录,但问题是他们创建的用户没有密码,我的用户已经通过createsuperuser创建。
Looks like the issue is I'm testing for a 200 code after logout, when it's redirecting me to another page. 看起来问题是我在注销后测试200代码,当它将我重定向到另一个页面时。 To fix I should assertEquals for the 304 and then again for the 200 on the page redirected too. 要修复我应该断言304的断言,然后再次重定向页面上的200。 This seems to have fixed it. 这似乎已经解决了。
While Logging in.. 登录时..
# Get login page
response = self.client.get('/admin/login/')
# Check response code
self.assertEquals(response.status_code, 200)
When the URL is set to 'admin/'
, it always gets redirected to admin/login/
. 当URL设置为'admin/'
,它总是被重定向到admin/login/
。 Thus returning response.status_code
as 302 (Which means redirecting). 因此返回response.status_code
为302(表示重定向)。 This is true for Django Version 1.8.4 Django版本1.8.4也是如此
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.