Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django LiveServerTestCase fails to load a page when I run multiple tests

I am trying to run multiple tests within one Django LiveServerTestCase. When I run any single test (with others commented) everything works as expected. But when I run test case with two tests the first one works fine but the second one loads page with "internal server error" message.

Code:

from django.test import LiveServerTestCase
from selenium.webdriver.firefox.webdriver import WebDriver


class MyLiveServerTestCase(LiveServerTestCase):    
    """
    BaseCleass for my selenium test cases
    """
    @classmethod
    def setUpClass(cls):
        cls.driver = WebDriver()
        cls.url = cls.live_server_url    

        super(MyLiveServerTestCase, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()
        super(MyLiveServerTestCase, cls).tearDownClass()

class AdminEditFormTest(MyLiveServerTestCase):
    """
    Some test case
    """

    def test_valid_data(self):
        """
        test when user enters correct data
        """
        self.driver.get(self.url)
        # ...

    def test_invalid_data(self):
        """ test when user enters INcorrect data """
        self.driver.get(self.url)
        # ...

If I use close() instead of quit() it fails with "error 98: address already in use" similar to this case except I have an error only when I have multiple tests in one LiveServerTestCase class or multiple test cases in one .py file.

How do I make LiveServerTestCase free port on tearDown (if it is the core problem)?

Is there any workaround? All I want are functional selenium tests running equally on local and remote servers.

I am using Django 1.6.7, Firefox 37.0, Selenium 2.45.0

upd

Using methods instead of classmethods leads to the same issue.

def setUp(self):
    self.driver = WebDriver()
    self.url = self.live_server_url    

def tearDown(self):
    self.driver.quit()
like image 848
Sashko Lykhenko Avatar asked Mar 31 '15 20:03

Sashko Lykhenko


1 Answers

Finally, the reason for the "internal server error" message is that WebDriver deletes all data from database on quit(), including contenttypes and other default tables.

This leads to an error when trying to load fixtures at the begin of the next test.

N.B. This behaviour is actually due to the way TransactionTestCase (from which LiveServerTestCase inherits) resets the database after the test runs: it truncates all tables.


So far, my solution is to load fixtures with all the data (also "default" Django data, e.g. contenttypes) on every test run.

class MyLiveServerTestCase(LiveServerTestCase):    
    """
    BaseClass for my Selenium test cases
    """
    fixtures = ['my_fixture_with_all_default_stuff_and_testing_data.json']

    @classmethod
    def setUpClass(cls):
        cls.driver = WebDriver()
        cls.url = cls.live_server_url    
        super(MyLiveServerTestCase, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()
        super(MyLiveServerTestCase, cls).tearDownClass()

Thanks to @help_asap for pointing out this flushing database on quit() problem!

like image 78
Sashko Lykhenko Avatar answered Sep 28 '22 10:09

Sashko Lykhenko