Python Django测试登录模块可以';不能核实吗?
我想测试登录重定向,但我使用客户端post用户名和密码,登录验证失败,我调整了很长时间。我不知道为什么 视图: 测试:Python Django测试登录模块可以';不能核实吗?,python,django,testing,Python,Django,Testing,我想测试登录重定向,但我使用客户端post用户名和密码,登录验证失败,我调整了很长时间。我不知道为什么 视图: 测试: 我发现do test,auth.authenticate return None,但我使用brower authenticate return not None,我不知道为什么,一种保护机制?我必须使用client.login func?谢谢@Daniel Roseman@ohannes 我没有为用户创建测试环境 添加一个测试用户,成功了 这是我添加的代码: from djan
我发现do test,auth.authenticate return None,但我使用brower authenticate return not None,我不知道为什么,一种保护机制?我必须使用client.login func?谢谢@Daniel Roseman@ohannes 我没有为用户创建测试环境 添加一个测试用户,成功了 这是我添加的代码:
from django.contrib.auth.models import User
class LoginViewsTests(TestCase):
def setUp(self):
self.adminuser = User.objects.create_user('test_001',
'test001@test.com',
'test1234')
manage.py createsuperuser,在默认数据库中创建用户。manage.py测试创建一个新的空数据库(不包含该超级用户)。要在测试数据库中拥有用户,必须在测试代码中创建它(在测试函数或设置中)-ohannes
ohannes您在测试中创建了用户吗?是的,$python manage.py createsuperuser,是因为它不能被超级用户使用吗?我使用普通用户来尝试,它不起作用。嗯,这不在测试范围内。测试总是从空白数据库开始。哦,那么我应该在测试中创建一个用户吗?
manage.py createsuperuser
,在默认数据库中创建一个用户manage.py test
创建一个新的空数据库(不包含该超级用户)。要在测试数据库中拥有用户,必须在测试代码中创建它(在测试函数或设置中)
def test_login_right(self, username='admin', password='zg123456'):
""" POST right username or password """
setup_test_environment()
client = Client(enforce_csrf_checks=False)
url = '/login/'
response = self.client.post(url,
data={'username': username,
'password': password},
secure=True,
follow=True)
status_code = response.status_code
self.assertNotIn(str(status_code)[0], ['4', '5'])
self.assertIn('Welcome', response.content.decode('utf8'))
from django.contrib.auth.models import User
class LoginViewsTests(TestCase):
def setUp(self):
self.adminuser = User.objects.create_user('test_001',
'test001@test.com',
'test1234')