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