from unittest import TestCase from unittest import TestLoader from unittest import TestSuite def _breadth_first(tree,children=iter): """Traverse the nodes of a tree in breadth-first order. The first argument should be the tree root; children should be a function taking as argument a tree node and returning an iterator of the node's children. """ yield tree last = tree for node in _breadth_first(tree,children): for child in children(node): yield child last = child if last == node: return class AC1(TestCase): def testA(self): pass class ZC2(TestCase): def testA(self): pass def testB(self): pass class BC3(TestCase): def test0A(self): pass def test3B(self): pass def test1C(self): pass if __name__ == "__main__": tests = TestLoader().loadTestsFromName('__main__') def test_children(node): if isinstance(node, TestSuite): return iter(node) return [] #print tests l = list(_breadth_first(tests, test_children)) #print l print 'Length:', len(l)