| 
									
										
										
										
											2009-07-19 20:18:21 +00:00
										 |  |  | """TestSuite""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from . import case | 
					
						
							| 
									
										
										
										
											2009-09-13 19:07:03 +00:00
										 |  |  | from . import util | 
					
						
							| 
									
										
										
										
											2009-07-19 20:18:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TestSuite(object): | 
					
						
							|  |  |  |     """A test suite is a composite test consisting of a number of TestCases.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     For use, create an instance of TestSuite, then add test case instances. | 
					
						
							|  |  |  |     When all tests have been added, the suite can be passed to a test | 
					
						
							|  |  |  |     runner, such as TextTestRunner. It will run the individual test cases | 
					
						
							|  |  |  |     in the order in which they were added, aggregating the results. When | 
					
						
							|  |  |  |     subclassing, do not forget to call the base class constructor. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     def __init__(self, tests=()): | 
					
						
							|  |  |  |         self._tests = [] | 
					
						
							|  |  |  |         self.addTests(tests) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __repr__(self): | 
					
						
							| 
									
										
										
										
											2009-09-13 19:07:03 +00:00
										 |  |  |         return "<%s tests=%s>" % (util.strclass(self.__class__), list(self)) | 
					
						
							| 
									
										
										
										
											2009-07-19 20:18:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __eq__(self, other): | 
					
						
							|  |  |  |         if not isinstance(other, self.__class__): | 
					
						
							|  |  |  |             return NotImplemented | 
					
						
							|  |  |  |         return list(self) == list(other) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __ne__(self, other): | 
					
						
							|  |  |  |         return not self == other | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Can't guarantee hash invariant, so flag as unhashable | 
					
						
							|  |  |  |     __hash__ = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __iter__(self): | 
					
						
							|  |  |  |         return iter(self._tests) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def countTestCases(self): | 
					
						
							|  |  |  |         cases = 0 | 
					
						
							|  |  |  |         for test in self: | 
					
						
							|  |  |  |             cases += test.countTestCases() | 
					
						
							|  |  |  |         return cases | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def addTest(self, test): | 
					
						
							|  |  |  |         # sanity checks | 
					
						
							|  |  |  |         if not hasattr(test, '__call__'): | 
					
						
							| 
									
										
										
										
											2010-01-28 21:16:33 +00:00
										 |  |  |             raise TypeError("{} is not callable".format(repr(test))) | 
					
						
							| 
									
										
										
										
											2009-07-19 20:18:21 +00:00
										 |  |  |         if isinstance(test, type) and issubclass(test, | 
					
						
							|  |  |  |                                                  (case.TestCase, TestSuite)): | 
					
						
							|  |  |  |             raise TypeError("TestCases and TestSuites must be instantiated " | 
					
						
							|  |  |  |                             "before passing them to addTest()") | 
					
						
							|  |  |  |         self._tests.append(test) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def addTests(self, tests): | 
					
						
							|  |  |  |         if isinstance(tests, basestring): | 
					
						
							|  |  |  |             raise TypeError("tests must be an iterable of tests, not a string") | 
					
						
							|  |  |  |         for test in tests: | 
					
						
							|  |  |  |             self.addTest(test) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def run(self, result): | 
					
						
							|  |  |  |         for test in self: | 
					
						
							|  |  |  |             if result.shouldStop: | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             test(result) | 
					
						
							|  |  |  |         return result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __call__(self, *args, **kwds): | 
					
						
							|  |  |  |         return self.run(*args, **kwds) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def debug(self): | 
					
						
							|  |  |  |         """Run the tests without collecting errors in a TestResult""" | 
					
						
							|  |  |  |         for test in self: | 
					
						
							|  |  |  |             test.debug() |