我有一个非常复杂的 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/