| Jim Fulton | fafd874 | 2004-08-28 15:22:12 +0000 | [diff] [blame] | 1 | """Test script for unittest. | 
 | 2 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 3 | By Collin Winter <collinw at gmail.com> | 
 | 4 |  | 
 | 5 | Still need testing: | 
 | 6 |     TestCase.{assert,fail}* methods (some are tested implicitly) | 
| Jim Fulton | fafd874 | 2004-08-28 15:22:12 +0000 | [diff] [blame] | 7 | """ | 
 | 8 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 9 | from test import test_support | 
| Jim Fulton | fafd874 | 2004-08-28 15:22:12 +0000 | [diff] [blame] | 10 | import unittest | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 11 | from unittest import TestCase | 
| Jim Fulton | fafd874 | 2004-08-28 15:22:12 +0000 | [diff] [blame] | 12 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 13 | ### Support code | 
 | 14 | ################################################################ | 
| Jim Fulton | fafd874 | 2004-08-28 15:22:12 +0000 | [diff] [blame] | 15 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 16 | class LoggingResult(unittest.TestResult): | 
 | 17 |     def __init__(self, log): | 
 | 18 |         self._events = log | 
 | 19 |         super(LoggingResult, self).__init__() | 
 | 20 |  | 
 | 21 |     def startTest(self, test): | 
 | 22 |         self._events.append('startTest') | 
 | 23 |         super(LoggingResult, self).startTest(test) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 24 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 25 |     def stopTest(self, test): | 
 | 26 |         self._events.append('stopTest') | 
 | 27 |         super(LoggingResult, self).stopTest(test) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 28 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 29 |     def addFailure(self, *args): | 
 | 30 |         self._events.append('addFailure') | 
 | 31 |         super(LoggingResult, self).addFailure(*args) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 32 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 33 |     def addError(self, *args): | 
 | 34 |         self._events.append('addError') | 
 | 35 |         super(LoggingResult, self).addError(*args) | 
 | 36 |  | 
 | 37 | class TestEquality(object): | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 38 |     # Check for a valid __eq__ implementation | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 39 |     def test_eq(self): | 
 | 40 |         for obj_1, obj_2 in self.eq_pairs: | 
 | 41 |             self.assertEqual(obj_1, obj_2) | 
 | 42 |             self.assertEqual(obj_2, obj_1) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 43 |  | 
 | 44 |     # Check for a valid __ne__ implementation | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 45 |     def test_ne(self): | 
 | 46 |         for obj_1, obj_2 in self.ne_pairs: | 
 | 47 |             self.failIfEqual(obj_1, obj_2) | 
 | 48 |             self.failIfEqual(obj_2, obj_1) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 49 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 50 | class TestHashing(object): | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 51 |     # Check for a valid __hash__ implementation | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 52 |     def test_hash(self): | 
 | 53 |         for obj_1, obj_2 in self.eq_pairs: | 
 | 54 |             try: | 
 | 55 |                 assert hash(obj_1) == hash(obj_2) | 
 | 56 |             except KeyboardInterrupt: | 
 | 57 |                 raise | 
 | 58 |             except AssertionError: | 
 | 59 |                 self.fail("%s and %s do not hash equal" % (obj_1, obj_2)) | 
 | 60 |             except Exception, e: | 
 | 61 |                 self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 62 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 63 |         for obj_1, obj_2 in self.ne_pairs: | 
 | 64 |             try: | 
 | 65 |                 assert hash(obj_1) != hash(obj_2) | 
 | 66 |             except KeyboardInterrupt: | 
 | 67 |                 raise | 
 | 68 |             except AssertionError: | 
 | 69 |                 self.fail("%s and %s hash equal, but shouldn't" % (obj_1, obj_2)) | 
 | 70 |             except Exception, e: | 
 | 71 |                 self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 72 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 73 |  | 
 | 74 | ################################################################ | 
 | 75 | ### /Support code | 
 | 76 |  | 
 | 77 | class Test_TestLoader(TestCase): | 
 | 78 |  | 
 | 79 |     ### Tests for TestLoader.loadTestsFromTestCase | 
 | 80 |     ################################################################ | 
 | 81 |  | 
 | 82 |     # "Return a suite of all tests cases contained in the TestCase-derived | 
 | 83 |     # class testCaseClass" | 
 | 84 |     def test_loadTestsFromTestCase(self): | 
 | 85 |         class Foo(unittest.TestCase): | 
 | 86 |             def test_1(self): pass | 
 | 87 |             def test_2(self): pass | 
 | 88 |             def foo_bar(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 89 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 90 |         tests = unittest.TestSuite([Foo('test_1'), Foo('test_2')]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 91 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 92 |         loader = unittest.TestLoader() | 
 | 93 |         self.assertEqual(loader.loadTestsFromTestCase(Foo), tests) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 94 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 95 |     # "Return a suite of all tests cases contained in the TestCase-derived | 
 | 96 |     # class testCaseClass" | 
 | 97 |     # | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 98 |     # Make sure it does the right thing even if no tests were found | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 99 |     def test_loadTestsFromTestCase__no_matches(self): | 
 | 100 |         class Foo(unittest.TestCase): | 
 | 101 |             def foo_bar(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 102 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 103 |         empty_suite = unittest.TestSuite() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 104 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 105 |         loader = unittest.TestLoader() | 
 | 106 |         self.assertEqual(loader.loadTestsFromTestCase(Foo), empty_suite) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 107 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 108 |     # "Return a suite of all tests cases contained in the TestCase-derived | 
 | 109 |     # class testCaseClass" | 
 | 110 |     # | 
 | 111 |     # What happens if loadTestsFromTestCase() is given an object | 
 | 112 |     # that isn't a subclass of TestCase? Specifically, what happens | 
 | 113 |     # if testCaseClass is a subclass of TestSuite? | 
 | 114 |     # | 
 | 115 |     # This is checked for specifically in the code, so we better add a | 
 | 116 |     # test for it. | 
 | 117 |     def test_loadTestsFromTestCase__TestSuite_subclass(self): | 
 | 118 |         class NotATestCase(unittest.TestSuite): | 
 | 119 |             pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 120 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 121 |         loader = unittest.TestLoader() | 
 | 122 |         try: | 
 | 123 |             loader.loadTestsFromTestCase(NotATestCase) | 
 | 124 |         except TypeError: | 
 | 125 |             pass | 
 | 126 |         else: | 
 | 127 |             self.fail('Should raise TypeError') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 128 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 129 |     # "Return a suite of all tests cases contained in the TestCase-derived | 
 | 130 |     # class testCaseClass" | 
 | 131 |     # | 
 | 132 |     # Make sure loadTestsFromTestCase() picks up the default test method | 
 | 133 |     # name (as specified by TestCase), even though the method name does | 
 | 134 |     # not match the default TestLoader.testMethodPrefix string | 
 | 135 |     def test_loadTestsFromTestCase__default_method_name(self): | 
 | 136 |         class Foo(unittest.TestCase): | 
 | 137 |             def runTest(self): | 
 | 138 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 139 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 140 |         loader = unittest.TestLoader() | 
 | 141 |         # This has to be false for the test to succeed | 
 | 142 |         self.failIf('runTest'.startswith(loader.testMethodPrefix)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 143 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 144 |         suite = loader.loadTestsFromTestCase(Foo) | 
 | 145 |         self.failUnless(isinstance(suite, loader.suiteClass)) | 
 | 146 |         self.assertEqual(list(suite), [Foo('runTest')]) | 
 | 147 |  | 
 | 148 |     ################################################################ | 
 | 149 |     ### /Tests for TestLoader.loadTestsFromTestCase | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 150 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 151 |     ### Tests for TestLoader.loadTestsFromModule | 
 | 152 |     ################################################################ | 
 | 153 |  | 
 | 154 |     # "This method searches `module` for classes derived from TestCase" | 
 | 155 |     def test_loadTestsFromModule__TestCase_subclass(self): | 
 | 156 |         import new | 
 | 157 |         m = new.module('m') | 
 | 158 |         class MyTestCase(unittest.TestCase): | 
 | 159 |             def test(self): | 
 | 160 |                 pass | 
 | 161 |         m.testcase_1 = MyTestCase | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 162 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 163 |         loader = unittest.TestLoader() | 
 | 164 |         suite = loader.loadTestsFromModule(m) | 
 | 165 |         self.failUnless(isinstance(suite, loader.suiteClass)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 166 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 167 |         expected = [loader.suiteClass([MyTestCase('test')])] | 
 | 168 |         self.assertEqual(list(suite), expected) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 169 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 170 |     # "This method searches `module` for classes derived from TestCase" | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 171 |     # | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 172 |     # What happens if no tests are found (no TestCase instances)? | 
 | 173 |     def test_loadTestsFromModule__no_TestCase_instances(self): | 
 | 174 |         import new | 
 | 175 |         m = new.module('m') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 176 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 177 |         loader = unittest.TestLoader() | 
 | 178 |         suite = loader.loadTestsFromModule(m) | 
 | 179 |         self.failUnless(isinstance(suite, loader.suiteClass)) | 
 | 180 |         self.assertEqual(list(suite), []) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 181 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 182 |     # "This method searches `module` for classes derived from TestCase" | 
 | 183 |     # | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 184 |     # What happens if no tests are found (TestCases instances, but no tests)? | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 185 |     def test_loadTestsFromModule__no_TestCase_tests(self): | 
 | 186 |         import new | 
 | 187 |         m = new.module('m') | 
 | 188 |         class MyTestCase(unittest.TestCase): | 
 | 189 |             pass | 
 | 190 |         m.testcase_1 = MyTestCase | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 191 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 192 |         loader = unittest.TestLoader() | 
 | 193 |         suite = loader.loadTestsFromModule(m) | 
 | 194 |         self.failUnless(isinstance(suite, loader.suiteClass)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 195 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 196 |         self.assertEqual(list(suite), [loader.suiteClass()]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 197 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 198 |     # "This method searches `module` for classes derived from TestCase"s | 
 | 199 |     # | 
 | 200 |     # What happens if loadTestsFromModule() is given something other | 
 | 201 |     # than a module? | 
 | 202 |     # | 
 | 203 |     # XXX Currently, it succeeds anyway. This flexibility | 
 | 204 |     # should either be documented or loadTestsFromModule() should | 
 | 205 |     # raise a TypeError | 
 | 206 |     # | 
 | 207 |     # XXX Certain people are using this behaviour. We'll add a test for it | 
 | 208 |     def test_loadTestsFromModule__not_a_module(self): | 
 | 209 |         class MyTestCase(unittest.TestCase): | 
 | 210 |             def test(self): | 
 | 211 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 212 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 213 |         class NotAModule(object): | 
 | 214 |             test_2 = MyTestCase | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 215 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 216 |         loader = unittest.TestLoader() | 
 | 217 |         suite = loader.loadTestsFromModule(NotAModule) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 218 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 219 |         reference = [unittest.TestSuite([MyTestCase('test')])] | 
 | 220 |         self.assertEqual(list(suite), reference) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 221 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 222 |     ################################################################ | 
 | 223 |     ### /Tests for TestLoader.loadTestsFromModule() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 224 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 225 |     ### Tests for TestLoader.loadTestsFromName() | 
 | 226 |     ################################################################ | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 227 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 228 |     # "The specifier name is a ``dotted name'' that may resolve either to | 
 | 229 |     # a module, a test case class, a TestSuite instance, a test method | 
 | 230 |     # within a test case class, or a callable object which returns a | 
 | 231 |     # TestCase or TestSuite instance." | 
 | 232 |     # | 
 | 233 |     # Is ValueError raised in response to an empty name? | 
 | 234 |     def test_loadTestsFromName__empty_name(self): | 
 | 235 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 236 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 237 |         try: | 
 | 238 |             loader.loadTestsFromName('') | 
 | 239 |         except ValueError, e: | 
 | 240 |             self.assertEqual(str(e), "Empty module name") | 
 | 241 |         else: | 
 | 242 |             self.fail("TestLoader.loadTestsFromName failed to raise ValueError") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 243 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 244 |     # "The specifier name is a ``dotted name'' that may resolve either to | 
 | 245 |     # a module, a test case class, a TestSuite instance, a test method | 
 | 246 |     # within a test case class, or a callable object which returns a | 
 | 247 |     # TestCase or TestSuite instance." | 
 | 248 |     # | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 249 |     # What happens when the name contains invalid characters? | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 250 |     def test_loadTestsFromName__malformed_name(self): | 
 | 251 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 252 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 253 |         # XXX Should this raise ValueError or ImportError? | 
 | 254 |         try: | 
 | 255 |             loader.loadTestsFromName('abc () //') | 
 | 256 |         except ValueError: | 
 | 257 |             pass | 
 | 258 |         except ImportError: | 
 | 259 |             pass | 
 | 260 |         else: | 
 | 261 |             self.fail("TestLoader.loadTestsFromName failed to raise ValueError") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 262 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 263 |     # "The specifier name is a ``dotted name'' that may resolve ... to a | 
 | 264 |     # module" | 
 | 265 |     # | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 266 |     # What happens when a module by that name can't be found? | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 267 |     def test_loadTestsFromName__unknown_module_name(self): | 
 | 268 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 269 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 270 |         try: | 
 | 271 |             loader.loadTestsFromName('sdasfasfasdf') | 
 | 272 |         except ImportError, e: | 
 | 273 |             self.assertEqual(str(e), "No module named sdasfasfasdf") | 
 | 274 |         else: | 
 | 275 |             self.fail("TestLoader.loadTestsFromName failed to raise ImportError") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 276 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 277 |     # "The specifier name is a ``dotted name'' that may resolve either to | 
 | 278 |     # a module, a test case class, a TestSuite instance, a test method | 
 | 279 |     # within a test case class, or a callable object which returns a | 
 | 280 |     # TestCase or TestSuite instance." | 
 | 281 |     # | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 282 |     # What happens when the module is found, but the attribute can't? | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 283 |     def test_loadTestsFromName__unknown_attr_name(self): | 
 | 284 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 285 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 286 |         try: | 
 | 287 |             loader.loadTestsFromName('unittest.sdasfasfasdf') | 
 | 288 |         except AttributeError, e: | 
 | 289 |             self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") | 
 | 290 |         else: | 
 | 291 |             self.fail("TestLoader.loadTestsFromName failed to raise AttributeError") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 292 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 293 |     # "The specifier name is a ``dotted name'' that may resolve either to | 
 | 294 |     # a module, a test case class, a TestSuite instance, a test method | 
 | 295 |     # within a test case class, or a callable object which returns a | 
 | 296 |     # TestCase or TestSuite instance." | 
 | 297 |     # | 
 | 298 |     # What happens when we provide the module, but the attribute can't be | 
 | 299 |     # found? | 
 | 300 |     def test_loadTestsFromName__relative_unknown_name(self): | 
 | 301 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 302 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 303 |         try: | 
 | 304 |             loader.loadTestsFromName('sdasfasfasdf', unittest) | 
 | 305 |         except AttributeError, e: | 
 | 306 |             self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") | 
 | 307 |         else: | 
 | 308 |             self.fail("TestLoader.loadTestsFromName failed to raise AttributeError") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 309 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 310 |     # "The specifier name is a ``dotted name'' that may resolve either to | 
 | 311 |     # a module, a test case class, a TestSuite instance, a test method | 
 | 312 |     # within a test case class, or a callable object which returns a | 
 | 313 |     # TestCase or TestSuite instance." | 
 | 314 |     # ... | 
 | 315 |     # "The method optionally resolves name relative to the given module" | 
 | 316 |     # | 
 | 317 |     # Does loadTestsFromName raise ValueError when passed an empty | 
 | 318 |     # name relative to a provided module? | 
 | 319 |     # | 
 | 320 |     # XXX Should probably raise a ValueError instead of an AttributeError | 
 | 321 |     def test_loadTestsFromName__relative_empty_name(self): | 
 | 322 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 323 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 324 |         try: | 
 | 325 |             loader.loadTestsFromName('', unittest) | 
 | 326 |         except AttributeError, e: | 
 | 327 |             pass | 
 | 328 |         else: | 
 | 329 |             self.fail("Failed to raise AttributeError") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 330 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 331 |     # "The specifier name is a ``dotted name'' that may resolve either to | 
 | 332 |     # a module, a test case class, a TestSuite instance, a test method | 
 | 333 |     # within a test case class, or a callable object which returns a | 
 | 334 |     # TestCase or TestSuite instance." | 
 | 335 |     # ... | 
 | 336 |     # "The method optionally resolves name relative to the given module" | 
 | 337 |     # | 
 | 338 |     # What happens when an impossible name is given, relative to the provided | 
 | 339 |     # `module`? | 
 | 340 |     def test_loadTestsFromName__relative_malformed_name(self): | 
 | 341 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 342 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 343 |         # XXX Should this raise AttributeError or ValueError? | 
 | 344 |         try: | 
 | 345 |             loader.loadTestsFromName('abc () //', unittest) | 
 | 346 |         except ValueError: | 
 | 347 |             pass | 
 | 348 |         except AttributeError: | 
 | 349 |             pass | 
 | 350 |         else: | 
 | 351 |             self.fail("TestLoader.loadTestsFromName failed to raise ValueError") | 
 | 352 |  | 
 | 353 |     # "The method optionally resolves name relative to the given module" | 
 | 354 |     # | 
 | 355 |     # Does loadTestsFromName raise TypeError when the `module` argument | 
 | 356 |     # isn't a module object? | 
 | 357 |     # | 
 | 358 |     # XXX Accepts the not-a-module object, ignorning the object's type | 
 | 359 |     # This should raise an exception or the method name should be changed | 
 | 360 |     # | 
 | 361 |     # XXX Some people are relying on this, so keep it for now | 
 | 362 |     def test_loadTestsFromName__relative_not_a_module(self): | 
 | 363 |         class MyTestCase(unittest.TestCase): | 
 | 364 |             def test(self): | 
 | 365 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 366 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 367 |         class NotAModule(object): | 
 | 368 |             test_2 = MyTestCase | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 369 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 370 |         loader = unittest.TestLoader() | 
 | 371 |         suite = loader.loadTestsFromName('test_2', NotAModule) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 372 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 373 |         reference = [MyTestCase('test')] | 
 | 374 |         self.assertEqual(list(suite), reference) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 375 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 376 |     # "The specifier name is a ``dotted name'' that may resolve either to | 
 | 377 |     # a module, a test case class, a TestSuite instance, a test method | 
 | 378 |     # within a test case class, or a callable object which returns a | 
 | 379 |     # TestCase or TestSuite instance." | 
 | 380 |     # | 
 | 381 |     # Does it raise an exception if the name resolves to an invalid | 
 | 382 |     # object? | 
 | 383 |     def test_loadTestsFromName__relative_bad_object(self): | 
 | 384 |         import new | 
 | 385 |         m = new.module('m') | 
 | 386 |         m.testcase_1 = object() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 387 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 388 |         loader = unittest.TestLoader() | 
 | 389 |         try: | 
 | 390 |             loader.loadTestsFromName('testcase_1', m) | 
 | 391 |         except TypeError: | 
 | 392 |             pass | 
 | 393 |         else: | 
 | 394 |             self.fail("Should have raised TypeError") | 
 | 395 |  | 
 | 396 |     # "The specifier name is a ``dotted name'' that may | 
 | 397 |     # resolve either to ... a test case class" | 
 | 398 |     def test_loadTestsFromName__relative_TestCase_subclass(self): | 
 | 399 |         import new | 
 | 400 |         m = new.module('m') | 
 | 401 |         class MyTestCase(unittest.TestCase): | 
 | 402 |             def test(self): | 
 | 403 |                 pass | 
 | 404 |         m.testcase_1 = MyTestCase | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 405 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 406 |         loader = unittest.TestLoader() | 
 | 407 |         suite = loader.loadTestsFromName('testcase_1', m) | 
 | 408 |         self.failUnless(isinstance(suite, loader.suiteClass)) | 
 | 409 |         self.assertEqual(list(suite), [MyTestCase('test')]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 410 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 411 |     # "The specifier name is a ``dotted name'' that may resolve either to | 
 | 412 |     # a module, a test case class, a TestSuite instance, a test method | 
 | 413 |     # within a test case class, or a callable object which returns a | 
 | 414 |     # TestCase or TestSuite instance." | 
 | 415 |     def test_loadTestsFromName__relative_TestSuite(self): | 
 | 416 |         import new | 
 | 417 |         m = new.module('m') | 
 | 418 |         class MyTestCase(unittest.TestCase): | 
 | 419 |             def test(self): | 
 | 420 |                 pass | 
 | 421 |         m.testsuite = unittest.TestSuite([MyTestCase('test')]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 422 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 423 |         loader = unittest.TestLoader() | 
 | 424 |         suite = loader.loadTestsFromName('testsuite', m) | 
 | 425 |         self.failUnless(isinstance(suite, loader.suiteClass)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 426 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 427 |         self.assertEqual(list(suite), [MyTestCase('test')]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 428 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 429 |     # "The specifier name is a ``dotted name'' that may resolve ... to | 
 | 430 |     # ... a test method within a test case class" | 
 | 431 |     def test_loadTestsFromName__relative_testmethod(self): | 
 | 432 |         import new | 
 | 433 |         m = new.module('m') | 
 | 434 |         class MyTestCase(unittest.TestCase): | 
 | 435 |             def test(self): | 
 | 436 |                 pass | 
 | 437 |         m.testcase_1 = MyTestCase | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 438 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 439 |         loader = unittest.TestLoader() | 
 | 440 |         suite = loader.loadTestsFromName('testcase_1.test', m) | 
 | 441 |         self.failUnless(isinstance(suite, loader.suiteClass)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 442 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 443 |         self.assertEqual(list(suite), [MyTestCase('test')]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 444 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 445 |     # "The specifier name is a ``dotted name'' that may resolve either to | 
 | 446 |     # a module, a test case class, a TestSuite instance, a test method | 
 | 447 |     # within a test case class, or a callable object which returns a | 
 | 448 |     # TestCase or TestSuite instance." | 
 | 449 |     # | 
 | 450 |     # Does loadTestsFromName() raise the proper exception when trying to | 
 | 451 |     # resolve "a test method within a test case class" that doesn't exist | 
 | 452 |     # for the given name (relative to a provided module)? | 
 | 453 |     def test_loadTestsFromName__relative_invalid_testmethod(self): | 
 | 454 |         import new | 
 | 455 |         m = new.module('m') | 
 | 456 |         class MyTestCase(unittest.TestCase): | 
 | 457 |             def test(self): | 
 | 458 |                 pass | 
 | 459 |         m.testcase_1 = MyTestCase | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 460 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 461 |         loader = unittest.TestLoader() | 
 | 462 |         try: | 
 | 463 |             loader.loadTestsFromName('testcase_1.testfoo', m) | 
 | 464 |         except AttributeError, e: | 
 | 465 |             self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'") | 
 | 466 |         else: | 
 | 467 |             self.fail("Failed to raise AttributeError") | 
 | 468 |  | 
 | 469 |     # "The specifier name is a ``dotted name'' that may resolve ... to | 
 | 470 |     # ... a callable object which returns a ... TestSuite instance" | 
 | 471 |     def test_loadTestsFromName__callable__TestSuite(self): | 
 | 472 |         import new | 
 | 473 |         m = new.module('m') | 
 | 474 |         testcase_1 = unittest.FunctionTestCase(lambda: None) | 
 | 475 |         testcase_2 = unittest.FunctionTestCase(lambda: None) | 
 | 476 |         def return_TestSuite(): | 
 | 477 |             return unittest.TestSuite([testcase_1, testcase_2]) | 
 | 478 |         m.return_TestSuite = return_TestSuite | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 479 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 480 |         loader = unittest.TestLoader() | 
 | 481 |         suite = loader.loadTestsFromName('return_TestSuite', m) | 
 | 482 |         self.failUnless(isinstance(suite, loader.suiteClass)) | 
 | 483 |         self.assertEqual(list(suite), [testcase_1, testcase_2]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 484 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 485 |     # "The specifier name is a ``dotted name'' that may resolve ... to | 
 | 486 |     # ... a callable object which returns a TestCase ... instance" | 
 | 487 |     def test_loadTestsFromName__callable__TestCase_instance(self): | 
 | 488 |         import new | 
 | 489 |         m = new.module('m') | 
 | 490 |         testcase_1 = unittest.FunctionTestCase(lambda: None) | 
 | 491 |         def return_TestCase(): | 
 | 492 |             return testcase_1 | 
 | 493 |         m.return_TestCase = return_TestCase | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 494 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 495 |         loader = unittest.TestLoader() | 
 | 496 |         suite = loader.loadTestsFromName('return_TestCase', m) | 
 | 497 |         self.failUnless(isinstance(suite, loader.suiteClass)) | 
 | 498 |         self.assertEqual(list(suite), [testcase_1]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 499 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 500 |     # "The specifier name is a ``dotted name'' that may resolve ... to | 
 | 501 |     # ... a callable object which returns a TestCase or TestSuite instance" | 
 | 502 |     # | 
 | 503 |     # What happens if the callable returns something else? | 
 | 504 |     def test_loadTestsFromName__callable__wrong_type(self): | 
 | 505 |         import new | 
 | 506 |         m = new.module('m') | 
 | 507 |         def return_wrong(): | 
 | 508 |             return 6 | 
 | 509 |         m.return_wrong = return_wrong | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 510 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 511 |         loader = unittest.TestLoader() | 
 | 512 |         try: | 
 | 513 |             suite = loader.loadTestsFromName('return_wrong', m) | 
 | 514 |         except TypeError: | 
 | 515 |             pass | 
 | 516 |         else: | 
 | 517 |             self.fail("TestLoader.loadTestsFromName failed to raise TypeError") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 518 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 519 |     # "The specifier can refer to modules and packages which have not been | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 520 |     # imported; they will be imported as a side-effect" | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 521 |     def test_loadTestsFromName__module_not_loaded(self): | 
 | 522 |         # We're going to try to load this module as a side-effect, so it | 
 | 523 |         # better not be loaded before we try. | 
 | 524 |         # | 
 | 525 |         # Why pick audioop? Google shows it isn't used very often, so there's | 
 | 526 |         # a good chance that it won't be imported when this test is run | 
 | 527 |         module_name = 'audioop' | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 528 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 529 |         import sys | 
 | 530 |         if module_name in sys.modules: | 
 | 531 |             del sys.modules[module_name] | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 532 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 533 |         loader = unittest.TestLoader() | 
 | 534 |         try: | 
 | 535 |             suite = loader.loadTestsFromName(module_name) | 
 | 536 |  | 
 | 537 |             self.failUnless(isinstance(suite, loader.suiteClass)) | 
 | 538 |             self.assertEqual(list(suite), []) | 
 | 539 |  | 
 | 540 |             # audioop should now be loaded, thanks to loadTestsFromName() | 
 | 541 |             self.failUnless(module_name in sys.modules) | 
 | 542 |         finally: | 
| Kristján Valur Jónsson | 170eee9 | 2007-05-03 20:09:56 +0000 | [diff] [blame] | 543 |             if module_name in sys.modules: | 
 | 544 |                 del sys.modules[module_name] | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 545 |  | 
 | 546 |     ################################################################ | 
 | 547 |     ### Tests for TestLoader.loadTestsFromName() | 
 | 548 |  | 
 | 549 |     ### Tests for TestLoader.loadTestsFromNames() | 
 | 550 |     ################################################################ | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 551 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 552 |     # "Similar to loadTestsFromName(), but takes a sequence of names rather | 
 | 553 |     # than a single name." | 
 | 554 |     # | 
 | 555 |     # What happens if that sequence of names is empty? | 
 | 556 |     def test_loadTestsFromNames__empty_name_list(self): | 
 | 557 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 558 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 559 |         suite = loader.loadTestsFromNames([]) | 
 | 560 |         self.failUnless(isinstance(suite, loader.suiteClass)) | 
 | 561 |         self.assertEqual(list(suite), []) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 562 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 563 |     # "Similar to loadTestsFromName(), but takes a sequence of names rather | 
 | 564 |     # than a single name." | 
 | 565 |     # ... | 
 | 566 |     # "The method optionally resolves name relative to the given module" | 
 | 567 |     # | 
 | 568 |     # What happens if that sequence of names is empty? | 
 | 569 |     # | 
 | 570 |     # XXX Should this raise a ValueError or just return an empty TestSuite? | 
 | 571 |     def test_loadTestsFromNames__relative_empty_name_list(self): | 
 | 572 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 573 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 574 |         suite = loader.loadTestsFromNames([], unittest) | 
 | 575 |         self.failUnless(isinstance(suite, loader.suiteClass)) | 
 | 576 |         self.assertEqual(list(suite), []) | 
 | 577 |  | 
 | 578 |     # "The specifier name is a ``dotted name'' that may resolve either to | 
 | 579 |     # a module, a test case class, a TestSuite instance, a test method | 
 | 580 |     # within a test case class, or a callable object which returns a | 
 | 581 |     # TestCase or TestSuite instance." | 
 | 582 |     # | 
 | 583 |     # Is ValueError raised in response to an empty name? | 
 | 584 |     def test_loadTestsFromNames__empty_name(self): | 
 | 585 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 586 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 587 |         try: | 
 | 588 |             loader.loadTestsFromNames(['']) | 
 | 589 |         except ValueError, e: | 
 | 590 |             self.assertEqual(str(e), "Empty module name") | 
 | 591 |         else: | 
 | 592 |             self.fail("TestLoader.loadTestsFromNames failed to raise ValueError") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 593 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 594 |     # "The specifier name is a ``dotted name'' that may resolve either to | 
 | 595 |     # a module, a test case class, a TestSuite instance, a test method | 
 | 596 |     # within a test case class, or a callable object which returns a | 
 | 597 |     # TestCase or TestSuite instance." | 
 | 598 |     # | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 599 |     # What happens when presented with an impossible module name? | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 600 |     def test_loadTestsFromNames__malformed_name(self): | 
 | 601 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 602 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 603 |         # XXX Should this raise ValueError or ImportError? | 
 | 604 |         try: | 
 | 605 |             loader.loadTestsFromNames(['abc () //']) | 
 | 606 |         except ValueError: | 
 | 607 |             pass | 
 | 608 |         except ImportError: | 
 | 609 |             pass | 
 | 610 |         else: | 
 | 611 |             self.fail("TestLoader.loadTestsFromNames failed to raise ValueError") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 612 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 613 |     # "The specifier name is a ``dotted name'' that may resolve either to | 
 | 614 |     # a module, a test case class, a TestSuite instance, a test method | 
 | 615 |     # within a test case class, or a callable object which returns a | 
 | 616 |     # TestCase or TestSuite instance." | 
 | 617 |     # | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 618 |     # What happens when no module can be found for the given name? | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 619 |     def test_loadTestsFromNames__unknown_module_name(self): | 
 | 620 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 621 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 622 |         try: | 
 | 623 |             loader.loadTestsFromNames(['sdasfasfasdf']) | 
 | 624 |         except ImportError, e: | 
 | 625 |             self.assertEqual(str(e), "No module named sdasfasfasdf") | 
 | 626 |         else: | 
 | 627 |             self.fail("TestLoader.loadTestsFromNames failed to raise ImportError") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 628 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 629 |     # "The specifier name is a ``dotted name'' that may resolve either to | 
 | 630 |     # a module, a test case class, a TestSuite instance, a test method | 
 | 631 |     # within a test case class, or a callable object which returns a | 
 | 632 |     # TestCase or TestSuite instance." | 
 | 633 |     # | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 634 |     # What happens when the module can be found, but not the attribute? | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 635 |     def test_loadTestsFromNames__unknown_attr_name(self): | 
 | 636 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 637 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 638 |         try: | 
 | 639 |             loader.loadTestsFromNames(['unittest.sdasfasfasdf', 'unittest']) | 
 | 640 |         except AttributeError, e: | 
 | 641 |             self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") | 
 | 642 |         else: | 
 | 643 |             self.fail("TestLoader.loadTestsFromNames failed to raise AttributeError") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 644 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 645 |     # "The specifier name is a ``dotted name'' that may resolve either to | 
 | 646 |     # a module, a test case class, a TestSuite instance, a test method | 
 | 647 |     # within a test case class, or a callable object which returns a | 
 | 648 |     # TestCase or TestSuite instance." | 
 | 649 |     # ... | 
 | 650 |     # "The method optionally resolves name relative to the given module" | 
 | 651 |     # | 
 | 652 |     # What happens when given an unknown attribute on a specified `module` | 
 | 653 |     # argument? | 
 | 654 |     def test_loadTestsFromNames__unknown_name_relative_1(self): | 
 | 655 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 656 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 657 |         try: | 
 | 658 |             loader.loadTestsFromNames(['sdasfasfasdf'], unittest) | 
 | 659 |         except AttributeError, e: | 
 | 660 |             self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") | 
 | 661 |         else: | 
 | 662 |             self.fail("TestLoader.loadTestsFromName failed to raise AttributeError") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 663 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 664 |     # "The specifier name is a ``dotted name'' that may resolve either to | 
 | 665 |     # a module, a test case class, a TestSuite instance, a test method | 
 | 666 |     # within a test case class, or a callable object which returns a | 
 | 667 |     # TestCase or TestSuite instance." | 
 | 668 |     # ... | 
 | 669 |     # "The method optionally resolves name relative to the given module" | 
 | 670 |     # | 
 | 671 |     # Do unknown attributes (relative to a provided module) still raise an | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 672 |     # exception even in the presence of valid attribute names? | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 673 |     def test_loadTestsFromNames__unknown_name_relative_2(self): | 
 | 674 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 675 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 676 |         try: | 
 | 677 |             loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest) | 
 | 678 |         except AttributeError, e: | 
 | 679 |             self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") | 
 | 680 |         else: | 
 | 681 |             self.fail("TestLoader.loadTestsFromName failed to raise AttributeError") | 
 | 682 |  | 
 | 683 |     # "The specifier name is a ``dotted name'' that may resolve either to | 
 | 684 |     # a module, a test case class, a TestSuite instance, a test method | 
 | 685 |     # within a test case class, or a callable object which returns a | 
 | 686 |     # TestCase or TestSuite instance." | 
 | 687 |     # ... | 
 | 688 |     # "The method optionally resolves name relative to the given module" | 
 | 689 |     # | 
 | 690 |     # What happens when faced with the empty string? | 
 | 691 |     # | 
 | 692 |     # XXX This currently raises AttributeError, though ValueError is probably | 
 | 693 |     # more appropriate | 
 | 694 |     def test_loadTestsFromNames__relative_empty_name(self): | 
 | 695 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 696 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 697 |         try: | 
 | 698 |             loader.loadTestsFromNames([''], unittest) | 
 | 699 |         except AttributeError: | 
 | 700 |             pass | 
 | 701 |         else: | 
 | 702 |             self.fail("Failed to raise ValueError") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 703 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 704 |     # "The specifier name is a ``dotted name'' that may resolve either to | 
 | 705 |     # a module, a test case class, a TestSuite instance, a test method | 
 | 706 |     # within a test case class, or a callable object which returns a | 
 | 707 |     # TestCase or TestSuite instance." | 
 | 708 |     # ... | 
 | 709 |     # "The method optionally resolves name relative to the given module" | 
 | 710 |     # | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 711 |     # What happens when presented with an impossible attribute name? | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 712 |     def test_loadTestsFromNames__relative_malformed_name(self): | 
 | 713 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 714 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 715 |         # XXX Should this raise AttributeError or ValueError? | 
 | 716 |         try: | 
 | 717 |             loader.loadTestsFromNames(['abc () //'], unittest) | 
 | 718 |         except AttributeError: | 
 | 719 |             pass | 
 | 720 |         except ValueError: | 
 | 721 |             pass | 
 | 722 |         else: | 
 | 723 |             self.fail("TestLoader.loadTestsFromNames failed to raise ValueError") | 
 | 724 |  | 
 | 725 |     # "The method optionally resolves name relative to the given module" | 
 | 726 |     # | 
 | 727 |     # Does loadTestsFromNames() make sure the provided `module` is in fact | 
 | 728 |     # a module? | 
 | 729 |     # | 
 | 730 |     # XXX This validation is currently not done. This flexibility should | 
 | 731 |     # either be documented or a TypeError should be raised. | 
 | 732 |     def test_loadTestsFromNames__relative_not_a_module(self): | 
 | 733 |         class MyTestCase(unittest.TestCase): | 
 | 734 |             def test(self): | 
 | 735 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 736 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 737 |         class NotAModule(object): | 
 | 738 |             test_2 = MyTestCase | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 739 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 740 |         loader = unittest.TestLoader() | 
 | 741 |         suite = loader.loadTestsFromNames(['test_2'], NotAModule) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 742 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 743 |         reference = [unittest.TestSuite([MyTestCase('test')])] | 
 | 744 |         self.assertEqual(list(suite), reference) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 745 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 746 |     # "The specifier name is a ``dotted name'' that may resolve either to | 
 | 747 |     # a module, a test case class, a TestSuite instance, a test method | 
 | 748 |     # within a test case class, or a callable object which returns a | 
 | 749 |     # TestCase or TestSuite instance." | 
 | 750 |     # | 
 | 751 |     # Does it raise an exception if the name resolves to an invalid | 
 | 752 |     # object? | 
 | 753 |     def test_loadTestsFromNames__relative_bad_object(self): | 
 | 754 |         import new | 
 | 755 |         m = new.module('m') | 
 | 756 |         m.testcase_1 = object() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 757 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 758 |         loader = unittest.TestLoader() | 
 | 759 |         try: | 
 | 760 |             loader.loadTestsFromNames(['testcase_1'], m) | 
 | 761 |         except TypeError: | 
 | 762 |             pass | 
 | 763 |         else: | 
 | 764 |             self.fail("Should have raised TypeError") | 
 | 765 |  | 
 | 766 |     # "The specifier name is a ``dotted name'' that may resolve ... to | 
 | 767 |     # ... a test case class" | 
 | 768 |     def test_loadTestsFromNames__relative_TestCase_subclass(self): | 
 | 769 |         import new | 
 | 770 |         m = new.module('m') | 
 | 771 |         class MyTestCase(unittest.TestCase): | 
 | 772 |             def test(self): | 
 | 773 |                 pass | 
 | 774 |         m.testcase_1 = MyTestCase | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 775 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 776 |         loader = unittest.TestLoader() | 
 | 777 |         suite = loader.loadTestsFromNames(['testcase_1'], m) | 
 | 778 |         self.failUnless(isinstance(suite, loader.suiteClass)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 779 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 780 |         expected = loader.suiteClass([MyTestCase('test')]) | 
 | 781 |         self.assertEqual(list(suite), [expected]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 782 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 783 |     # "The specifier name is a ``dotted name'' that may resolve ... to | 
 | 784 |     # ... a TestSuite instance" | 
 | 785 |     def test_loadTestsFromNames__relative_TestSuite(self): | 
 | 786 |         import new | 
 | 787 |         m = new.module('m') | 
 | 788 |         class MyTestCase(unittest.TestCase): | 
 | 789 |             def test(self): | 
 | 790 |                 pass | 
 | 791 |         m.testsuite = unittest.TestSuite([MyTestCase('test')]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 792 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 793 |         loader = unittest.TestLoader() | 
 | 794 |         suite = loader.loadTestsFromNames(['testsuite'], m) | 
 | 795 |         self.failUnless(isinstance(suite, loader.suiteClass)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 796 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 797 |         self.assertEqual(list(suite), [m.testsuite]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 798 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 799 |     # "The specifier name is a ``dotted name'' that may resolve ... to ... a | 
 | 800 |     # test method within a test case class" | 
 | 801 |     def test_loadTestsFromNames__relative_testmethod(self): | 
 | 802 |         import new | 
 | 803 |         m = new.module('m') | 
 | 804 |         class MyTestCase(unittest.TestCase): | 
 | 805 |             def test(self): | 
 | 806 |                 pass | 
 | 807 |         m.testcase_1 = MyTestCase | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 808 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 809 |         loader = unittest.TestLoader() | 
 | 810 |         suite = loader.loadTestsFromNames(['testcase_1.test'], m) | 
 | 811 |         self.failUnless(isinstance(suite, loader.suiteClass)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 812 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 813 |         ref_suite = unittest.TestSuite([MyTestCase('test')]) | 
 | 814 |         self.assertEqual(list(suite), [ref_suite]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 815 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 816 |     # "The specifier name is a ``dotted name'' that may resolve ... to ... a | 
 | 817 |     # test method within a test case class" | 
 | 818 |     # | 
 | 819 |     # Does the method gracefully handle names that initially look like they | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 820 |     # resolve to "a test method within a test case class" but don't? | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 821 |     def test_loadTestsFromNames__relative_invalid_testmethod(self): | 
 | 822 |         import new | 
 | 823 |         m = new.module('m') | 
 | 824 |         class MyTestCase(unittest.TestCase): | 
 | 825 |             def test(self): | 
 | 826 |                 pass | 
 | 827 |         m.testcase_1 = MyTestCase | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 828 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 829 |         loader = unittest.TestLoader() | 
 | 830 |         try: | 
 | 831 |             loader.loadTestsFromNames(['testcase_1.testfoo'], m) | 
 | 832 |         except AttributeError, e: | 
 | 833 |             self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'") | 
 | 834 |         else: | 
 | 835 |             self.fail("Failed to raise AttributeError") | 
 | 836 |  | 
 | 837 |     # "The specifier name is a ``dotted name'' that may resolve ... to | 
 | 838 |     # ... a callable object which returns a ... TestSuite instance" | 
 | 839 |     def test_loadTestsFromNames__callable__TestSuite(self): | 
 | 840 |         import new | 
 | 841 |         m = new.module('m') | 
 | 842 |         testcase_1 = unittest.FunctionTestCase(lambda: None) | 
 | 843 |         testcase_2 = unittest.FunctionTestCase(lambda: None) | 
 | 844 |         def return_TestSuite(): | 
 | 845 |             return unittest.TestSuite([testcase_1, testcase_2]) | 
 | 846 |         m.return_TestSuite = return_TestSuite | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 847 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 848 |         loader = unittest.TestLoader() | 
 | 849 |         suite = loader.loadTestsFromNames(['return_TestSuite'], m) | 
 | 850 |         self.failUnless(isinstance(suite, loader.suiteClass)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 851 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 852 |         expected = unittest.TestSuite([testcase_1, testcase_2]) | 
 | 853 |         self.assertEqual(list(suite), [expected]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 854 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 855 |     # "The specifier name is a ``dotted name'' that may resolve ... to | 
 | 856 |     # ... a callable object which returns a TestCase ... instance" | 
 | 857 |     def test_loadTestsFromNames__callable__TestCase_instance(self): | 
 | 858 |         import new | 
 | 859 |         m = new.module('m') | 
 | 860 |         testcase_1 = unittest.FunctionTestCase(lambda: None) | 
 | 861 |         def return_TestCase(): | 
 | 862 |             return testcase_1 | 
 | 863 |         m.return_TestCase = return_TestCase | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 864 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 865 |         loader = unittest.TestLoader() | 
 | 866 |         suite = loader.loadTestsFromNames(['return_TestCase'], m) | 
 | 867 |         self.failUnless(isinstance(suite, loader.suiteClass)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 868 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 869 |         ref_suite = unittest.TestSuite([testcase_1]) | 
 | 870 |         self.assertEqual(list(suite), [ref_suite]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 871 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 872 |     # "The specifier name is a ``dotted name'' that may resolve ... to | 
 | 873 |     # ... a callable object which returns a TestCase or TestSuite instance" | 
 | 874 |     # | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 875 |     # Are staticmethods handled correctly? | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 876 |     def test_loadTestsFromNames__callable__call_staticmethod(self): | 
 | 877 |         import new | 
 | 878 |         m = new.module('m') | 
 | 879 |         class Test1(unittest.TestCase): | 
 | 880 |             def test(self): | 
 | 881 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 882 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 883 |         testcase_1 = Test1('test') | 
 | 884 |         class Foo(unittest.TestCase): | 
 | 885 |             @staticmethod | 
 | 886 |             def foo(): | 
 | 887 |                 return testcase_1 | 
 | 888 |         m.Foo = Foo | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 889 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 890 |         loader = unittest.TestLoader() | 
 | 891 |         suite = loader.loadTestsFromNames(['Foo.foo'], m) | 
 | 892 |         self.failUnless(isinstance(suite, loader.suiteClass)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 893 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 894 |         ref_suite = unittest.TestSuite([testcase_1]) | 
 | 895 |         self.assertEqual(list(suite), [ref_suite]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 896 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 897 |     # "The specifier name is a ``dotted name'' that may resolve ... to | 
 | 898 |     # ... a callable object which returns a TestCase or TestSuite instance" | 
 | 899 |     # | 
 | 900 |     # What happens when the callable returns something else? | 
 | 901 |     def test_loadTestsFromNames__callable__wrong_type(self): | 
 | 902 |         import new | 
 | 903 |         m = new.module('m') | 
 | 904 |         def return_wrong(): | 
 | 905 |             return 6 | 
 | 906 |         m.return_wrong = return_wrong | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 907 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 908 |         loader = unittest.TestLoader() | 
 | 909 |         try: | 
 | 910 |             suite = loader.loadTestsFromNames(['return_wrong'], m) | 
 | 911 |         except TypeError: | 
 | 912 |             pass | 
 | 913 |         else: | 
 | 914 |             self.fail("TestLoader.loadTestsFromNames failed to raise TypeError") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 915 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 916 |     # "The specifier can refer to modules and packages which have not been | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 917 |     # imported; they will be imported as a side-effect" | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 918 |     def test_loadTestsFromNames__module_not_loaded(self): | 
 | 919 |         # We're going to try to load this module as a side-effect, so it | 
 | 920 |         # better not be loaded before we try. | 
 | 921 |         # | 
 | 922 |         # Why pick audioop? Google shows it isn't used very often, so there's | 
 | 923 |         # a good chance that it won't be imported when this test is run | 
 | 924 |         module_name = 'audioop' | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 925 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 926 |         import sys | 
 | 927 |         if module_name in sys.modules: | 
 | 928 |             del sys.modules[module_name] | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 929 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 930 |         loader = unittest.TestLoader() | 
 | 931 |         try: | 
 | 932 |             suite = loader.loadTestsFromNames([module_name]) | 
 | 933 |  | 
 | 934 |             self.failUnless(isinstance(suite, loader.suiteClass)) | 
 | 935 |             self.assertEqual(list(suite), [unittest.TestSuite()]) | 
 | 936 |  | 
 | 937 |             # audioop should now be loaded, thanks to loadTestsFromName() | 
 | 938 |             self.failUnless(module_name in sys.modules) | 
 | 939 |         finally: | 
| Kristján Valur Jónsson | 170eee9 | 2007-05-03 20:09:56 +0000 | [diff] [blame] | 940 |             if module_name in sys.modules: | 
 | 941 |                 del sys.modules[module_name] | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 942 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 943 |     ################################################################ | 
 | 944 |     ### /Tests for TestLoader.loadTestsFromNames() | 
 | 945 |  | 
 | 946 |     ### Tests for TestLoader.getTestCaseNames() | 
 | 947 |     ################################################################ | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 948 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 949 |     # "Return a sorted sequence of method names found within testCaseClass" | 
 | 950 |     # | 
 | 951 |     # Test.foobar is defined to make sure getTestCaseNames() respects | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 952 |     # loader.testMethodPrefix | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 953 |     def test_getTestCaseNames(self): | 
 | 954 |         class Test(unittest.TestCase): | 
 | 955 |             def test_1(self): pass | 
 | 956 |             def test_2(self): pass | 
 | 957 |             def foobar(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 958 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 959 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 960 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 961 |         self.assertEqual(loader.getTestCaseNames(Test), ['test_1', 'test_2']) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 962 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 963 |     # "Return a sorted sequence of method names found within testCaseClass" | 
 | 964 |     # | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 965 |     # Does getTestCaseNames() behave appropriately if no tests are found? | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 966 |     def test_getTestCaseNames__no_tests(self): | 
 | 967 |         class Test(unittest.TestCase): | 
 | 968 |             def foobar(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 969 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 970 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 971 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 972 |         self.assertEqual(loader.getTestCaseNames(Test), []) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 973 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 974 |     # "Return a sorted sequence of method names found within testCaseClass" | 
 | 975 |     # | 
 | 976 |     # Are not-TestCases handled gracefully? | 
 | 977 |     # | 
 | 978 |     # XXX This should raise a TypeError, not return a list | 
 | 979 |     # | 
 | 980 |     # XXX It's too late in the 2.5 release cycle to fix this, but it should | 
 | 981 |     # probably be revisited for 2.6 | 
 | 982 |     def test_getTestCaseNames__not_a_TestCase(self): | 
 | 983 |         class BadCase(int): | 
 | 984 |             def test_foo(self): | 
 | 985 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 986 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 987 |         loader = unittest.TestLoader() | 
 | 988 |         names = loader.getTestCaseNames(BadCase) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 989 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 990 |         self.assertEqual(names, ['test_foo']) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 991 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 992 |     # "Return a sorted sequence of method names found within testCaseClass" | 
 | 993 |     # | 
 | 994 |     # Make sure inherited names are handled. | 
 | 995 |     # | 
 | 996 |     # TestP.foobar is defined to make sure getTestCaseNames() respects | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 997 |     # loader.testMethodPrefix | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 998 |     def test_getTestCaseNames__inheritance(self): | 
 | 999 |         class TestP(unittest.TestCase): | 
 | 1000 |             def test_1(self): pass | 
 | 1001 |             def test_2(self): pass | 
 | 1002 |             def foobar(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1003 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1004 |         class TestC(TestP): | 
 | 1005 |             def test_1(self): pass | 
 | 1006 |             def test_3(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1007 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1008 |         loader = unittest.TestLoader() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1009 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1010 |         names = ['test_1', 'test_2', 'test_3'] | 
 | 1011 |         self.assertEqual(loader.getTestCaseNames(TestC), names) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1012 |  | 
 | 1013 |     ################################################################ | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1014 |     ### /Tests for TestLoader.getTestCaseNames() | 
 | 1015 |  | 
 | 1016 |     ### Tests for TestLoader.testMethodPrefix | 
 | 1017 |     ################################################################ | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1018 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1019 |     # "String giving the prefix of method names which will be interpreted as | 
 | 1020 |     # test methods" | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1021 |     # | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1022 |     # Implicit in the documentation is that testMethodPrefix is respected by | 
 | 1023 |     # all loadTestsFrom* methods. | 
 | 1024 |     def test_testMethodPrefix__loadTestsFromTestCase(self): | 
 | 1025 |         class Foo(unittest.TestCase): | 
 | 1026 |             def test_1(self): pass | 
 | 1027 |             def test_2(self): pass | 
 | 1028 |             def foo_bar(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1029 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1030 |         tests_1 = unittest.TestSuite([Foo('foo_bar')]) | 
 | 1031 |         tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1032 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1033 |         loader = unittest.TestLoader() | 
 | 1034 |         loader.testMethodPrefix = 'foo' | 
 | 1035 |         self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_1) | 
 | 1036 |  | 
 | 1037 |         loader.testMethodPrefix = 'test' | 
 | 1038 |         self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_2) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1039 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1040 |     # "String giving the prefix of method names which will be interpreted as | 
 | 1041 |     # test methods" | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1042 |     # | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1043 |     # Implicit in the documentation is that testMethodPrefix is respected by | 
 | 1044 |     # all loadTestsFrom* methods. | 
 | 1045 |     def test_testMethodPrefix__loadTestsFromModule(self): | 
 | 1046 |         import new | 
 | 1047 |         m = new.module('m') | 
 | 1048 |         class Foo(unittest.TestCase): | 
 | 1049 |             def test_1(self): pass | 
 | 1050 |             def test_2(self): pass | 
 | 1051 |             def foo_bar(self): pass | 
 | 1052 |         m.Foo = Foo | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1053 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1054 |         tests_1 = [unittest.TestSuite([Foo('foo_bar')])] | 
 | 1055 |         tests_2 = [unittest.TestSuite([Foo('test_1'), Foo('test_2')])] | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1056 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1057 |         loader = unittest.TestLoader() | 
 | 1058 |         loader.testMethodPrefix = 'foo' | 
 | 1059 |         self.assertEqual(list(loader.loadTestsFromModule(m)), tests_1) | 
 | 1060 |  | 
 | 1061 |         loader.testMethodPrefix = 'test' | 
 | 1062 |         self.assertEqual(list(loader.loadTestsFromModule(m)), tests_2) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1063 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1064 |     # "String giving the prefix of method names which will be interpreted as | 
 | 1065 |     # test methods" | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1066 |     # | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1067 |     # Implicit in the documentation is that testMethodPrefix is respected by | 
 | 1068 |     # all loadTestsFrom* methods. | 
 | 1069 |     def test_testMethodPrefix__loadTestsFromName(self): | 
 | 1070 |         import new | 
 | 1071 |         m = new.module('m') | 
 | 1072 |         class Foo(unittest.TestCase): | 
 | 1073 |             def test_1(self): pass | 
 | 1074 |             def test_2(self): pass | 
 | 1075 |             def foo_bar(self): pass | 
 | 1076 |         m.Foo = Foo | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1077 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1078 |         tests_1 = unittest.TestSuite([Foo('foo_bar')]) | 
 | 1079 |         tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1080 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1081 |         loader = unittest.TestLoader() | 
 | 1082 |         loader.testMethodPrefix = 'foo' | 
 | 1083 |         self.assertEqual(loader.loadTestsFromName('Foo', m), tests_1) | 
 | 1084 |  | 
 | 1085 |         loader.testMethodPrefix = 'test' | 
 | 1086 |         self.assertEqual(loader.loadTestsFromName('Foo', m), tests_2) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1087 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1088 |     # "String giving the prefix of method names which will be interpreted as | 
 | 1089 |     # test methods" | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1090 |     # | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1091 |     # Implicit in the documentation is that testMethodPrefix is respected by | 
 | 1092 |     # all loadTestsFrom* methods. | 
 | 1093 |     def test_testMethodPrefix__loadTestsFromNames(self): | 
 | 1094 |         import new | 
 | 1095 |         m = new.module('m') | 
 | 1096 |         class Foo(unittest.TestCase): | 
 | 1097 |             def test_1(self): pass | 
 | 1098 |             def test_2(self): pass | 
 | 1099 |             def foo_bar(self): pass | 
 | 1100 |         m.Foo = Foo | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1101 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1102 |         tests_1 = unittest.TestSuite([unittest.TestSuite([Foo('foo_bar')])]) | 
 | 1103 |         tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')]) | 
 | 1104 |         tests_2 = unittest.TestSuite([tests_2]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1105 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1106 |         loader = unittest.TestLoader() | 
 | 1107 |         loader.testMethodPrefix = 'foo' | 
 | 1108 |         self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_1) | 
 | 1109 |  | 
 | 1110 |         loader.testMethodPrefix = 'test' | 
 | 1111 |         self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_2) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1112 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1113 |     # "The default value is 'test'" | 
 | 1114 |     def test_testMethodPrefix__default_value(self): | 
 | 1115 |         loader = unittest.TestLoader() | 
 | 1116 |         self.failUnless(loader.testMethodPrefix == 'test') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1117 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1118 |     ################################################################ | 
 | 1119 |     ### /Tests for TestLoader.testMethodPrefix | 
 | 1120 |  | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1121 |     ### Tests for TestLoader.sortTestMethodsUsing | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1122 |     ################################################################ | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1123 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1124 |     # "Function to be used to compare method names when sorting them in | 
 | 1125 |     # getTestCaseNames() and all the loadTestsFromX() methods" | 
 | 1126 |     def test_sortTestMethodsUsing__loadTestsFromTestCase(self): | 
 | 1127 |         def reversed_cmp(x, y): | 
 | 1128 |             return -cmp(x, y) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1129 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1130 |         class Foo(unittest.TestCase): | 
 | 1131 |             def test_1(self): pass | 
 | 1132 |             def test_2(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1133 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1134 |         loader = unittest.TestLoader() | 
 | 1135 |         loader.sortTestMethodsUsing = reversed_cmp | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1136 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1137 |         tests = loader.suiteClass([Foo('test_2'), Foo('test_1')]) | 
 | 1138 |         self.assertEqual(loader.loadTestsFromTestCase(Foo), tests) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1139 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1140 |     # "Function to be used to compare method names when sorting them in | 
 | 1141 |     # getTestCaseNames() and all the loadTestsFromX() methods" | 
 | 1142 |     def test_sortTestMethodsUsing__loadTestsFromModule(self): | 
 | 1143 |         def reversed_cmp(x, y): | 
 | 1144 |             return -cmp(x, y) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1145 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1146 |         import new | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1147 |         m = new.module('m') | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1148 |         class Foo(unittest.TestCase): | 
 | 1149 |             def test_1(self): pass | 
 | 1150 |             def test_2(self): pass | 
 | 1151 |         m.Foo = Foo | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1152 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1153 |         loader = unittest.TestLoader() | 
 | 1154 |         loader.sortTestMethodsUsing = reversed_cmp | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1155 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1156 |         tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])] | 
 | 1157 |         self.assertEqual(list(loader.loadTestsFromModule(m)), tests) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1158 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1159 |     # "Function to be used to compare method names when sorting them in | 
 | 1160 |     # getTestCaseNames() and all the loadTestsFromX() methods" | 
 | 1161 |     def test_sortTestMethodsUsing__loadTestsFromName(self): | 
 | 1162 |         def reversed_cmp(x, y): | 
 | 1163 |             return -cmp(x, y) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1164 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1165 |         import new | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1166 |         m = new.module('m') | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1167 |         class Foo(unittest.TestCase): | 
 | 1168 |             def test_1(self): pass | 
 | 1169 |             def test_2(self): pass | 
 | 1170 |         m.Foo = Foo | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1171 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1172 |         loader = unittest.TestLoader() | 
 | 1173 |         loader.sortTestMethodsUsing = reversed_cmp | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1174 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1175 |         tests = loader.suiteClass([Foo('test_2'), Foo('test_1')]) | 
 | 1176 |         self.assertEqual(loader.loadTestsFromName('Foo', m), tests) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1177 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1178 |     # "Function to be used to compare method names when sorting them in | 
 | 1179 |     # getTestCaseNames() and all the loadTestsFromX() methods" | 
 | 1180 |     def test_sortTestMethodsUsing__loadTestsFromNames(self): | 
 | 1181 |         def reversed_cmp(x, y): | 
 | 1182 |             return -cmp(x, y) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1183 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1184 |         import new | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1185 |         m = new.module('m') | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1186 |         class Foo(unittest.TestCase): | 
 | 1187 |             def test_1(self): pass | 
 | 1188 |             def test_2(self): pass | 
 | 1189 |         m.Foo = Foo | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1190 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1191 |         loader = unittest.TestLoader() | 
 | 1192 |         loader.sortTestMethodsUsing = reversed_cmp | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1193 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1194 |         tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])] | 
 | 1195 |         self.assertEqual(list(loader.loadTestsFromNames(['Foo'], m)), tests) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1196 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1197 |     # "Function to be used to compare method names when sorting them in | 
 | 1198 |     # getTestCaseNames()" | 
 | 1199 |     # | 
 | 1200 |     # Does it actually affect getTestCaseNames()? | 
 | 1201 |     def test_sortTestMethodsUsing__getTestCaseNames(self): | 
 | 1202 |         def reversed_cmp(x, y): | 
 | 1203 |             return -cmp(x, y) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1204 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1205 |         class Foo(unittest.TestCase): | 
 | 1206 |             def test_1(self): pass | 
 | 1207 |             def test_2(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1208 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1209 |         loader = unittest.TestLoader() | 
 | 1210 |         loader.sortTestMethodsUsing = reversed_cmp | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1211 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1212 |         test_names = ['test_2', 'test_1'] | 
 | 1213 |         self.assertEqual(loader.getTestCaseNames(Foo), test_names) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1214 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1215 |     # "The default value is the built-in cmp() function" | 
 | 1216 |     def test_sortTestMethodsUsing__default_value(self): | 
 | 1217 |         loader = unittest.TestLoader() | 
 | 1218 |         self.failUnless(loader.sortTestMethodsUsing is cmp) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1219 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1220 |     # "it can be set to None to disable the sort." | 
 | 1221 |     # | 
 | 1222 |     # XXX How is this different from reassigning cmp? Are the tests returned | 
 | 1223 |     # in a random order or something? This behaviour should die | 
 | 1224 |     def test_sortTestMethodsUsing__None(self): | 
 | 1225 |         class Foo(unittest.TestCase): | 
 | 1226 |             def test_1(self): pass | 
 | 1227 |             def test_2(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1228 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1229 |         loader = unittest.TestLoader() | 
 | 1230 |         loader.sortTestMethodsUsing = None | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1231 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1232 |         test_names = ['test_2', 'test_1'] | 
 | 1233 |         self.assertEqual(set(loader.getTestCaseNames(Foo)), set(test_names)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1234 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1235 |     ################################################################ | 
 | 1236 |     ### /Tests for TestLoader.sortTestMethodsUsing | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1237 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1238 |     ### Tests for TestLoader.suiteClass | 
 | 1239 |     ################################################################ | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1240 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1241 |     # "Callable object that constructs a test suite from a list of tests." | 
 | 1242 |     def test_suiteClass__loadTestsFromTestCase(self): | 
 | 1243 |         class Foo(unittest.TestCase): | 
 | 1244 |             def test_1(self): pass | 
 | 1245 |             def test_2(self): pass | 
 | 1246 |             def foo_bar(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1247 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1248 |         tests = [Foo('test_1'), Foo('test_2')] | 
 | 1249 |  | 
 | 1250 |         loader = unittest.TestLoader() | 
 | 1251 |         loader.suiteClass = list | 
 | 1252 |         self.assertEqual(loader.loadTestsFromTestCase(Foo), tests) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1253 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1254 |     # It is implicit in the documentation for TestLoader.suiteClass that | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1255 |     # all TestLoader.loadTestsFrom* methods respect it. Let's make sure | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1256 |     def test_suiteClass__loadTestsFromModule(self): | 
 | 1257 |         import new | 
 | 1258 |         m = new.module('m') | 
 | 1259 |         class Foo(unittest.TestCase): | 
 | 1260 |             def test_1(self): pass | 
 | 1261 |             def test_2(self): pass | 
 | 1262 |             def foo_bar(self): pass | 
 | 1263 |         m.Foo = Foo | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1264 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1265 |         tests = [[Foo('test_1'), Foo('test_2')]] | 
 | 1266 |  | 
 | 1267 |         loader = unittest.TestLoader() | 
 | 1268 |         loader.suiteClass = list | 
 | 1269 |         self.assertEqual(loader.loadTestsFromModule(m), tests) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1270 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1271 |     # It is implicit in the documentation for TestLoader.suiteClass that | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1272 |     # all TestLoader.loadTestsFrom* methods respect it. Let's make sure | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1273 |     def test_suiteClass__loadTestsFromName(self): | 
 | 1274 |         import new | 
 | 1275 |         m = new.module('m') | 
 | 1276 |         class Foo(unittest.TestCase): | 
 | 1277 |             def test_1(self): pass | 
 | 1278 |             def test_2(self): pass | 
 | 1279 |             def foo_bar(self): pass | 
 | 1280 |         m.Foo = Foo | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1281 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1282 |         tests = [Foo('test_1'), Foo('test_2')] | 
 | 1283 |  | 
 | 1284 |         loader = unittest.TestLoader() | 
 | 1285 |         loader.suiteClass = list | 
 | 1286 |         self.assertEqual(loader.loadTestsFromName('Foo', m), tests) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1287 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1288 |     # It is implicit in the documentation for TestLoader.suiteClass that | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1289 |     # all TestLoader.loadTestsFrom* methods respect it. Let's make sure | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1290 |     def test_suiteClass__loadTestsFromNames(self): | 
 | 1291 |         import new | 
 | 1292 |         m = new.module('m') | 
 | 1293 |         class Foo(unittest.TestCase): | 
 | 1294 |             def test_1(self): pass | 
 | 1295 |             def test_2(self): pass | 
 | 1296 |             def foo_bar(self): pass | 
 | 1297 |         m.Foo = Foo | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1298 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1299 |         tests = [[Foo('test_1'), Foo('test_2')]] | 
 | 1300 |  | 
 | 1301 |         loader = unittest.TestLoader() | 
 | 1302 |         loader.suiteClass = list | 
 | 1303 |         self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1304 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1305 |     # "The default value is the TestSuite class" | 
 | 1306 |     def test_suiteClass__default_value(self): | 
 | 1307 |         loader = unittest.TestLoader() | 
 | 1308 |         self.failUnless(loader.suiteClass is unittest.TestSuite) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1309 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1310 |     ################################################################ | 
 | 1311 |     ### /Tests for TestLoader.suiteClass | 
 | 1312 |  | 
 | 1313 | ### Support code for Test_TestSuite | 
 | 1314 | ################################################################ | 
 | 1315 |  | 
 | 1316 | class Foo(unittest.TestCase): | 
 | 1317 |     def test_1(self): pass | 
 | 1318 |     def test_2(self): pass | 
 | 1319 |     def test_3(self): pass | 
 | 1320 |     def runTest(self): pass | 
 | 1321 |  | 
 | 1322 | def _mk_TestSuite(*names): | 
 | 1323 |     return unittest.TestSuite(Foo(n) for n in names) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1324 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1325 | ################################################################ | 
 | 1326 | ### /Support code for Test_TestSuite | 
 | 1327 |  | 
 | 1328 | class Test_TestSuite(TestCase, TestEquality): | 
 | 1329 |  | 
 | 1330 |     ### Set up attributes needed by inherited tests | 
 | 1331 |     ################################################################ | 
 | 1332 |  | 
 | 1333 |     # Used by TestEquality.test_eq | 
 | 1334 |     eq_pairs = [(unittest.TestSuite(), unittest.TestSuite()) | 
 | 1335 |                ,(unittest.TestSuite(), unittest.TestSuite([])) | 
 | 1336 |                ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_1'))] | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1337 |  | 
 | 1338 |     # Used by TestEquality.test_ne | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1339 |     ne_pairs = [(unittest.TestSuite(), _mk_TestSuite('test_1')) | 
 | 1340 |                ,(unittest.TestSuite([]), _mk_TestSuite('test_1')) | 
 | 1341 |                ,(_mk_TestSuite('test_1', 'test_2'), _mk_TestSuite('test_1', 'test_3')) | 
 | 1342 |                ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_2'))] | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1343 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1344 |     ################################################################ | 
 | 1345 |     ### /Set up attributes needed by inherited tests | 
 | 1346 |  | 
 | 1347 |     ### Tests for TestSuite.__init__ | 
 | 1348 |     ################################################################ | 
 | 1349 |  | 
 | 1350 |     # "class TestSuite([tests])" | 
 | 1351 |     # | 
 | 1352 |     # The tests iterable should be optional | 
 | 1353 |     def test_init__tests_optional(self): | 
 | 1354 |         suite = unittest.TestSuite() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1355 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1356 |         self.assertEqual(suite.countTestCases(), 0) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1357 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1358 |     # "class TestSuite([tests])" | 
 | 1359 |     # ... | 
 | 1360 |     # "If tests is given, it must be an iterable of individual test cases | 
 | 1361 |     # or other test suites that will be used to build the suite initially" | 
 | 1362 |     # | 
 | 1363 |     # TestSuite should deal with empty tests iterables by allowing the | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1364 |     # creation of an empty suite | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1365 |     def test_init__empty_tests(self): | 
 | 1366 |         suite = unittest.TestSuite([]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1367 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1368 |         self.assertEqual(suite.countTestCases(), 0) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1369 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1370 |     # "class TestSuite([tests])" | 
 | 1371 |     # ... | 
 | 1372 |     # "If tests is given, it must be an iterable of individual test cases | 
 | 1373 |     # or other test suites that will be used to build the suite initially" | 
 | 1374 |     # | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1375 |     # TestSuite should allow any iterable to provide tests | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1376 |     def test_init__tests_from_any_iterable(self): | 
 | 1377 |         def tests(): | 
 | 1378 |             yield unittest.FunctionTestCase(lambda: None) | 
 | 1379 |             yield unittest.FunctionTestCase(lambda: None) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1380 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1381 |         suite_1 = unittest.TestSuite(tests()) | 
 | 1382 |         self.assertEqual(suite_1.countTestCases(), 2) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1383 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1384 |         suite_2 = unittest.TestSuite(suite_1) | 
 | 1385 |         self.assertEqual(suite_2.countTestCases(), 2) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1386 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1387 |         suite_3 = unittest.TestSuite(set(suite_1)) | 
 | 1388 |         self.assertEqual(suite_3.countTestCases(), 2) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1389 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1390 |     # "class TestSuite([tests])" | 
 | 1391 |     # ... | 
 | 1392 |     # "If tests is given, it must be an iterable of individual test cases | 
 | 1393 |     # or other test suites that will be used to build the suite initially" | 
 | 1394 |     # | 
 | 1395 |     # Does TestSuite() also allow other TestSuite() instances to be present | 
 | 1396 |     # in the tests iterable? | 
 | 1397 |     def test_init__TestSuite_instances_in_tests(self): | 
 | 1398 |         def tests(): | 
 | 1399 |             ftc = unittest.FunctionTestCase(lambda: None) | 
 | 1400 |             yield unittest.TestSuite([ftc]) | 
 | 1401 |             yield unittest.FunctionTestCase(lambda: None) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1402 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1403 |         suite = unittest.TestSuite(tests()) | 
 | 1404 |         self.assertEqual(suite.countTestCases(), 2) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1405 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1406 |     ################################################################ | 
 | 1407 |     ### /Tests for TestSuite.__init__ | 
 | 1408 |  | 
 | 1409 |     # Container types should support the iter protocol | 
 | 1410 |     def test_iter(self): | 
 | 1411 |         test1 = unittest.FunctionTestCase(lambda: None) | 
 | 1412 |         test2 = unittest.FunctionTestCase(lambda: None) | 
 | 1413 |         suite = unittest.TestSuite((test1, test2)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1414 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1415 |         self.assertEqual(list(suite), [test1, test2]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1416 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1417 |     # "Return the number of tests represented by the this test object. | 
 | 1418 |     # ...this method is also implemented by the TestSuite class, which can | 
 | 1419 |     # return larger [greater than 1] values" | 
 | 1420 |     # | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1421 |     # Presumably an empty TestSuite returns 0? | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1422 |     def test_countTestCases_zero_simple(self): | 
 | 1423 |         suite = unittest.TestSuite() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1424 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1425 |         self.assertEqual(suite.countTestCases(), 0) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1426 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1427 |     # "Return the number of tests represented by the this test object. | 
 | 1428 |     # ...this method is also implemented by the TestSuite class, which can | 
 | 1429 |     # return larger [greater than 1] values" | 
 | 1430 |     # | 
 | 1431 |     # Presumably an empty TestSuite (even if it contains other empty | 
 | 1432 |     # TestSuite instances) returns 0? | 
 | 1433 |     def test_countTestCases_zero_nested(self): | 
 | 1434 |         class Test1(unittest.TestCase): | 
 | 1435 |             def test(self): | 
 | 1436 |                 pass | 
 | 1437 |  | 
 | 1438 |         suite = unittest.TestSuite([unittest.TestSuite()]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1439 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1440 |         self.assertEqual(suite.countTestCases(), 0) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1441 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1442 |     # "Return the number of tests represented by the this test object. | 
 | 1443 |     # ...this method is also implemented by the TestSuite class, which can | 
 | 1444 |     # return larger [greater than 1] values" | 
 | 1445 |     def test_countTestCases_simple(self): | 
 | 1446 |         test1 = unittest.FunctionTestCase(lambda: None) | 
 | 1447 |         test2 = unittest.FunctionTestCase(lambda: None) | 
 | 1448 |         suite = unittest.TestSuite((test1, test2)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1449 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1450 |         self.assertEqual(suite.countTestCases(), 2) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1451 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1452 |     # "Return the number of tests represented by the this test object. | 
 | 1453 |     # ...this method is also implemented by the TestSuite class, which can | 
 | 1454 |     # return larger [greater than 1] values" | 
 | 1455 |     # | 
 | 1456 |     # Make sure this holds for nested TestSuite instances, too | 
 | 1457 |     def test_countTestCases_nested(self): | 
 | 1458 |         class Test1(unittest.TestCase): | 
 | 1459 |             def test1(self): pass | 
 | 1460 |             def test2(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1461 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1462 |         test2 = unittest.FunctionTestCase(lambda: None) | 
 | 1463 |         test3 = unittest.FunctionTestCase(lambda: None) | 
 | 1464 |         child = unittest.TestSuite((Test1('test2'), test2)) | 
 | 1465 |         parent = unittest.TestSuite((test3, child, Test1('test1'))) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1466 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1467 |         self.assertEqual(parent.countTestCases(), 4) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1468 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1469 |     # "Run the tests associated with this suite, collecting the result into | 
 | 1470 |     # the test result object passed as result." | 
 | 1471 |     # | 
 | 1472 |     # And if there are no tests? What then? | 
 | 1473 |     def test_run__empty_suite(self): | 
 | 1474 |         events = [] | 
 | 1475 |         result = LoggingResult(events) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1476 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1477 |         suite = unittest.TestSuite() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1478 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1479 |         suite.run(result) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1480 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1481 |         self.assertEqual(events, []) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1482 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1483 |     # "Note that unlike TestCase.run(), TestSuite.run() requires the | 
 | 1484 |     # "result object to be passed in." | 
 | 1485 |     def test_run__requires_result(self): | 
 | 1486 |         suite = unittest.TestSuite() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1487 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1488 |         try: | 
 | 1489 |             suite.run() | 
 | 1490 |         except TypeError: | 
 | 1491 |             pass | 
 | 1492 |         else: | 
 | 1493 |             self.fail("Failed to raise TypeError") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1494 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1495 |     # "Run the tests associated with this suite, collecting the result into | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1496 |     # the test result object passed as result." | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1497 |     def test_run(self): | 
 | 1498 |         events = [] | 
 | 1499 |         result = LoggingResult(events) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1500 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1501 |         class LoggingCase(unittest.TestCase): | 
 | 1502 |             def run(self, result): | 
 | 1503 |                 events.append('run %s' % self._testMethodName) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1504 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1505 |             def test1(self): pass | 
 | 1506 |             def test2(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1507 |  | 
 | 1508 |         tests = [LoggingCase('test1'), LoggingCase('test2')] | 
 | 1509 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1510 |         unittest.TestSuite(tests).run(result) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1511 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1512 |         self.assertEqual(events, ['run test1', 'run test2']) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1513 |  | 
 | 1514 |     # "Add a TestCase ... to the suite" | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1515 |     def test_addTest__TestCase(self): | 
 | 1516 |         class Foo(unittest.TestCase): | 
 | 1517 |             def test(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1518 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1519 |         test = Foo('test') | 
 | 1520 |         suite = unittest.TestSuite() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1521 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1522 |         suite.addTest(test) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1523 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1524 |         self.assertEqual(suite.countTestCases(), 1) | 
 | 1525 |         self.assertEqual(list(suite), [test]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1526 |  | 
 | 1527 |     # "Add a ... TestSuite to the suite" | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1528 |     def test_addTest__TestSuite(self): | 
 | 1529 |         class Foo(unittest.TestCase): | 
 | 1530 |             def test(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1531 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1532 |         suite_2 = unittest.TestSuite([Foo('test')]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1533 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1534 |         suite = unittest.TestSuite() | 
 | 1535 |         suite.addTest(suite_2) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1536 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1537 |         self.assertEqual(suite.countTestCases(), 1) | 
 | 1538 |         self.assertEqual(list(suite), [suite_2]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1539 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1540 |     # "Add all the tests from an iterable of TestCase and TestSuite | 
 | 1541 |     # instances to this test suite." | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1542 |     # | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1543 |     # "This is equivalent to iterating over tests, calling addTest() for | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1544 |     # each element" | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1545 |     def test_addTests(self): | 
 | 1546 |         class Foo(unittest.TestCase): | 
 | 1547 |             def test_1(self): pass | 
 | 1548 |             def test_2(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1549 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1550 |         test_1 = Foo('test_1') | 
 | 1551 |         test_2 = Foo('test_2') | 
 | 1552 |         inner_suite = unittest.TestSuite([test_2]) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1553 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1554 |         def gen(): | 
 | 1555 |             yield test_1 | 
 | 1556 |             yield test_2 | 
 | 1557 |             yield inner_suite | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1558 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1559 |         suite_1 = unittest.TestSuite() | 
 | 1560 |         suite_1.addTests(gen()) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1561 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1562 |         self.assertEqual(list(suite_1), list(gen())) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1563 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1564 |         # "This is equivalent to iterating over tests, calling addTest() for | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1565 |         # each element" | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1566 |         suite_2 = unittest.TestSuite() | 
 | 1567 |         for t in gen(): | 
 | 1568 |             suite_2.addTest(t) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1569 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1570 |         self.assertEqual(suite_1, suite_2) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1571 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1572 |     # "Add all the tests from an iterable of TestCase and TestSuite | 
 | 1573 |     # instances to this test suite." | 
 | 1574 |     # | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1575 |     # What happens if it doesn't get an iterable? | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1576 |     def test_addTest__noniterable(self): | 
 | 1577 |         suite = unittest.TestSuite() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1578 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1579 |         try: | 
 | 1580 |             suite.addTests(5) | 
 | 1581 |         except TypeError: | 
 | 1582 |             pass | 
 | 1583 |         else: | 
 | 1584 |             self.fail("Failed to raise TypeError") | 
| Georg Brandl | d9e5026 | 2007-03-07 11:54:49 +0000 | [diff] [blame] | 1585 |  | 
 | 1586 |     def test_addTest__noncallable(self): | 
 | 1587 |         suite = unittest.TestSuite() | 
 | 1588 |         self.assertRaises(TypeError, suite.addTest, 5) | 
 | 1589 |  | 
 | 1590 |     def test_addTest__casesuiteclass(self): | 
 | 1591 |         suite = unittest.TestSuite() | 
 | 1592 |         self.assertRaises(TypeError, suite.addTest, Test_TestSuite) | 
 | 1593 |         self.assertRaises(TypeError, suite.addTest, unittest.TestSuite) | 
 | 1594 |  | 
 | 1595 |     def test_addTests__string(self): | 
 | 1596 |         suite = unittest.TestSuite() | 
 | 1597 |         self.assertRaises(TypeError, suite.addTests, "foo") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1598 |  | 
 | 1599 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1600 | class Test_FunctionTestCase(TestCase): | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1601 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1602 |     # "Return the number of tests represented by the this test object. For | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1603 |     # TestCase instances, this will always be 1" | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1604 |     def test_countTestCases(self): | 
 | 1605 |         test = unittest.FunctionTestCase(lambda: None) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1606 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1607 |         self.assertEqual(test.countTestCases(), 1) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1608 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1609 |     # "When a setUp() method is defined, the test runner will run that method | 
 | 1610 |     # prior to each test. Likewise, if a tearDown() method is defined, the | 
 | 1611 |     # test runner will invoke that method after each test. In the example, | 
 | 1612 |     # setUp() was used to create a fresh sequence for each test." | 
 | 1613 |     # | 
 | 1614 |     # Make sure the proper call order is maintained, even if setUp() raises | 
 | 1615 |     # an exception. | 
 | 1616 |     def test_run_call_order__error_in_setUp(self): | 
 | 1617 |         events = [] | 
 | 1618 |         result = LoggingResult(events) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1619 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1620 |         def setUp(): | 
 | 1621 |             events.append('setUp') | 
 | 1622 |             raise RuntimeError('raised by setUp') | 
 | 1623 |  | 
 | 1624 |         def test(): | 
 | 1625 |             events.append('test') | 
 | 1626 |  | 
 | 1627 |         def tearDown(): | 
 | 1628 |             events.append('tearDown') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1629 |  | 
 | 1630 |         expected = ['startTest', 'setUp', 'addError', 'stopTest'] | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1631 |         unittest.FunctionTestCase(test, setUp, tearDown).run(result) | 
 | 1632 |         self.assertEqual(events, expected) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1633 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1634 |     # "When a setUp() method is defined, the test runner will run that method | 
 | 1635 |     # prior to each test. Likewise, if a tearDown() method is defined, the | 
 | 1636 |     # test runner will invoke that method after each test. In the example, | 
 | 1637 |     # setUp() was used to create a fresh sequence for each test." | 
 | 1638 |     # | 
 | 1639 |     # Make sure the proper call order is maintained, even if the test raises | 
 | 1640 |     # an error (as opposed to a failure). | 
 | 1641 |     def test_run_call_order__error_in_test(self): | 
 | 1642 |         events = [] | 
 | 1643 |         result = LoggingResult(events) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1644 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1645 |         def setUp(): | 
 | 1646 |             events.append('setUp') | 
 | 1647 |  | 
 | 1648 |         def test(): | 
 | 1649 |             events.append('test') | 
 | 1650 |             raise RuntimeError('raised by test') | 
 | 1651 |  | 
 | 1652 |         def tearDown(): | 
 | 1653 |             events.append('tearDown') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1654 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1655 |         expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown', | 
 | 1656 |                     'stopTest'] | 
 | 1657 |         unittest.FunctionTestCase(test, setUp, tearDown).run(result) | 
 | 1658 |         self.assertEqual(events, expected) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1659 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1660 |     # "When a setUp() method is defined, the test runner will run that method | 
 | 1661 |     # prior to each test. Likewise, if a tearDown() method is defined, the | 
 | 1662 |     # test runner will invoke that method after each test. In the example, | 
 | 1663 |     # setUp() was used to create a fresh sequence for each test." | 
 | 1664 |     # | 
 | 1665 |     # Make sure the proper call order is maintained, even if the test signals | 
 | 1666 |     # a failure (as opposed to an error). | 
 | 1667 |     def test_run_call_order__failure_in_test(self): | 
 | 1668 |         events = [] | 
 | 1669 |         result = LoggingResult(events) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1670 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1671 |         def setUp(): | 
 | 1672 |             events.append('setUp') | 
 | 1673 |  | 
 | 1674 |         def test(): | 
 | 1675 |             events.append('test') | 
 | 1676 |             self.fail('raised by test') | 
 | 1677 |  | 
 | 1678 |         def tearDown(): | 
 | 1679 |             events.append('tearDown') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1680 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1681 |         expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown', | 
 | 1682 |                     'stopTest'] | 
 | 1683 |         unittest.FunctionTestCase(test, setUp, tearDown).run(result) | 
 | 1684 |         self.assertEqual(events, expected) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1685 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1686 |     # "When a setUp() method is defined, the test runner will run that method | 
 | 1687 |     # prior to each test. Likewise, if a tearDown() method is defined, the | 
 | 1688 |     # test runner will invoke that method after each test. In the example, | 
 | 1689 |     # setUp() was used to create a fresh sequence for each test." | 
 | 1690 |     # | 
 | 1691 |     # Make sure the proper call order is maintained, even if tearDown() raises | 
 | 1692 |     # an exception. | 
 | 1693 |     def test_run_call_order__error_in_tearDown(self): | 
 | 1694 |         events = [] | 
 | 1695 |         result = LoggingResult(events) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1696 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1697 |         def setUp(): | 
 | 1698 |             events.append('setUp') | 
 | 1699 |  | 
 | 1700 |         def test(): | 
 | 1701 |             events.append('test') | 
 | 1702 |  | 
 | 1703 |         def tearDown(): | 
 | 1704 |             events.append('tearDown') | 
 | 1705 |             raise RuntimeError('raised by tearDown') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1706 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1707 |         expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError', | 
 | 1708 |                     'stopTest'] | 
 | 1709 |         unittest.FunctionTestCase(test, setUp, tearDown).run(result) | 
 | 1710 |         self.assertEqual(events, expected) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1711 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1712 |     # "Return a string identifying the specific test case." | 
 | 1713 |     # | 
 | 1714 |     # Because of the vague nature of the docs, I'm not going to lock this | 
 | 1715 |     # test down too much. Really all that can be asserted is that the id() | 
 | 1716 |     # will be a string (either 8-byte or unicode -- again, because the docs | 
 | 1717 |     # just say "string") | 
 | 1718 |     def test_id(self): | 
 | 1719 |         test = unittest.FunctionTestCase(lambda: None) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1720 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1721 |         self.failUnless(isinstance(test.id(), basestring)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1722 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1723 |     # "Returns a one-line description of the test, or None if no description | 
 | 1724 |     # has been provided. The default implementation of this method returns | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1725 |     # the first line of the test method's docstring, if available, or None." | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1726 |     def test_shortDescription__no_docstring(self): | 
 | 1727 |         test = unittest.FunctionTestCase(lambda: None) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1728 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1729 |         self.assertEqual(test.shortDescription(), None) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1730 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1731 |     # "Returns a one-line description of the test, or None if no description | 
 | 1732 |     # has been provided. The default implementation of this method returns | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1733 |     # the first line of the test method's docstring, if available, or None." | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1734 |     def test_shortDescription__singleline_docstring(self): | 
 | 1735 |         desc = "this tests foo" | 
 | 1736 |         test = unittest.FunctionTestCase(lambda: None, description=desc) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1737 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1738 |         self.assertEqual(test.shortDescription(), "this tests foo") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1739 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1740 | class Test_TestResult(TestCase): | 
 | 1741 |     # Note: there are not separate tests for TestResult.wasSuccessful(), | 
 | 1742 |     # TestResult.errors, TestResult.failures, TestResult.testsRun or | 
 | 1743 |     # TestResult.shouldStop because these only have meaning in terms of | 
 | 1744 |     # other TestResult methods. | 
 | 1745 |     # | 
 | 1746 |     # Accordingly, tests for the aforenamed attributes are incorporated | 
 | 1747 |     # in with the tests for the defining methods. | 
 | 1748 |     ################################################################ | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1749 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1750 |     def test_init(self): | 
 | 1751 |         result = unittest.TestResult() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1752 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1753 |         self.failUnless(result.wasSuccessful()) | 
 | 1754 |         self.assertEqual(len(result.errors), 0) | 
 | 1755 |         self.assertEqual(len(result.failures), 0) | 
 | 1756 |         self.assertEqual(result.testsRun, 0) | 
 | 1757 |         self.assertEqual(result.shouldStop, False) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1758 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1759 |     # "This method can be called to signal that the set of tests being | 
 | 1760 |     # run should be aborted by setting the TestResult's shouldStop | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1761 |     # attribute to True." | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1762 |     def test_stop(self): | 
 | 1763 |         result = unittest.TestResult() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1764 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1765 |         result.stop() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1766 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1767 |         self.assertEqual(result.shouldStop, True) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1768 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1769 |     # "Called when the test case test is about to be run. The default | 
 | 1770 |     # implementation simply increments the instance's testsRun counter." | 
 | 1771 |     def test_startTest(self): | 
 | 1772 |         class Foo(unittest.TestCase): | 
 | 1773 |             def test_1(self): | 
 | 1774 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1775 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1776 |         test = Foo('test_1') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1777 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1778 |         result = unittest.TestResult() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1779 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1780 |         result.startTest(test) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1781 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1782 |         self.failUnless(result.wasSuccessful()) | 
 | 1783 |         self.assertEqual(len(result.errors), 0) | 
 | 1784 |         self.assertEqual(len(result.failures), 0) | 
 | 1785 |         self.assertEqual(result.testsRun, 1) | 
 | 1786 |         self.assertEqual(result.shouldStop, False) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1787 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1788 |         result.stopTest(test) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1789 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1790 |     # "Called after the test case test has been executed, regardless of | 
 | 1791 |     # the outcome. The default implementation does nothing." | 
 | 1792 |     def test_stopTest(self): | 
 | 1793 |         class Foo(unittest.TestCase): | 
 | 1794 |             def test_1(self): | 
 | 1795 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1796 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1797 |         test = Foo('test_1') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1798 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1799 |         result = unittest.TestResult() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1800 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1801 |         result.startTest(test) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1802 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1803 |         self.failUnless(result.wasSuccessful()) | 
 | 1804 |         self.assertEqual(len(result.errors), 0) | 
 | 1805 |         self.assertEqual(len(result.failures), 0) | 
 | 1806 |         self.assertEqual(result.testsRun, 1) | 
 | 1807 |         self.assertEqual(result.shouldStop, False) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1808 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1809 |         result.stopTest(test) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1810 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1811 |         # Same tests as above; make sure nothing has changed | 
 | 1812 |         self.failUnless(result.wasSuccessful()) | 
 | 1813 |         self.assertEqual(len(result.errors), 0) | 
 | 1814 |         self.assertEqual(len(result.failures), 0) | 
 | 1815 |         self.assertEqual(result.testsRun, 1) | 
 | 1816 |         self.assertEqual(result.shouldStop, False) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1817 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1818 |     # "addSuccess(test)" | 
 | 1819 |     # ... | 
 | 1820 |     # "Called when the test case test succeeds" | 
 | 1821 |     # ... | 
 | 1822 |     # "wasSuccessful() - Returns True if all tests run so far have passed, | 
 | 1823 |     # otherwise returns False" | 
 | 1824 |     # ... | 
 | 1825 |     # "testsRun - The total number of tests run so far." | 
 | 1826 |     # ... | 
 | 1827 |     # "errors - A list containing 2-tuples of TestCase instances and | 
 | 1828 |     # formatted tracebacks. Each tuple represents a test which raised an | 
 | 1829 |     # unexpected exception. Contains formatted | 
 | 1830 |     # tracebacks instead of sys.exc_info() results." | 
 | 1831 |     # ... | 
 | 1832 |     # "failures - A list containing 2-tuples of TestCase instances and | 
 | 1833 |     # formatted tracebacks. Each tuple represents a test where a failure was | 
 | 1834 |     # explicitly signalled using the TestCase.fail*() or TestCase.assert*() | 
 | 1835 |     # methods. Contains formatted tracebacks instead | 
 | 1836 |     # of sys.exc_info() results." | 
 | 1837 |     def test_addSuccess(self): | 
 | 1838 |         class Foo(unittest.TestCase): | 
 | 1839 |             def test_1(self): | 
 | 1840 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1841 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1842 |         test = Foo('test_1') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1843 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1844 |         result = unittest.TestResult() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1845 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1846 |         result.startTest(test) | 
 | 1847 |         result.addSuccess(test) | 
 | 1848 |         result.stopTest(test) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1849 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1850 |         self.failUnless(result.wasSuccessful()) | 
 | 1851 |         self.assertEqual(len(result.errors), 0) | 
 | 1852 |         self.assertEqual(len(result.failures), 0) | 
 | 1853 |         self.assertEqual(result.testsRun, 1) | 
 | 1854 |         self.assertEqual(result.shouldStop, False) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1855 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1856 |     # "addFailure(test, err)" | 
 | 1857 |     # ... | 
 | 1858 |     # "Called when the test case test signals a failure. err is a tuple of | 
 | 1859 |     # the form returned by sys.exc_info(): (type, value, traceback)" | 
 | 1860 |     # ... | 
 | 1861 |     # "wasSuccessful() - Returns True if all tests run so far have passed, | 
 | 1862 |     # otherwise returns False" | 
 | 1863 |     # ... | 
 | 1864 |     # "testsRun - The total number of tests run so far." | 
 | 1865 |     # ... | 
 | 1866 |     # "errors - A list containing 2-tuples of TestCase instances and | 
 | 1867 |     # formatted tracebacks. Each tuple represents a test which raised an | 
 | 1868 |     # unexpected exception. Contains formatted | 
 | 1869 |     # tracebacks instead of sys.exc_info() results." | 
 | 1870 |     # ... | 
 | 1871 |     # "failures - A list containing 2-tuples of TestCase instances and | 
 | 1872 |     # formatted tracebacks. Each tuple represents a test where a failure was | 
 | 1873 |     # explicitly signalled using the TestCase.fail*() or TestCase.assert*() | 
 | 1874 |     # methods. Contains formatted tracebacks instead | 
 | 1875 |     # of sys.exc_info() results." | 
 | 1876 |     def test_addFailure(self): | 
 | 1877 |         import sys | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1878 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1879 |         class Foo(unittest.TestCase): | 
 | 1880 |             def test_1(self): | 
 | 1881 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1882 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1883 |         test = Foo('test_1') | 
 | 1884 |         try: | 
 | 1885 |             test.fail("foo") | 
 | 1886 |         except: | 
 | 1887 |             exc_info_tuple = sys.exc_info() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1888 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1889 |         result = unittest.TestResult() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1890 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1891 |         result.startTest(test) | 
 | 1892 |         result.addFailure(test, exc_info_tuple) | 
 | 1893 |         result.stopTest(test) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1894 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1895 |         self.failIf(result.wasSuccessful()) | 
 | 1896 |         self.assertEqual(len(result.errors), 0) | 
 | 1897 |         self.assertEqual(len(result.failures), 1) | 
 | 1898 |         self.assertEqual(result.testsRun, 1) | 
 | 1899 |         self.assertEqual(result.shouldStop, False) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1900 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1901 |         test_case, formatted_exc = result.failures[0] | 
 | 1902 |         self.failUnless(test_case is test) | 
 | 1903 |         self.failUnless(isinstance(formatted_exc, str)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1904 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1905 |     # "addError(test, err)" | 
 | 1906 |     # ... | 
 | 1907 |     # "Called when the test case test raises an unexpected exception err | 
 | 1908 |     # is a tuple of the form returned by sys.exc_info(): | 
 | 1909 |     # (type, value, traceback)" | 
 | 1910 |     # ... | 
 | 1911 |     # "wasSuccessful() - Returns True if all tests run so far have passed, | 
 | 1912 |     # otherwise returns False" | 
 | 1913 |     # ... | 
 | 1914 |     # "testsRun - The total number of tests run so far." | 
 | 1915 |     # ... | 
 | 1916 |     # "errors - A list containing 2-tuples of TestCase instances and | 
 | 1917 |     # formatted tracebacks. Each tuple represents a test which raised an | 
 | 1918 |     # unexpected exception. Contains formatted | 
 | 1919 |     # tracebacks instead of sys.exc_info() results." | 
 | 1920 |     # ... | 
 | 1921 |     # "failures - A list containing 2-tuples of TestCase instances and | 
 | 1922 |     # formatted tracebacks. Each tuple represents a test where a failure was | 
 | 1923 |     # explicitly signalled using the TestCase.fail*() or TestCase.assert*() | 
 | 1924 |     # methods. Contains formatted tracebacks instead | 
 | 1925 |     # of sys.exc_info() results." | 
 | 1926 |     def test_addError(self): | 
 | 1927 |         import sys | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1928 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1929 |         class Foo(unittest.TestCase): | 
 | 1930 |             def test_1(self): | 
 | 1931 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1932 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1933 |         test = Foo('test_1') | 
 | 1934 |         try: | 
 | 1935 |             raise TypeError() | 
 | 1936 |         except: | 
 | 1937 |             exc_info_tuple = sys.exc_info() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1938 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1939 |         result = unittest.TestResult() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1940 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1941 |         result.startTest(test) | 
 | 1942 |         result.addError(test, exc_info_tuple) | 
 | 1943 |         result.stopTest(test) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1944 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1945 |         self.failIf(result.wasSuccessful()) | 
 | 1946 |         self.assertEqual(len(result.errors), 1) | 
 | 1947 |         self.assertEqual(len(result.failures), 0) | 
 | 1948 |         self.assertEqual(result.testsRun, 1) | 
 | 1949 |         self.assertEqual(result.shouldStop, False) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1950 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1951 |         test_case, formatted_exc = result.errors[0] | 
 | 1952 |         self.failUnless(test_case is test) | 
 | 1953 |         self.failUnless(isinstance(formatted_exc, str)) | 
 | 1954 |  | 
 | 1955 | ### Support code for Test_TestCase | 
 | 1956 | ################################################################ | 
 | 1957 |  | 
 | 1958 | class Foo(unittest.TestCase): | 
 | 1959 |     def runTest(self): pass | 
 | 1960 |     def test1(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1961 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1962 | class Bar(Foo): | 
 | 1963 |     def test2(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1964 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1965 | ################################################################ | 
 | 1966 | ### /Support code for Test_TestCase | 
 | 1967 |  | 
 | 1968 | class Test_TestCase(TestCase, TestEquality, TestHashing): | 
 | 1969 |  | 
 | 1970 |     ### Set up attributes used by inherited tests | 
 | 1971 |     ################################################################ | 
 | 1972 |  | 
 | 1973 |     # Used by TestHashing.test_hash and TestEquality.test_eq | 
 | 1974 |     eq_pairs = [(Foo('test1'), Foo('test1'))] | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1975 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1976 |     # Used by TestEquality.test_ne | 
 | 1977 |     ne_pairs = [(Foo('test1'), Foo('runTest')) | 
 | 1978 |                ,(Foo('test1'), Bar('test1')) | 
 | 1979 |                ,(Foo('test1'), Bar('test2'))] | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1980 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1981 |     ################################################################ | 
 | 1982 |     ### /Set up attributes used by inherited tests | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1983 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1984 |  | 
 | 1985 |     # "class TestCase([methodName])" | 
 | 1986 |     # ... | 
 | 1987 |     # "Each instance of TestCase will run a single test method: the | 
 | 1988 |     # method named methodName." | 
 | 1989 |     # ... | 
 | 1990 |     # "methodName defaults to "runTest"." | 
 | 1991 |     # | 
 | 1992 |     # Make sure it really is optional, and that it defaults to the proper | 
 | 1993 |     # thing. | 
 | 1994 |     def test_init__no_test_name(self): | 
 | 1995 |         class Test(unittest.TestCase): | 
 | 1996 |             def runTest(self): raise MyException() | 
 | 1997 |             def test(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 1998 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 1999 |         self.assertEqual(Test().id()[-13:], '.Test.runTest') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2000 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2001 |     # "class TestCase([methodName])" | 
 | 2002 |     # ... | 
 | 2003 |     # "Each instance of TestCase will run a single test method: the | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2004 |     # method named methodName." | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2005 |     def test_init__test_name__valid(self): | 
 | 2006 |         class Test(unittest.TestCase): | 
 | 2007 |             def runTest(self): raise MyException() | 
 | 2008 |             def test(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2009 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2010 |         self.assertEqual(Test('test').id()[-10:], '.Test.test') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2011 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2012 |     # "class TestCase([methodName])" | 
 | 2013 |     # ... | 
 | 2014 |     # "Each instance of TestCase will run a single test method: the | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2015 |     # method named methodName." | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2016 |     def test_init__test_name__invalid(self): | 
 | 2017 |         class Test(unittest.TestCase): | 
 | 2018 |             def runTest(self): raise MyException() | 
 | 2019 |             def test(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2020 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2021 |         try: | 
 | 2022 |             Test('testfoo') | 
 | 2023 |         except ValueError: | 
 | 2024 |             pass | 
 | 2025 |         else: | 
 | 2026 |             self.fail("Failed to raise ValueError") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2027 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2028 |     # "Return the number of tests represented by the this test object. For | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2029 |     # TestCase instances, this will always be 1" | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2030 |     def test_countTestCases(self): | 
 | 2031 |         class Foo(unittest.TestCase): | 
 | 2032 |             def test(self): pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2033 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2034 |         self.assertEqual(Foo('test').countTestCases(), 1) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2035 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2036 |     # "Return the default type of test result object to be used to run this | 
 | 2037 |     # test. For TestCase instances, this will always be | 
 | 2038 |     # unittest.TestResult;  subclasses of TestCase should | 
 | 2039 |     # override this as necessary." | 
 | 2040 |     def test_defaultTestResult(self): | 
 | 2041 |         class Foo(unittest.TestCase): | 
 | 2042 |             def runTest(self): | 
 | 2043 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2044 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2045 |         result = Foo().defaultTestResult() | 
 | 2046 |         self.assertEqual(type(result), unittest.TestResult) | 
 | 2047 |  | 
 | 2048 |     # "When a setUp() method is defined, the test runner will run that method | 
 | 2049 |     # prior to each test. Likewise, if a tearDown() method is defined, the | 
 | 2050 |     # test runner will invoke that method after each test. In the example, | 
 | 2051 |     # setUp() was used to create a fresh sequence for each test." | 
 | 2052 |     # | 
 | 2053 |     # Make sure the proper call order is maintained, even if setUp() raises | 
 | 2054 |     # an exception. | 
 | 2055 |     def test_run_call_order__error_in_setUp(self): | 
 | 2056 |         events = [] | 
 | 2057 |         result = LoggingResult(events) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2058 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2059 |         class Foo(unittest.TestCase): | 
 | 2060 |             def setUp(self): | 
 | 2061 |                 events.append('setUp') | 
 | 2062 |                 raise RuntimeError('raised by Foo.setUp') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2063 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2064 |             def test(self): | 
 | 2065 |                 events.append('test') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2066 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2067 |             def tearDown(self): | 
 | 2068 |                 events.append('tearDown') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2069 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2070 |         Foo('test').run(result) | 
 | 2071 |         expected = ['startTest', 'setUp', 'addError', 'stopTest'] | 
 | 2072 |         self.assertEqual(events, expected) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2073 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2074 |     # "When a setUp() method is defined, the test runner will run that method | 
 | 2075 |     # prior to each test. Likewise, if a tearDown() method is defined, the | 
 | 2076 |     # test runner will invoke that method after each test. In the example, | 
 | 2077 |     # setUp() was used to create a fresh sequence for each test." | 
 | 2078 |     # | 
 | 2079 |     # Make sure the proper call order is maintained, even if the test raises | 
 | 2080 |     # an error (as opposed to a failure). | 
 | 2081 |     def test_run_call_order__error_in_test(self): | 
 | 2082 |         events = [] | 
 | 2083 |         result = LoggingResult(events) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2084 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2085 |         class Foo(unittest.TestCase): | 
 | 2086 |             def setUp(self): | 
 | 2087 |                 events.append('setUp') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2088 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2089 |             def test(self): | 
 | 2090 |                 events.append('test') | 
 | 2091 |                 raise RuntimeError('raised by Foo.test') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2092 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2093 |             def tearDown(self): | 
 | 2094 |                 events.append('tearDown') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2095 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2096 |         expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown', | 
 | 2097 |                     'stopTest'] | 
 | 2098 |         Foo('test').run(result) | 
 | 2099 |         self.assertEqual(events, expected) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2100 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2101 |     # "When a setUp() method is defined, the test runner will run that method | 
 | 2102 |     # prior to each test. Likewise, if a tearDown() method is defined, the | 
 | 2103 |     # test runner will invoke that method after each test. In the example, | 
 | 2104 |     # setUp() was used to create a fresh sequence for each test." | 
 | 2105 |     # | 
 | 2106 |     # Make sure the proper call order is maintained, even if the test signals | 
 | 2107 |     # a failure (as opposed to an error). | 
 | 2108 |     def test_run_call_order__failure_in_test(self): | 
 | 2109 |         events = [] | 
 | 2110 |         result = LoggingResult(events) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2111 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2112 |         class Foo(unittest.TestCase): | 
 | 2113 |             def setUp(self): | 
 | 2114 |                 events.append('setUp') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2115 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2116 |             def test(self): | 
 | 2117 |                 events.append('test') | 
 | 2118 |                 self.fail('raised by Foo.test') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2119 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2120 |             def tearDown(self): | 
 | 2121 |                 events.append('tearDown') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2122 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2123 |         expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown', | 
 | 2124 |                     'stopTest'] | 
 | 2125 |         Foo('test').run(result) | 
 | 2126 |         self.assertEqual(events, expected) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2127 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2128 |     # "When a setUp() method is defined, the test runner will run that method | 
 | 2129 |     # prior to each test. Likewise, if a tearDown() method is defined, the | 
 | 2130 |     # test runner will invoke that method after each test. In the example, | 
 | 2131 |     # setUp() was used to create a fresh sequence for each test." | 
 | 2132 |     # | 
 | 2133 |     # Make sure the proper call order is maintained, even if tearDown() raises | 
 | 2134 |     # an exception. | 
 | 2135 |     def test_run_call_order__error_in_tearDown(self): | 
 | 2136 |         events = [] | 
 | 2137 |         result = LoggingResult(events) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2138 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2139 |         class Foo(unittest.TestCase): | 
 | 2140 |             def setUp(self): | 
 | 2141 |                 events.append('setUp') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2142 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2143 |             def test(self): | 
 | 2144 |                 events.append('test') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2145 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2146 |             def tearDown(self): | 
 | 2147 |                 events.append('tearDown') | 
 | 2148 |                 raise RuntimeError('raised by Foo.tearDown') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2149 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2150 |         Foo('test').run(result) | 
 | 2151 |         expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError', | 
 | 2152 |                     'stopTest'] | 
 | 2153 |         self.assertEqual(events, expected) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2154 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2155 |     # "This class attribute gives the exception raised by the test() method. | 
 | 2156 |     # If a test framework needs to use a specialized exception, possibly to | 
 | 2157 |     # carry additional information, it must subclass this exception in | 
 | 2158 |     # order to ``play fair'' with the framework.  The initial value of this | 
 | 2159 |     # attribute is AssertionError" | 
 | 2160 |     def test_failureException__default(self): | 
 | 2161 |         class Foo(unittest.TestCase): | 
 | 2162 |             def test(self): | 
 | 2163 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2164 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2165 |         self.failUnless(Foo('test').failureException is AssertionError) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2166 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2167 |     # "This class attribute gives the exception raised by the test() method. | 
 | 2168 |     # If a test framework needs to use a specialized exception, possibly to | 
 | 2169 |     # carry additional information, it must subclass this exception in | 
 | 2170 |     # order to ``play fair'' with the framework." | 
 | 2171 |     # | 
 | 2172 |     # Make sure TestCase.run() respects the designated failureException | 
 | 2173 |     def test_failureException__subclassing__explicit_raise(self): | 
 | 2174 |         events = [] | 
 | 2175 |         result = LoggingResult(events) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2176 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2177 |         class Foo(unittest.TestCase): | 
 | 2178 |             def test(self): | 
 | 2179 |                 raise RuntimeError() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2180 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2181 |             failureException = RuntimeError | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2182 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2183 |         self.failUnless(Foo('test').failureException is RuntimeError) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2184 |  | 
 | 2185 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2186 |         Foo('test').run(result) | 
 | 2187 |         expected = ['startTest', 'addFailure', 'stopTest'] | 
 | 2188 |         self.assertEqual(events, expected) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2189 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2190 |     # "This class attribute gives the exception raised by the test() method. | 
 | 2191 |     # If a test framework needs to use a specialized exception, possibly to | 
 | 2192 |     # carry additional information, it must subclass this exception in | 
 | 2193 |     # order to ``play fair'' with the framework." | 
 | 2194 |     # | 
 | 2195 |     # Make sure TestCase.run() respects the designated failureException | 
 | 2196 |     def test_failureException__subclassing__implicit_raise(self): | 
 | 2197 |         events = [] | 
 | 2198 |         result = LoggingResult(events) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2199 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2200 |         class Foo(unittest.TestCase): | 
 | 2201 |             def test(self): | 
 | 2202 |                 self.fail("foo") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2203 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2204 |             failureException = RuntimeError | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2205 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2206 |         self.failUnless(Foo('test').failureException is RuntimeError) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2207 |  | 
 | 2208 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2209 |         Foo('test').run(result) | 
 | 2210 |         expected = ['startTest', 'addFailure', 'stopTest'] | 
 | 2211 |         self.assertEqual(events, expected) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2212 |  | 
 | 2213 |     # "The default implementation does nothing." | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2214 |     def test_setUp(self): | 
 | 2215 |         class Foo(unittest.TestCase): | 
 | 2216 |             def runTest(self): | 
 | 2217 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2218 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2219 |         # ... and nothing should happen | 
 | 2220 |         Foo().setUp() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2221 |  | 
 | 2222 |     # "The default implementation does nothing." | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2223 |     def test_tearDown(self): | 
 | 2224 |         class Foo(unittest.TestCase): | 
 | 2225 |             def runTest(self): | 
 | 2226 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2227 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2228 |         # ... and nothing should happen | 
 | 2229 |         Foo().tearDown() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2230 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2231 |     # "Return a string identifying the specific test case." | 
 | 2232 |     # | 
 | 2233 |     # Because of the vague nature of the docs, I'm not going to lock this | 
 | 2234 |     # test down too much. Really all that can be asserted is that the id() | 
 | 2235 |     # will be a string (either 8-byte or unicode -- again, because the docs | 
 | 2236 |     # just say "string") | 
 | 2237 |     def test_id(self): | 
 | 2238 |         class Foo(unittest.TestCase): | 
 | 2239 |             def runTest(self): | 
 | 2240 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2241 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2242 |         self.failUnless(isinstance(Foo().id(), basestring)) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2243 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2244 |     # "Returns a one-line description of the test, or None if no description | 
 | 2245 |     # has been provided. The default implementation of this method returns | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2246 |     # the first line of the test method's docstring, if available, or None." | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2247 |     def test_shortDescription__no_docstring(self): | 
 | 2248 |         class Foo(unittest.TestCase): | 
 | 2249 |             def runTest(self): | 
 | 2250 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2251 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2252 |         self.assertEqual(Foo().shortDescription(), None) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2253 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2254 |     # "Returns a one-line description of the test, or None if no description | 
 | 2255 |     # has been provided. The default implementation of this method returns | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2256 |     # the first line of the test method's docstring, if available, or None." | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2257 |     def test_shortDescription__singleline_docstring(self): | 
 | 2258 |         class Foo(unittest.TestCase): | 
 | 2259 |             def runTest(self): | 
 | 2260 |                 "this tests foo" | 
 | 2261 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2262 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2263 |         self.assertEqual(Foo().shortDescription(), "this tests foo") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2264 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2265 |     # "Returns a one-line description of the test, or None if no description | 
 | 2266 |     # has been provided. The default implementation of this method returns | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2267 |     # the first line of the test method's docstring, if available, or None." | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2268 |     def test_shortDescription__multiline_docstring(self): | 
 | 2269 |         class Foo(unittest.TestCase): | 
 | 2270 |             def runTest(self): | 
 | 2271 |                 """this tests foo | 
 | 2272 |                 blah, bar and baz are also tested""" | 
 | 2273 |                 pass | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2274 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2275 |         self.assertEqual(Foo().shortDescription(), "this tests foo") | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2276 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2277 |     # "If result is omitted or None, a temporary result object is created | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2278 |     # and used, but is not made available to the caller" | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2279 |     def test_run__uses_defaultTestResult(self): | 
 | 2280 |         events = [] | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2281 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2282 |         class Foo(unittest.TestCase): | 
 | 2283 |             def test(self): | 
 | 2284 |                 events.append('test') | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2285 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2286 |             def defaultTestResult(self): | 
 | 2287 |                 return LoggingResult(events) | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2288 |  | 
 | 2289 |         # Make run() find a result object on its own | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2290 |         Foo('test').run() | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2291 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2292 |         expected = ['startTest', 'test', 'stopTest'] | 
 | 2293 |         self.assertEqual(events, expected) | 
| Jim Fulton | fafd874 | 2004-08-28 15:22:12 +0000 | [diff] [blame] | 2294 |  | 
 | 2295 | ###################################################################### | 
 | 2296 | ## Main | 
 | 2297 | ###################################################################### | 
 | 2298 |  | 
 | 2299 | def test_main(): | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2300 |     test_support.run_unittest(Test_TestCase, Test_TestLoader, | 
 | 2301 |         Test_TestSuite, Test_TestResult, Test_FunctionTestCase) | 
| Jim Fulton | fafd874 | 2004-08-28 15:22:12 +0000 | [diff] [blame] | 2302 |  | 
| Georg Brandl | 15c5ce9 | 2007-03-07 09:09:40 +0000 | [diff] [blame] | 2303 | if __name__ == "__main__": | 
| Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 2304 |     test_main() |