blob: baf8414bf2cf22995a46b5d9fdc8c403edaa8e49 [file] [log] [blame]
Benjamin Petersonbed7d042009-07-19 21:01:52 +00001"""TestSuite"""
2
3from . import case
4
5
6class TestSuite(object):
7 """A test suite is a composite test consisting of a number of TestCases.
8
9 For use, create an instance of TestSuite, then add test case instances.
10 When all tests have been added, the suite can be passed to a test
11 runner, such as TextTestRunner. It will run the individual test cases
12 in the order in which they were added, aggregating the results. When
13 subclassing, do not forget to call the base class constructor.
14 """
15 def __init__(self, tests=()):
16 self._tests = []
17 self.addTests(tests)
18
19 def __repr__(self):
20 return "<%s tests=%s>" % (_strclass(self.__class__), list(self))
21
22 def __eq__(self, other):
23 if not isinstance(other, self.__class__):
24 return NotImplemented
25 return list(self) == list(other)
26
27 def __ne__(self, other):
28 return not self == other
29
30 def __iter__(self):
31 return iter(self._tests)
32
33 def countTestCases(self):
34 cases = 0
35 for test in self:
36 cases += test.countTestCases()
37 return cases
38
39 def addTest(self, test):
40 # sanity checks
41 if not hasattr(test, '__call__'):
42 raise TypeError("the test to add must be callable")
43 if isinstance(test, type) and issubclass(test,
44 (case.TestCase, TestSuite)):
45 raise TypeError("TestCases and TestSuites must be instantiated "
46 "before passing them to addTest()")
47 self._tests.append(test)
48
49 def addTests(self, tests):
50 if isinstance(tests, str):
51 raise TypeError("tests must be an iterable of tests, not a string")
52 for test in tests:
53 self.addTest(test)
54
55 def run(self, result):
56 for test in self:
57 if result.shouldStop:
58 break
59 test(result)
60 return result
61
62 def __call__(self, *args, **kwds):
63 return self.run(*args, **kwds)
64
65 def debug(self):
66 """Run the tests without collecting errors in a TestResult"""
67 for test in self:
68 test.debug()