我正在尝试在使用South的Django项目(1.5.4)上运行Selenium测试。当我尝试用夹具注入初始数据时,我认为South与我的测试相冲突,但我不确定为什么;我感谢任何帮助。
根据Django documentation,应该在第一个syncdb之后加载灯具,然后应用所有迁移。
问题1)这是否考虑到南迁?我是否需要以某种方式单独运行它们?
我运行测试时得到的错误使我看起来在第一次测试后我的南迁移仍然存在于测试数据库中......但我认为每个测试都有自己的数据库(和迁移/固定装置)?第一个测试通过/失败,但每个后续测试都会引发此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'")
此South documentation和SO question似乎表示我需要覆盖某种类型的前进方法才能让灯具正常工作,但我不完全确定将其应用于测试情况而不是生产(或者如果这是我需要的解决方案)。
问题2)我是否应该在测试设置中覆盖前进?我会在哪里做的?
我的相关测试代码:
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'))
谢谢!
因此,根据Alex的建议和this South doc,我将此行添加到我的settings.py:
SOUTH_TESTS_MIGRATE = False
但我现在得到8个错误中的8个(在我第一次测试中获得1次通过/失败之前,然后是7次错误)。单个测试的完整错误如下:
======================================================================
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'")
我跑的命令:
$ python manage.py test functional_tests
我不太确定我是否更好,更糟或相同,但我似乎更符合文档......
谢谢!
所以其他一些页面帮助我解决了这个问题(除了Alex指向南方文档的指针)。首先,this person had a similar issue,并使用SOUTH_TESTS_MIGRATE = False语句解决它。所以我的解决方案的一半就是包含它。
我的解决方案的后半部分是修复我的夹具文件。我用将所有倾销到我的夹具中:
$ python manage.py datadump > RDB/fixtures/toy-course.json
显然,这是用南方进行装配的一种不好的方法 - 因为它还将南移民表转移到装置中。上面的帖子显示了博主使用特定于应用程序的灯具(在this SO post中也讨论过),这是让我的灯具工作的关键。 The Django docs on fixtures确实显示了可转储应用程序的可选参数,但我不知道忽略它们会导致南方发生冲突。因此,我的解决方案的后半部分是创建我的夹具以特定于应用程序:
$ python manage.py datadump RDB > RDB/fixtures/toy-course.json
我的测试现在运行良好(缓慢,但可能是另一个问题)!
答案 0 :(得分:2)
默认情况下,使用South迁移创建测试数据库。在SOUTH_TESTS_MIGRATE = False
中设置settings.py
,引用docs:
如果这是假的,South的测试运行器集成将进行测试 数据库是使用syncdb创建的,而不是通过迁移( 默认值)。