Django功能LiveServerTestCase-使用硒提交表单后,对象保存到非测试数据库 [英] Django functional LiveServerTestCase - After submitting form with selenium, objects save to non-test database

查看:83
本文介绍了Django功能LiveServerTestCase-使用硒提交表单后,对象保存到非测试数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这件事上我完全失去了头脑。我不知道为什么会这样。每次运行此测试时,该对象都会保存到普通的非测试数据库中。但是,测试结束时两个断言无论如何都会失败,表示他们在数据库中找不到任何用户,即使每次运行测试时,我都必须进入管理员才能删除在本地主机上创建的对象。我在设置中使用的是SQLITE3,并且我了解SQLITE测试应该在内存中运行,而不是访问数据库。我已经搜索了很多东西,但在网络上找不到任何有用的信息。测试功能如下:

Absolutely losing my brain over this. I can't figure out why this is happening. Each time I run this test, the object gets saved to the normal, non-test database. However, both assertions at the end of the test fail anyway, saying they can't find ANY users in the database, even though each time the test runs I have to go into the admin to delete the objects it's created on localhost. I'm using SQLITE3 in my settings, and I understand that SQLITE tests are supposed to run in memory, rather than hitting the database. I've searched and searched and can't find any useful information on the web. Here's the test function:

 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中没有任何奇怪或不寻常的设置,可能会使您感到困惑。

If there's any other context I can provide, I'll happily show you. This is practically a blank project, so there aren't any strange or unusual settings in settings.py which might confuse things. Any help would be greatly appreciated.

推荐答案

根据 LiveServerTestCase docs ,实时服务器在端口上默认为8081。但是,您是从端口8000抓取页面。

According to the LiveServerTestCase docs, the live server is on port 8081 by default. However you are fetching the page from port 8000 instead.

我希望您在端口8000上运行开发服务器,并且您的测试正在连接到该服务器,因此新对象将出现在非8000端口中。测试数据库。您需要更改代码以从端口8081提取页面。

I expect you are running the dev server on port 8000 and your tests are connecting to it, so your new objects appear in the non-test database. You need to change your code to fetch the page from port 8081 instead.

这篇关于Django功能LiveServerTestCase-使用硒提交表单后,对象保存到非测试数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆