Django:测试混乱

时间:2010-08-19 14:24:43

标签: django unit-testing

在我写的单元测试中,我为每个TestCase提供了一个测试夹具。但是,当它在测试套件中运行时,我不断发现重复(IntegrityError)。

所以,我想知道,不是应该在每个TestCase的开头创建和填充数据库,并且在每个TestCase中执行每个测试之后,我可以为每个TestCase提供不同的夹具吗?


编辑:添加测试代码

class DashboardTestCase(TestCase):
    fixtures = ['initial.json']

    def setUp(self):
        u = User(username='su')
        u.save()
        self.name = "Test Dashboard"
        self.slug = "test-dashboard"
        self.description = "Test Description"
        self.fiscal_year = "2011"
        self.region = "Test Region"
        self.review_date = "2010-08-01"
        self.date_completed = "2009-03-15"
        self.prepared_by = "Test User"
        self.dashboard = Dashboard(name=self.name, description=self.description, fiscal_year=self.fiscal_year,
                                    region=self.region, review_date=self.review_date, date_completed=self.date_completed,
                                    prepared_by=self.prepared_by)
        self.dashboard.save()

    def testDashboardName(self):
        self.assertEqual(self.dashboard.name, self.name)
    def testDashboardDescription(self):
        self.assertEqual(self.dashboard.description, self.description)
    def testDashboardFiscalYear(self):
        self.assertEqual(self.dashboard.fiscal_year, self.fiscal_year)
    def testDashboardRegion(self):
        self.assertEqual(self.dashboard.region, self.region)
    def testDashboardReviewDate(self):
        self.assertEqual(self.dashboard.review_date, self.review_date)
    def testDashboardDateCompleted(self):
        self.assertEqual(self.dashboard.date_completed, self.date_completed)
    def testDashboardPreparedBy(self):
        self.assertEqual(self.dashboard.prepared_by, self.prepared_by)
    def testDashboardSlug(self):
        self.assertEqual(self.dashboard.slug, self.slug)
    def testDashboardSlugClash(self):
        # Create another dashboard with exactly the same name.
        self.dashboard2 = Dashboard(name='Test Dashboard')
        self.dashboard2.save()
        self.assertEqual(self.dashboard2.slug, 'test-dashboard1')

追溯:

Installing json fixture 'initial_data' from '/Users/Eric/Documents/DjangoProjects/MMR/../MMR/dashboard/fixtures'.
Problem installing fixture '/Users/Eric/Documents/DjangoProjects/MMR/../MMR/dashboard/fixtures/initial_data.json': Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 169, in handle
    obj.save(using=using)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/serializers/base.py", line 165, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/base.py", line 501, in save_base
    rows = manager.using(using).filter(pk=pk_val)._update(values)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/query.py", line 491, in _update
    return query.get_compiler(self.db).execute_sql(None)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 861, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 727, in execute_sql
    cursor.execute(sql, params)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 86, in execute
    return self.cursor.execute(query, args)
  File "build/bdist.macosx-10.5-i386/egg/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "build/bdist.macosx-10.5-i386/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
IntegrityError: (1062, "Duplicate entry 'dashboard-action' for key 'app_label'")

1 个答案:

答案 0 :(得分:2)

  

所以,我想知道,不是应该在每个TestCase的开头创建和填充的数据库

数据库仅在测试运行开始时创建一次。也就是说,当你执行./manage.py test时。在执行每个测试方法之前和之后,分别加载和删除测试夹具。这意味着在执行每个测试方法之前,所有灯具都会加载到数据库中。运行所有测试后,将删除数据库

  

我在测试套件中运行时不断发现重复(IntegrityError)

如果代码存在,我将首先检查代码initial_dataInitial_datasyncdb结束时加载到数据库,这在测试开始之前发生。确保您的initial_data未加载任何有冲突的数据。

第二个要看的是其他灯具,特别是如果每​​个测试类使用多个灯具文件。

<强>更新

Problem installing fixture '/Users/Eric/Documents/DjangoProjects/MMR/../MMR/dashboard/fixtures/initial_data.json':

即使在加载夹具(initial.json)之前,似乎也发生了错误。 initial_data.json内的内容很可能是重复的,从而违反了数据库的完整性。

这就是我要进行故障排除的方法:

"Duplicate entry 'dashboard-action' for key 'app_label'"
  1. 确定哪个模型/表具有与app_label
  2. 匹配的约束
  3. 浏览initial_data.json并查看app_label是否存在两行具有相同值的行。
  4. 删除其中一个,然后重试。
  5. 另外,我建议将测试夹具重命名为其他东西。在initial.jsoninitial_data.json之间很容易混淆。除非你打算当然使用initial_data.json。在这种情况下,fixtures = ...行是多余的,无论如何都可以删除initial_data.json

    更新2

    在沾沾自喜地提出建议并拍拍自己的背后,我发现了Django bug。我正在挖掘更多的污垢。我找到更多后会再次更新。