This should finally fix #6896. Let's watch the buildbots.
[python.git] / Lib / unittest / suite.py
blob730b4d9747abc11214515a0ea289736469ff4909
1 """TestSuite"""
3 from . import case
4 from . import util
7 class TestSuite(object):
8 """A test suite is a composite test consisting of a number of TestCases.
10 For use, create an instance of TestSuite, then add test case instances.
11 When all tests have been added, the suite can be passed to a test
12 runner, such as TextTestRunner. It will run the individual test cases
13 in the order in which they were added, aggregating the results. When
14 subclassing, do not forget to call the base class constructor.
15 """
16 def __init__(self, tests=()):
17 self._tests = []
18 self.addTests(tests)
20 def __repr__(self):
21 return "<%s tests=%s>" % (util.strclass(self.__class__), list(self))
23 def __eq__(self, other):
24 if not isinstance(other, self.__class__):
25 return NotImplemented
26 return list(self) == list(other)
28 def __ne__(self, other):
29 return not self == other
31 # Can't guarantee hash invariant, so flag as unhashable
32 __hash__ = None
34 def __iter__(self):
35 return iter(self._tests)
37 def countTestCases(self):
38 cases = 0
39 for test in self:
40 cases += test.countTestCases()
41 return cases
43 def addTest(self, test):
44 # sanity checks
45 if not hasattr(test, '__call__'):
46 raise TypeError("the test to add must be callable")
47 if isinstance(test, type) and issubclass(test,
48 (case.TestCase, TestSuite)):
49 raise TypeError("TestCases and TestSuites must be instantiated "
50 "before passing them to addTest()")
51 self._tests.append(test)
53 def addTests(self, tests):
54 if isinstance(tests, basestring):
55 raise TypeError("tests must be an iterable of tests, not a string")
56 for test in tests:
57 self.addTest(test)
59 def run(self, result):
60 for test in self:
61 if result.shouldStop:
62 break
63 test(result)
64 return result
66 def __call__(self, *args, **kwds):
67 return self.run(*args, **kwds)
69 def debug(self):
70 """Run the tests without collecting errors in a TestResult"""
71 for test in self:
72 test.debug()