简体   繁体   中英

Django functional LiveServerTestCase - After submitting form with selenium, objects save to non-test database

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()

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.

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

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.

Quick Update:

As of Django 1.11, the server setup by LiveServerTestCase uses any free port assigned by the localhost, not simply 8081.

You can access the live server URL and port using self.live_server_url , as per docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM