2014-07-01 37 views
1

分別運行first_TestCasesecond_TestCase所有工作正常。 但是,當我創建TestSuite時,它只運行first_TestCase。這是爲什麼發生?Python單元測試:TestSuite只運行第一個TestCase

import unittest 
from first_TestCase import first_TestCase 
from second_TestCase import second_TestCase 


    def suite(): 
     suite = unittest.TestSuite() 
     suite.addTest(first_TestCase()) 
     suite.addTest(second_TestCase()) 
     return suite 

if __name__ == "__main__": 
    suite = unittest.defaultTestLoader.loadTestsFromTestCase(first_TestCase) 
    unittest.TextTestRunner().run(suite) 
+0

不是'suite = unittest.defaultTestLoader.loadTestsFromTestCase(first_TestCase)'造成的? – Evert

+0

@Evert:可能是,但根據文檔,它應該「返回包含在TestCase派生的testCaseClass中的所有測試用例的套件。」 – ti01878

+0

我將其解釋爲僅返回包含'first_TestCase'中而不是'second_TestCase'中的測試用例的套件。另外,看起來你的'suite()'的定義不會添加任何東西,因爲它從來沒有在其他地方使用過。 – Evert

回答

0

相反的:

if __name__ == "__main__": 
    suite = unittest.defaultTestLoader.loadTestsFromTestCase(first_TestCase) 
    unittest.TextTestRunner().run(suite) 

我應該使用:

if __name__ == "__main__": 
    unittest.main() 
+0

我不明白如何解決這個問題。 – sheeptest

1

你說:

if __name__ == "__main__": 
    suite = unittest.defaultTestLoader.loadTestsFromTestCase(first_TestCase) 
    unittest.TextTestRunner().run(suite) 

你只從first_TestCase荷載試驗前的權利你通過運行。你永遠不會碰到那個suite()函數。

你應該這樣做:

if __name__ == "__main__": 
    unittest.TextTestRunner().run(suite()) 

因爲你不是要求在當前的實施套件()函數。

相關問題