我使用Python的單元測試與簡單的代碼如下所示:如何從TestSuite中提取測試用例列表?
suite = unittest.TestSuite()
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(module1))
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(module2))
不過,我想要做一些自定義的東西,每個測試,他們已經被收集套件後。我想我可以做這樣的事情的測試用例迭代的套件:
print suite.countTestCases()
for test in suite: # Also tried with suite.__iter__()
# Do something with test
print test.__class__
然而,對於儘可能多的測試案例,因爲我加載,它永遠只能打印
3
<class 'unittest.suite.TestSuite'>
有沒有一種方法我可以從套件中獲取TestCase類的所有對象?有沒有其他一些方法可以加載測試用例來促進這一點?
好的,我認爲這就是我想要的。我注意到_tests,但我試圖稱它爲'print suite._tests'。我想我會把它當成Python的noob。 – denaje
我打算去,但顯然你必須等10分鐘才能接受。非常感謝! – denaje