【发布时间】:2018-02-24 21:54:17
【问题描述】:
我已经被这个问题困扰了几个小时。我是 django 和自动化测试的新手,我一直在玩 Selenium 和 django 的 StaticLiveServerTestCase。我一直在尝试测试我的登录表单(顺便说一下,当我使用 runserver 并自己测试它时效果很好。)
一切都很好,除了我似乎无法成功登录我的测试用户。我已将断点范围缩小到 django 的 User.authenticate 方法。
在我的 setUp 中成功创建了 User 对象,我可以通过在我的测试方法中访问它的属性来确认这一点。但是,身份验证失败。
我查看了以下内容以寻求帮助,但它们并没有让我走得太远:
- Django Unittests Client Login: fails in test suite, but not in Shell
- Authentication failed in Django test
知道为什么身份验证失败吗?我需要在我的设置中添加一些内容吗?
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from django.contrib.auth.models import User
from django.contrib.auth import authenticate
class AccountTestCase(StaticLiveServerTestCase):
def setUp(self):
self.selenium = webdriver.Chrome()
super().setUp()
User.objects.create(username='test', email='test@test.com', password='Test1234', is_active=True)
def tearDownClass(self):
self.selenium.quit()
super().tearDown()
def test_register(self):
user = authenticate(username='test', password='Test1234')
if user is not None: # prints Backend login failed
print("Backend login successful")
else:
print("Backend login failed")
user = User.objects.get(username='test')
print(user)
print(user.username) # prints test
print(user.password) # prints Test1234
【问题讨论】:
标签: django selenium django-authentication django-tests