2012-02-13 63 views
2

我有一个非常复杂的django应用程序,它具有以下结构。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我从test_obj1.pytest_obj2.py导入TestCase实例,它足以运行所有可用的测试。

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

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应用程序?我GOOGLE了,我什么也没有发现。

回答

2

您应该添加所有测试具有特殊功能:

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

谢谢,它的工作原理。 – 2012-02-13 15:42:46

相关问题