Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract a list of TestCases from a TestSuite?

I am using Python's unittest with simple code like so:

suite = unittest.TestSuite()
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(module1))
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(module2))

However, I am wanting to do some custom things to each test after they have been gathered by the suite. I thought I could do something like this to iterate over the test cases in suite:

print suite.countTestCases()
for test in suite:             # Also tried with suite.__iter__()
    # Do something with test
    print test.__class__

However, for as many test cases as I load, it only ever prints

3
<class 'unittest.suite.TestSuite'>

Is there a way I can get all the objects of class TestCase from the suite? Is there some other way I should be loading test cases to facilitate this?

like image 307
elynnaie Avatar asked Jan 25 '12 14:01

elynnaie


1 Answers

Try

  for test in suite:
    print test._tests
like image 129
Dor Shemer Avatar answered Oct 21 '22 07:10

Dor Shemer