Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango - Unit Tests on localhost creating objects in main database, but not finding them in the test database later in the method
    primarykey
    data
    text
    <p>Experiencing some fairly strange behaviour with django unit tests. I have a class inherited from django.test.testcase that verifies a registration form creates a person object and a user object, which it does, but I can subsequently look for and find the objects in the Admin interface after the tests have finished. This is the test:</p> <pre><code>import unittest import time from django.test import TestCase from selenium import webdriver from selenium.webdriver.common.keys import Keys from django.contrib.auth.models import User from apps.persons.models import Person class NewVisitorTest(TestCase): def setUp(self): self.browser = webdriver.Firefox() self.browser.implicitly_wait(3) def tearDown(self): self.browser.quit() def test_can_sign_up_and_login(self): # User goes to register url self.browser.get('http://localhost:8000/register/') # User can see that she has come to the right page on the website self.assertIn('Register', self.browser.title) # User can see that there is a sign up form signup_form = self.browser.find_element_by_id('id_registration_form') # User can enter text into the first name field first_name_input = self.browser.find_element_by_id('id_first_name') first_name_input.send_keys('Jim') # User can enter text into the last name field last_name_input = self.browser.find_element_by_id('id_last_name') last_name_input.send_keys('bob') # User can enter text into the username field username_input = self.browser.find_element_by_id('id_username') username_input.send_keys('jim_bob') # User can enter text into the email field email_input = self.browser.find_element_by_id('id_email') email_input.send_keys('jim_bob@jimbob.com') # User can enter text into the first password field password_input = self.browser.find_element_by_id('id_password') password_input.send_keys('kittensarecute') # User can enter text into the second password field password_1_input = self.browser.find_element_by_id('id_password1') password_1_input.send_keys('kittensarecute') # Submit form signup_form.submit() time.sleep(20) persons = Person.objects.all() print persons self.assertEqual(len(persons), 1) users = User.objects.all() print users self.assertEqual(len(users), 1) # Deliberately fail self.fail('Finish the test!') if __name__ == '__main__': unittest.main() </code></pre> <p>The code fails on assertEqual(len(persons), 1). I can reproduce the same behaviour in the browser and have no issues. Each time I run the test I have to delete the user it creates in the main local database, so I'm assuming it's saving the objects in the database defined in settings['databases'], but then looking for them in the test database later in the method. I see in the terminal that the test database is created at the beginning of each test, so it's definitely there, which seems confusing. Any help would be massively appreciated. </p> <p>EDIT: It's all on sqlite</p> <pre><code> File ".../tests/tests.py", line 60, in test_can_sign_up_and_login self.assertEqual(len(persons), 1) AssertionError: 0 != 1 </code></pre> <p>Edit 2:</p> <pre><code>DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': '/Users/me/github/stage/project/data/db.db', # Or path to database file if using sqlite3. # The following settings are not used with sqlite3: 'USER': '', 'PASSWORD': '', 'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP. 'PORT': '', # Set to empty string for default. } } </code></pre> <p>I have a folder with apps in them, so to run these tests and run:</p> <pre><code>python manage.py test tests </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload