我正在尝试在使用 South 的 Django 项目(1.5.4)上运行 Selenium 测试。当我尝试使用固定装置注入初始数据时,我认为 South 与我的测试发生冲突,但我不确定为什么;我很感激任何帮助。
根据 Django文档,应该在第一个 syncdb 之后加载夹具,然后应用所有迁移。
问题 1)这是否考虑到南迁移?我需要以某种方式单独运行它们吗?
运行测试时出现的错误使我的 South 迁移似乎在第一次测试后仍然存在于测试数据库中......但我认为每个测试都有自己的数据库(和迁移/固定装置)创建?第一个测试通过/失败,但每个后续测试都会引发此 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 文档和SO question似乎表明我需要重写某种类型的forwards方法才能使夹具工作,但我不完全确定如何将其应用于测试情况而不是生产(或者如果那是我需要的解决方案)。
问题 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 下面的建议和这个 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
我不太确定我是否使问题变得更好、更糟或相同,但我似乎更符合文档...
谢谢!
更新#2——有解决方案
因此,还有其他几页帮助我弄清楚了(除了 Alex 指向 South doc 的指针)。首先,这个人有一个类似的问题,并使用 SOUTH_TESTS_MIGRATE = False 语句解决了它。所以我的一半解决方案是包含它。
我解决方案的后半部分是修复我的夹具文档。我将所有东西都倾倒在我的固定装置中:
$ python manage.py datadump > RDB/fixtures/toy-course.json
显然,用 South 固定它是一种不好的方法——因为它还将 South 迁移表转储到固定装置中。上面的帖子展示了博主使用特定于应用程序的装置(在这篇 SO 帖子中也谈到了),这是让我的装置工作的关键。固定装置上的 Django 文档确实显示了仅转储应用程序的可选参数,但我不知道忽略它们会导致 South 发生冲突。因此,我解决方案的后半部分是创建特定于应用程序的夹具:
$ python manage.py datadump RDB > RDB/fixtures/toy-course.json
我的测试现在运行良好(缓慢,但可能是不同的问题)!