django - django 应用程序的自定义测试套件

标签 django unit-testing test-suite

我有一个非常复杂的 django 应用程序,其结构如下。

/myapp
/myapp/obj1/..
/myapp/obj1/views.py
/myapp/obj1/forms.py
/myapp/obj2/..
/myapp/obj2/views.py
/myapp/obj2/forms.py
/myapp/tests/..
/myapp/tests/__init__.py
/myapp/tests/test_obj1.py
/myapp/tests/test_obj2.py

我还有很多东西。在 /myapp/tests/__init__.py我导入TestCase来自test_obj1.py的实例和test_obj2.py运行所有可用的测试就足够了。

我想做的是创建一个自定义测试套件。根据文档:

There is a second way to define the test suite for a module: if you define a function called suite() in either models.py or tests.py, the Django test runner will use that function to construct the test suite for that module. This follows the suggested organization for unit tests. See the Python documentation for more details on how to construct a complex test suite.

所以,我创建了这样的函数:

def suite():
    suite = unittest.TestSuite()
    suite.addTest(TestObj1Form())
    suite.addTest(TestObj2Form())
    return suite

但是,当我运行测试时,我收到此错误:ValueError: no such test method in <class 'myproject.myapp.tests.test_obj1.TestObj1Form'>: runTest 。当然,我可以定义这个方法,但是如果我运行测试,它将仅调用这个方法并忽略所有 test*方法。

对于如何正确为 django 应用程序创建自定义测试套件有什么建议吗?我用谷歌搜索过,但没有找到任何相关信息。

最佳答案

您应该使用特殊函数添加所有测试:

suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestObj1Form))

关于django - django 应用程序的自定义测试套件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9261474/

相关文章:

unit-testing - 最小起订量:如何断言我的模拟对象上的方法未运行?

c# - Moq.Mock<T> - 如何设置采用表达式的方法

python - 如何在 python setup.py 中运行测试套件

java - JUnit - 从 TestSuite 访问 TestResult

php - 在一台服务器上结合静态 HTML、Django 后端和 PHP 论坛?

python - Django,ModelMultipleChoiceField 中的初始值仅设置为禁用

当从 jQuery load() 调用 URL 时,javascript 未从 django 模板加载

python - 我无法将 CSS 文件链接到 Django 元素中的 HTML 文件

c# - 使用 Moq 对通用工作单元和存储库模式框架进行单元测试

unit-testing - 从另一个 JUnit 测试类运行 JUnit 测试类