2012-02-13 34 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