I am trying to run Selenium tests in a Django project (1.5.4) that uses south. I think that the South contradicts my tests when I try to enter the source data using fixtures, but I'm not sure why; I appreciate any help.
According to the Django documentation , appliances should be loaded after the first syncdb, and then all migrations are applied.
Question 1) Does this take into account the southern migration? Do I need to somehow run them separately?
The error that occurs during the execution of my tests seems that my southern migrations are still present in the test database after the first test ... but I thought that each test has its own database (and migrations / devices)? The first test passes / fails, but each subsequent test raises this IntegrityError:
IntegrityError: Problem installing fixture '<PROJECT_PATH>/fixtures/toy_course.json': Could not load contenttypes.ContentType(pk=8): (1062, "Duplicate entry 'south-migrationhistory' for key 'app_label'")
This Southern documentation and SO Question seem to indicate that I need to redefine some type of forward to get working tools, but I'm not quite sure how to apply this to the testing situation instead of production (or if that's what I need) .
Question 2) Do I intend to override forwards in my test setup? Where can i do this?
My respective test code is:
from django.conf import settings from selenium import webdriver from functional_tests.test import SeleniumTestCase class Resources(SeleniumTestCase): fixtures = ['toy_course.json'] def setUp(self): self.browser = webdriver.Chrome(settings.SELENIUM_WEBDRIVER) self.browser.implicitly_wait(3) def tearDown(self): self.browser.quit() def test_main_page_renders_correctly(self): """ User sees a properly formatted main page """ self.open('/RDB/') h3_headers = self.browser.find_elements_by_tag_name('h3') self.assertIn( 'Complete List of Resources', [header.text for header in h3_headers]) self.assertTrue(self.check_exists_by_id('main_table')) self.assertTrue(self.check_exists_by_id('searchDiv')) self.assertTrue(self.check_exists_by_class_name('tablesorter'))
Thanks!
UPDATE
So, as suggested by Alex below and this southern document , I added this line to my .py settings:
SOUTH_TESTS_MIGRATE = False
But now I get 8 out of 8 errors (before I got 1 pass / crash on the first test, and then 7 errors). The complete error for one test is given below:
====================================================================== ERROR: test_table_sorts_on_click (functional_tests.tests.main_resources.Resources) ---------------------------------------------------------------------- Traceback (most recent call last): File "/<PATH TO VIRTUAL ENV>/virtual_environments/relate/lib/python2.7/site-packages/django/test/testcases.py", line 259, in __call__ self._pre_setup() File "/<PATH TO VIRTUAL ENV>/virtual_environments/relate/lib/python2.7/site-packages/django/test/testcases.py", line 479, in _pre_setup self._fixture_setup() File "/<PATH TO VIRTUAL ENV>/virtual_environments/relate/lib/python2.7/site-packages/django/test/testcases.py", line 518, in _fixture_setup **{'verbosity': 0, 'database': db_name, 'skip_validation': True}) File "/<PATH TO VIRTUAL ENV>/virtual_environments/relate/lib/python2.7/site-packages/django/core/management/__init__.py", line 161, in call_command return klass.execute(*args, **defaults) File "/<PATH TO VIRTUAL ENV>/virtual_environments/relate/lib/python2.7/site-packages/django/core/management/base.py", line 255, in execute output = self.handle(*args, **options) File "/<PATH TO VIRTUAL ENV>/virtual_environments/relate/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 193, in handle obj.save(using=using) File "/<PATH TO VIRTUAL ENV>/virtual_environments/relate/lib/python2.7/site-packages/django/core/serializers/base.py", line 165, in save models.Model.save_base(self.object, using=using, raw=True) File "/<PATH TO VIRTUAL ENV>/virtual_environments/relate/lib/python2.7/site-packages/django/db/models/base.py", line 626, in save_base rows = manager.using(using).filter(pk=pk_val)._update(values) File "/<PATH TO VIRTUAL ENV>/virtual_environments/relate/lib/python2.7/site-packages/django/db/models/query.py", line 605, in _update return query.get_compiler(self.db).execute_sql(None) File "/<PATH TO VIRTUAL ENV>/virtual_environments/relate/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1014, in execute_sql cursor = super(SQLUpdateCompiler, self).execute_sql(result_type) File "/<PATH TO VIRTUAL ENV>/virtual_environments/relate/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 840, in execute_sql cursor.execute(sql, params) File "/<PATH TO VIRTUAL ENV>/virtual_environments/relate/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 122, in execute six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2]) File "/<PATH TO VIRTUAL ENV>/virtual_environments/relate/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 120, in execute return self.cursor.execute(query, args) File "/<PATH TO VIRTUAL ENV>/virtual_environments/relate/lib/python2.7/site-packages/MySQLdb/cursors.py", line 201, in execute self.errorhandler(self, exc, value) File "/<PATH TO VIRTUAL ENV>/virtual_environments/relate/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue IntegrityError: Problem installing fixture '/<PATH TO PROJECT>/RDB/fixtures/toy_course.json': Could not load contenttypes.ContentType(pk=8): (1062, "Duplicate entry 'south-migrationhistory' for key 'app_label'")
The command in which I ran:
$ python manage.py test functional_tests
I'm not quite sure if I posed the problem better, worse, or the same, but I seem to agree more with the documentation ...
Thanks!
UPDATE # 2 - with solution
So, a few other pages that helped me figure it out (in addition to Alex's index to the southern doctor). Firstly, this person had a similar problem and solved it using the instruction SOUTH_TESTS_MIGRATE = False. So half of my decision was to include this.
The second half of my decision was to fix the document document. I dumped everything into my fixture with:
$ python manage.py datadump > RDB/fixtures/toy-course.json
This is apparently a bad way to make fixtures with the South - because it also uploads South migration tables to the fixture. In the post above, the blogger uses app-specific apps (which is also mentioned in this SO post ), and that was the key to getting my lights working. Django docs on fixtures show optional parameters to reset only the application, but I did not know that ignoring them would lead to a South conflict. So, the second half of my solution was to create my application for the application:
$ python manage.py datadump RDB > RDB/fixtures/toy-course.json
And my tests now work fine (slow, but maybe another problem)!