绝对为此失去了我的大脑。我无法弄清楚为什么会这样。每次我运行这个测试时,对象都会保存到正常的非测试数据库中。但是,测试结束时的两个断言无论如何都失败了,说他们在数据库中找不到任何用户,即使每次测试运行时我都必须进入管理员删除它在 localhost 上创建的对象。我在我的设置中使用 SQLITE3,并且我知道 SQLITE 测试应该在内存中运行,而不是访问数据库。我已经搜索和搜索,在网上找不到任何有用的信息。这是测试功能:
import time
import datetime
from django.test import TestCase, LiveServerTestCase
from django.core.urlresolvers import resolve
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from django.contrib.auth.models import User
from apps.registration.forms import RegistrationForm
class NewVisitorTest(LiveServerTestCase):
def setUp(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)
def tearDown(self):
self.browser.quit()
def test_registration_process(self):
# Goes to registration page
self.browser.get('http://localhost:8000/register/')
# User can find sign up form
registration_form = self.browser.find_element_by_id('id_registration_form')
# User can fill out sign up form
first_name_input = self.browser.find_element_by_id('id_first_name')
first_name_input.send_keys('Jim')
last_name_input = self.browser.find_element_by_id('id_last_name')
last_name_input.send_keys('Barrow')
date = datetime.date.today()
date_input = self.browser.find_element_by_id('id_birthday')
date_input.send_keys(str(date))
username_input = self.browser.find_element_by_id('id_username')
username_input.send_keys('jim_barrow')
password_input = self.browser.find_element_by_id('id_password')
password_input.send_keys('kittensarecute')
password_1_input = self.browser.find_element_by_id('id_password1')
password_1_input.send_keys('kittensarecute')
email_input = self.browser.find_element_by_id('id_email')
email_input.send_keys('jim_barrow@gmail.com')
# User can submit sign up form
registration_form.submit()
# User is now registered as a user object
users = User.objects.all()
self.assertEqual(len(users), 1)
# User is now registered as a person object
persons = Person.objects.all()
self.assertEqual(len(persons), 1)
if __name__ == '__main__':
unittest.main()
如果我可以提供任何其他上下文,我会很乐意向您展示。这实际上是一个空白项目,因此 settings.py 中没有任何奇怪或不寻常的设置可能会造成混淆。任何帮助将不胜感激。