【问题标题】:run every TestCase inside a module在模块中运行每个 TestCase
【发布时间】:2013-01-29 22:24:11
【问题描述】:

如何在测试包下的特定模块中运行所有TestCase 类的测试?

在一个 Django 项目中,我在 tests/ 下拆分了 tests.py
每个文件(模块)都有几个 TestCase 类,每个类都有几个测试方法。 init.py 导入它们中的每一个。

我已经知道我可以做到这些:

  1. 运行所有测试:

    ./manage.py test myapp
    
  2. 或者运行特定的TestCase:

    ./manage.py test myapp.OneOfManyTestCase    
    
  3. 或者从 TestCase 类运行非常具体的测试方法:

    ./manage.py test myapp.OneOfManyTestCase.test_some_small_method
    

但是,我不知道如何从特定模块运行每个 TestCases。
比如说,OneOfManyTestCase 类来自tests/lot_of_test.py,还有其他测试用例。
Django 似乎并不关心带有 TestCases 的模块。

如何在lot_of_test 中运行所有的测试用例?

【问题讨论】:

    标签: django django-testing


    【解决方案1】:

    我认为要实现这一点,您需要从 DjangoTestSuiteRunner 继承您自己的 TestRunner 并覆盖 build_suite 方法。

    【讨论】:

    • 最终这样做了。感谢您的提示:)
    【解决方案2】:

    我最终选择了writing down my own TestSuiteRunner,就像@sneawo 所说的那样。

    Django-style 失败后,尝试照常导入 python-style。

    One line 修复:

    suite.addTest(build_test(label))
    

    进入

    try:
        suite.addTest(django.test.simple.build_test(label))
    except ValueError:
        # change to python-style package name
        head, tail = label.split('.', 1)
        full_label = '.'.join([head, django.test.simple.TEST_MODULE, tail])
        # load tests
        tests = unittest.defaultTestLoader.loadTestsFromName(full_label)
        suite.addTests(tests)
    

    并在settings.py 中设置TEST_RUNNER

    TEST_RUNNER='myapp.tests.module_test_suite_runner.ModuleTestSuiteRunner'
    

    【讨论】:

      猜你喜欢
      • 2016-09-30
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多