【问题标题】:Fixtures in Django testing with South / Selenium使用 South / Selenium 进行 Django 测试的装置
【发布时间】:2013-10-03 00:55:01
【问题描述】:

我正在尝试在使用 South 的 Django 项目 (1.5.4) 上运行 Selenium 测试。当我尝试使用固定装置注入初始数据时,我认为 South 与我的测试发生冲突,但我不确定为什么;感谢您的帮助。

根据 Django documentation,fixture 应该在第一个 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 documentationSO question 似乎表明我需要重写某种类型的 forwards 方法才能使固定装置正常工作,但我不完全确定如何将其应用于测试情况而不是生产环境(或者如果这是我需要的解决方案)。

问题 2) 我应该在我的测试设置中覆盖 forwards 吗?我会在哪里做呢?

我的相关测试代码:

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

我不太确定我是否使问题变得更好、更糟或相同,但我似乎更符合文档...

谢谢!


更新 #2 -- 有解决方案

还有其他几页帮助我弄清楚了(除了 Alex 指向 South doc 的指针)。首先,this person had a similar issue,并使用 SOUTH_TESTS_MIGRATE = False 语句解决了它。所以我的一半解决方案是包含它。

我解决方案的后半部分是修复我的夹具文档。我正在将所有东西倾倒到我的固定装置中:

$ python manage.py datadump > RDB/fixtures/toy-course.json

显然,用 South 进行固定是一种不好的方法——因为它还将 South 迁移表转储到固定中。上面的帖子展示了博主使用特定于应用程序的装置(在this SO post 中也讨论过),这是让我的装置工作的关键。 The Django docs on fixtures 确实显示了仅转储应用程序的可选参数,但我不知道忽略它们会导致 South 发生冲突。因此,我的解决方案的后半部分是创建特定于应用程序的夹具:

$ python manage.py datadump RDB > RDB/fixtures/toy-course.json

我的测试现在运行良好(缓慢,但可能是不同的问题)!

【问题讨论】:

    标签: django unit-testing selenium django-south django-fixtures


    【解决方案1】:

    默认情况下,您的测试数据库是使用 South 迁移创建的。在您的settings.py 中设置SOUTH_TESTS_MIGRATE = False,引用docs

    如果这是 False,South 的测试运行器集成将使测试 使用 syncdb 创建数据库,而不是通过迁移( 默认)。

    【讨论】:

    • 嗨,亚历克斯,感谢您的回答。我完全错过了那个文档页面......但是当我将它添加到我的 settings.py 中时,我现在在运行 $python manage.py test functional_tests 时得到 8 个错误中的 8 个(而不是 1 个通过/失败 + 7 个错误)。与上述相同的错误...我将在问题的更新中提供更多详细信息。我还缺少其他东西吗?我也会做更多的研究。感谢您的帮助!
    • 完全归功于亚历克斯,因为他为我指明了正确的方向。我想通了——在我的第二次更新中给出了完整的答案。
    猜你喜欢
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 2011-08-13
    • 2011-06-15
    • 2011-06-01
    • 2022-01-23
    • 2012-04-11
    • 1970-01-01
    相关资源
    最近更新 更多