2

I am trying to add some selenium tests to my Django project, but the second test always fails with a Server Error (500) . Since both tests start exactly the same, I figure it must have something to do with the setUp and tearDown methods. Can someone help? Thanks.

from django.test import LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from django.contrib.auth.models import User 
from selenium.webdriver.support.ui import Select
class UserTest(LiveServerTestCase):
    def setUp(self):
        User.objects.create_user(username='user', password='pass', email='test@test.com')
        self.browser = webdriver.Chrome()

    def tearDown(self):
        self.browser.quit()

    def changeSelector(self, browser, value):
        mealSelector = Select(browser.find_element_by_id('mealsToday'))
        mealSelector.select_by_visible_text(str(value))

    def login_user(self):
        self.browser.get(self.live_server_url)
        self.timeout(5)
        self.assertIn('Animals', self.browser.title)
        # Log in
        login_button = self.browser.find_element_by_id('login').click()
        self.browser.find_element_by_id('id_username').send_keys('user')
        self.browser.find_element_by_id('id_password').send_keys('pass')

    def timeout(self, time_to_sleep):
        import time
        time.sleep(time_to_sleep)

    def test_one_test(self):
        self.login_user()

    def test_two_test(self):
        self.login_user()

Edit: I should mention that the first test works fine and returns success. Any test after the first one fails right on starting up with the 500 error.

Edit 2: What I see when I run my tests:

======================================================================
FAIL: test_two_test (functional_tests.tests.UserTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/functional_tests/tests.py", line 34, in test_two_test
    self.login_user()
  File "/functional_tests/tests.py", line 20, in login_user
    self.assertIn('Animals', self.browser.title)
AssertionError: 'Animals' not found in 'http://localhost:8081/'

Even this minimal code fails:

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 selenium.webdriver.support.ui import Select
class UserTest(StaticLiveServerTestCase):
    def setUp(self):
        self.browser = webdriver.Chrome()

    def tearDown(self):
        self.browser.quit()

    def login_user(self):
        self.browser.get(self.live_server_url)
        self.assertIn('Animals', self.browser.title)
    def test_one_test(self):
        self.login_user()

    def test_two_test(self):
        self.login_user()

The second time the get is called in the second method I can see that the 500 Error is there and that nothing is correctly loaded. Why would this be?

skaz
  • 21,962
  • 20
  • 69
  • 98

3 Answers3

2

After some code to be able to show me errors (the testing suite sets DEBUG=False to closer simulate the real env) by setting DEBUG=True

Then I saw that the code was bombing because a row wasn't there that the system expected. This is because I add this row in a migration script. This passes the first test because all migration scripts are run at the beginning of testing, but after all data is deleted after the first test, it is never added again.

Community
  • 1
  • 1
skaz
  • 21,962
  • 20
  • 69
  • 98
1

It's difficult to say without seeing the complete traceback, but, it could be because of the create_user() call - it fails to create a user with an existing username. Try moving the create_user() under the setUpClass():

class UserTest(LiveServerTestCase):
    @classmethod
    def setUpClass(cls):
        User.objects.create_user(username='user', password='pass', email='test@test.com')
        super(UserTest, cls).setUpClass()

    def setUp(self):
        self.browser = webdriver.Chrome()
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Sorry for the no traceback - it is hard because I am not getting any on the test - just the 500 result. I don't know how to get the traceback. Any ideas? – skaz Jun 23 '15 at 20:30
  • @skaz what do you see on the console when you run the tests? Do you have logging configured? – alecxe Jun 23 '15 at 20:31
  • I will edit my post and show you what I see. I don't have logging configured - I am doing a tutorial and haven't gotten that far. – skaz Jun 24 '15 at 05:06
  • I find it extremely hard to find documentation of classes for django. when I google 'LIveServerTestCase documentation' I am always brought to the somewhat tutorial like explanation of how the class works, but can't find anywhere the actual class definition. Does this exist anywhere? Thanks. – skaz Jun 24 '15 at 05:17
  • something more fundamental is going on here - even when I remove the login code, a simple call to `browser.get` fails with the same error. – skaz Jun 24 '15 at 06:27
  • Also, added logging and I see no error. Just the calls to the pages which all look fine. – skaz Jun 24 '15 at 07:47
  • Aha! I was able to enable debugging for the test server and now I see that the code is bombing on a row in the database that gets populated from a migration script which must not be getting called the second time around. Django probably assumes the database structure can be built up, then the data deleted, and then start from fresh again, but my migration has data in it. What can I do here? – skaz Jun 24 '15 at 08:00
0

Maybe, it will be useful for somebody.

I have problems with testing with Selenium using LiveServerTestCase, Debug was True, all about last answers were about was OK.

But, earlier I tried to deploy my web-application on Heroku, and I have such line of code in my settings.py:

django_heroku.settings(locals())

But when I removed it, I didn't catch this error.

Valentyn
  • 35
  • 4