【问题标题】:Cannot create test suite for Django无法为 Django 创建测试套件
【发布时间】:2012-04-11 18:34:38
【问题描述】:

我在 Django 1.3 中创建测试套件时遇到问题。

假设我在名为app_name 的目录中安装了一个应用程序。该目录中的一个文件是foo.py,它定义了一个名为Foo 的类。我想测试一下,所以我还有一个名为foo_test.py 的目录文件,它定义了一个名为FooTest 的类。该文件如下所示:

import unittest
import foo

class FooTest(unittest.TestCase):
  def setUp(self):
    self.foo_instance = foo.Foo()

  ... etc

现在,我将在其他文件中包含其他测试用例,并且我希望将它们全部作为测试套件的一部分运行。所以在同一个目录app_name 我创建了一个文件tests.py 来定义套件。一开始我是这样定义的:

import foo_test

from django.test.simple import DjangoTestSuiteRunner

def suite():
  runner = DjangoTestSuiteRunner()
  return runner.build_suite(['app_name'])

不幸的是,这会失败,因为调用 runner.build_suite(['app_name']) 会在 app_name 中搜索 tests.py 文件,然后执行 suite(),这会递归地继续下去,直到 Python 解释器停止一切以超过最大递归深度。

runner.build_suite(['app_name']) 更改为

runner.build_suite(['app_name.foo_test'])

runner.build_suite(['app_name.foo_test.FooTest'])

导致像ValueError: Test label 'app_name.foo_test' does not refer to a test 这样的错误。

并将其更改为:

runner.build_suite(['foo_test'])

runner.build_suite(['foo_test.FooTest'])

导致类似App with label foo_test could not be found的错误。

在这一点上我有点没有想法。任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: django unit-testing testing django-testing


    【解决方案1】:

    查看Python documentation for organizing tests,并使用其中一种替代方法来构建您的测试套件。顺便说一句,推荐的方法都没有使用build_suite

    【讨论】:

      猜你喜欢
      • 2017-12-31
      • 1970-01-01
      • 2018-07-13
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多