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