annotate Lib/test/test_unittest.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents eb9b1ac3d9d8
children b9c78fb6d71d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33314
2f52df173952 Added an __iter__ method for test suites.
Jim Fulton <jim@zope.com>
parents:
diff changeset
1 """Test script for unittest.
2f52df173952 Added an __iter__ method for test suites.
Jim Fulton <jim@zope.com>
parents:
diff changeset
2
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
3 By Collin Winter <collinw at gmail.com>
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
4
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
5 Still need testing:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
6 TestCase.{assert,fail}* methods (some are tested implicitly)
33314
2f52df173952 Added an __iter__ method for test suites.
Jim Fulton <jim@zope.com>
parents:
diff changeset
7 """
2f52df173952 Added an __iter__ method for test suites.
Jim Fulton <jim@zope.com>
parents:
diff changeset
8
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
9 from StringIO import StringIO
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
10 import re
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
11 from test import test_support
33314
2f52df173952 Added an __iter__ method for test suites.
Jim Fulton <jim@zope.com>
parents:
diff changeset
12 import unittest
53664
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
13 from unittest import TestCase, TestProgram
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
14 import types
52729
1abf9e30c19f Store the functions in the _type_equality_funcs as wrapped objects that are deep copyable.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52719
diff changeset
15 from copy import deepcopy
53664
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
16 from cStringIO import StringIO
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
17
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
18 ### Support code
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
19 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
20
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
21 class LoggingResult(unittest.TestResult):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
22 def __init__(self, log):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
23 self._events = log
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
24 super(LoggingResult, self).__init__()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
25
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
26 def startTest(self, test):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
27 self._events.append('startTest')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
28 super(LoggingResult, self).startTest(test)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
29
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
30 def startTestRun(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
31 self._events.append('startTestRun')
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
32 super(LoggingResult, self).startTestRun()
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
33
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
34 def stopTest(self, test):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
35 self._events.append('stopTest')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
36 super(LoggingResult, self).stopTest(test)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
37
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
38 def stopTestRun(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
39 self._events.append('stopTestRun')
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
40 super(LoggingResult, self).stopTestRun()
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
41
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
42 def addFailure(self, *args):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
43 self._events.append('addFailure')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
44 super(LoggingResult, self).addFailure(*args)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
45
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
46 def addSuccess(self, *args):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
47 self._events.append('addSuccess')
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
48 super(LoggingResult, self).addSuccess(*args)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
49
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
50 def addError(self, *args):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
51 self._events.append('addError')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
52 super(LoggingResult, self).addError(*args)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
53
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
54 def addSkip(self, *args):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
55 self._events.append('addSkip')
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
56 super(LoggingResult, self).addSkip(*args)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
57
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
58 def addExpectedFailure(self, *args):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
59 self._events.append('addExpectedFailure')
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
60 super(LoggingResult, self).addExpectedFailure(*args)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
61
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
62 def addUnexpectedSuccess(self, *args):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
63 self._events.append('addUnexpectedSuccess')
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
64 super(LoggingResult, self).addUnexpectedSuccess(*args)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
65
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
66
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
67 class TestEquality(object):
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
68 """Used as a mixin for TestCase"""
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
69
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
70 # Check for a valid __eq__ implementation
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
71 def test_eq(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
72 for obj_1, obj_2 in self.eq_pairs:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
73 self.assertEqual(obj_1, obj_2)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
74 self.assertEqual(obj_2, obj_1)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
75
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
76 # Check for a valid __ne__ implementation
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
77 def test_ne(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
78 for obj_1, obj_2 in self.ne_pairs:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
79 self.failIfEqual(obj_1, obj_2)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
80 self.failIfEqual(obj_2, obj_1)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
81
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
82 class TestHashing(object):
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
83 """Used as a mixin for TestCase"""
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
84
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
85 # Check for a valid __hash__ implementation
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
86 def test_hash(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
87 for obj_1, obj_2 in self.eq_pairs:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
88 try:
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
89 if not hash(obj_1) == hash(obj_2):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
90 self.fail("%r and %r do not hash equal" % (obj_1, obj_2))
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
91 except KeyboardInterrupt:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
92 raise
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
93 except Exception, e:
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
94 self.fail("Problem hashing %r and %r: %s" % (obj_1, obj_2, e))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
95
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
96 for obj_1, obj_2 in self.ne_pairs:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
97 try:
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
98 if hash(obj_1) == hash(obj_2):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
99 self.fail("%s and %s hash equal, but shouldn't" %
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
100 (obj_1, obj_2))
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
101 except KeyboardInterrupt:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
102 raise
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
103 except Exception, e:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
104 self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
105
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
106
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
107 # List subclass we can add attributes to.
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
108 class MyClassSuite(list):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
109
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
110 def __init__(self, tests, klass):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
111 super(MyClassSuite, self).__init__(tests)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
112
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
113
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
114 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
115 ### /Support code
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
116
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
117 class Test_TestLoader(TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
118
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
119 ### Tests for TestLoader.loadTestsFromTestCase
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
120 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
121
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
122 # "Return a suite of all tests cases contained in the TestCase-derived
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
123 # class testCaseClass"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
124 def test_loadTestsFromTestCase(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
125 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
126 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
127 def test_2(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
128 def foo_bar(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
129
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
130 tests = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
131
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
132 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
133 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
134
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
135 # "Return a suite of all tests cases contained in the TestCase-derived
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
136 # class testCaseClass"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
137 #
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
138 # Make sure it does the right thing even if no tests were found
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
139 def test_loadTestsFromTestCase__no_matches(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
140 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
141 def foo_bar(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
142
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
143 empty_suite = unittest.TestSuite()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
144
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
145 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
146 self.assertEqual(loader.loadTestsFromTestCase(Foo), empty_suite)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
147
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
148 # "Return a suite of all tests cases contained in the TestCase-derived
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
149 # class testCaseClass"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
150 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
151 # What happens if loadTestsFromTestCase() is given an object
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
152 # that isn't a subclass of TestCase? Specifically, what happens
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
153 # if testCaseClass is a subclass of TestSuite?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
154 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
155 # This is checked for specifically in the code, so we better add a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
156 # test for it.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
157 def test_loadTestsFromTestCase__TestSuite_subclass(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
158 class NotATestCase(unittest.TestSuite):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
159 pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
160
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
161 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
162 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
163 loader.loadTestsFromTestCase(NotATestCase)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
164 except TypeError:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
165 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
166 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
167 self.fail('Should raise TypeError')
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
168
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
169 # "Return a suite of all tests cases contained in the TestCase-derived
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
170 # class testCaseClass"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
171 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
172 # Make sure loadTestsFromTestCase() picks up the default test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
173 # name (as specified by TestCase), even though the method name does
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
174 # not match the default TestLoader.testMethodPrefix string
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
175 def test_loadTestsFromTestCase__default_method_name(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
176 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
177 def runTest(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
178 pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
179
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
180 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
181 # This has to be false for the test to succeed
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
182 self.failIf('runTest'.startswith(loader.testMethodPrefix))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
183
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
184 suite = loader.loadTestsFromTestCase(Foo)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
185 self.failUnless(isinstance(suite, loader.suiteClass))
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
186 self.assertEqual(list(suite), [Foo('runTest')])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
187
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
188 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
189 ### /Tests for TestLoader.loadTestsFromTestCase
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
190
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
191 ### Tests for TestLoader.loadTestsFromModule
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
192 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
193
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
194 # "This method searches `module` for classes derived from TestCase"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
195 def test_loadTestsFromModule__TestCase_subclass(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
196 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
197 class MyTestCase(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
198 def test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
199 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
200 m.testcase_1 = MyTestCase
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
201
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
202 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
203 suite = loader.loadTestsFromModule(m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
204 self.failUnless(isinstance(suite, loader.suiteClass))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
205
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
206 expected = [loader.suiteClass([MyTestCase('test')])]
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
207 self.assertEqual(list(suite), expected)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
208
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
209 # "This method searches `module` for classes derived from TestCase"
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
210 #
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
211 # What happens if no tests are found (no TestCase instances)?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
212 def test_loadTestsFromModule__no_TestCase_instances(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
213 m = types.ModuleType('m')
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
214
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
215 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
216 suite = loader.loadTestsFromModule(m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
217 self.failUnless(isinstance(suite, loader.suiteClass))
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
218 self.assertEqual(list(suite), [])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
219
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
220 # "This method searches `module` for classes derived from TestCase"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
221 #
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
222 # What happens if no tests are found (TestCases instances, but no tests)?
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
223 def test_loadTestsFromModule__no_TestCase_tests(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
224 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
225 class MyTestCase(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
226 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
227 m.testcase_1 = MyTestCase
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
228
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
229 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
230 suite = loader.loadTestsFromModule(m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
231 self.failUnless(isinstance(suite, loader.suiteClass))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
232
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
233 self.assertEqual(list(suite), [loader.suiteClass()])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
234
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
235 # "This method searches `module` for classes derived from TestCase"s
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
236 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
237 # What happens if loadTestsFromModule() is given something other
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
238 # than a module?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
239 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
240 # XXX Currently, it succeeds anyway. This flexibility
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
241 # should either be documented or loadTestsFromModule() should
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
242 # raise a TypeError
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
243 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
244 # XXX Certain people are using this behaviour. We'll add a test for it
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
245 def test_loadTestsFromModule__not_a_module(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
246 class MyTestCase(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
247 def test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
248 pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
249
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
250 class NotAModule(object):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
251 test_2 = MyTestCase
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
252
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
253 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
254 suite = loader.loadTestsFromModule(NotAModule)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
255
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
256 reference = [unittest.TestSuite([MyTestCase('test')])]
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
257 self.assertEqual(list(suite), reference)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
258
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
259 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
260 ### /Tests for TestLoader.loadTestsFromModule()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
261
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
262 ### Tests for TestLoader.loadTestsFromName()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
263 ################################################################
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
264
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
265 # "The specifier name is a ``dotted name'' that may resolve either to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
266 # a module, a test case class, a TestSuite instance, a test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
267 # within a test case class, or a callable object which returns a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
268 # TestCase or TestSuite instance."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
269 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
270 # Is ValueError raised in response to an empty name?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
271 def test_loadTestsFromName__empty_name(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
272 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
273
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
274 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
275 loader.loadTestsFromName('')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
276 except ValueError, e:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
277 self.assertEqual(str(e), "Empty module name")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
278 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
279 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
280
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
281 # "The specifier name is a ``dotted name'' that may resolve either to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
282 # a module, a test case class, a TestSuite instance, a test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
283 # within a test case class, or a callable object which returns a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
284 # TestCase or TestSuite instance."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
285 #
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
286 # What happens when the name contains invalid characters?
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
287 def test_loadTestsFromName__malformed_name(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
288 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
289
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
290 # XXX Should this raise ValueError or ImportError?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
291 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
292 loader.loadTestsFromName('abc () //')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
293 except ValueError:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
294 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
295 except ImportError:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
296 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
297 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
298 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
299
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
300 # "The specifier name is a ``dotted name'' that may resolve ... to a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
301 # module"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
302 #
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
303 # What happens when a module by that name can't be found?
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
304 def test_loadTestsFromName__unknown_module_name(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
305 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
306
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
307 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
308 loader.loadTestsFromName('sdasfasfasdf')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
309 except ImportError, e:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
310 self.assertEqual(str(e), "No module named sdasfasfasdf")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
311 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
312 self.fail("TestLoader.loadTestsFromName failed to raise ImportError")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
313
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
314 # "The specifier name is a ``dotted name'' that may resolve either to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
315 # a module, a test case class, a TestSuite instance, a test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
316 # within a test case class, or a callable object which returns a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
317 # TestCase or TestSuite instance."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
318 #
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
319 # What happens when the module is found, but the attribute can't?
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
320 def test_loadTestsFromName__unknown_attr_name(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
321 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
322
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
323 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
324 loader.loadTestsFromName('unittest.sdasfasfasdf')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
325 except AttributeError, e:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
326 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
327 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
328 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
329
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
330 # "The specifier name is a ``dotted name'' that may resolve either to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
331 # a module, a test case class, a TestSuite instance, a test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
332 # within a test case class, or a callable object which returns a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
333 # TestCase or TestSuite instance."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
334 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
335 # What happens when we provide the module, but the attribute can't be
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
336 # found?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
337 def test_loadTestsFromName__relative_unknown_name(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
338 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
339
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
340 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
341 loader.loadTestsFromName('sdasfasfasdf', unittest)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
342 except AttributeError, e:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
343 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
344 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
345 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
346
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
347 # "The specifier name is a ``dotted name'' that may resolve either to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
348 # a module, a test case class, a TestSuite instance, a test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
349 # within a test case class, or a callable object which returns a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
350 # TestCase or TestSuite instance."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
351 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
352 # "The method optionally resolves name relative to the given module"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
353 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
354 # Does loadTestsFromName raise ValueError when passed an empty
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
355 # name relative to a provided module?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
356 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
357 # XXX Should probably raise a ValueError instead of an AttributeError
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
358 def test_loadTestsFromName__relative_empty_name(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
359 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
360
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
361 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
362 loader.loadTestsFromName('', unittest)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
363 except AttributeError, e:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
364 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
365 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
366 self.fail("Failed to raise AttributeError")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
367
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
368 # "The specifier name is a ``dotted name'' that may resolve either to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
369 # a module, a test case class, a TestSuite instance, a test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
370 # within a test case class, or a callable object which returns a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
371 # TestCase or TestSuite instance."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
372 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
373 # "The method optionally resolves name relative to the given module"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
374 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
375 # What happens when an impossible name is given, relative to the provided
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
376 # `module`?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
377 def test_loadTestsFromName__relative_malformed_name(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
378 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
379
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
380 # XXX Should this raise AttributeError or ValueError?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
381 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
382 loader.loadTestsFromName('abc () //', unittest)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
383 except ValueError:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
384 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
385 except AttributeError:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
386 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
387 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
388 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
389
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
390 # "The method optionally resolves name relative to the given module"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
391 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
392 # Does loadTestsFromName raise TypeError when the `module` argument
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
393 # isn't a module object?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
394 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
395 # XXX Accepts the not-a-module object, ignorning the object's type
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
396 # This should raise an exception or the method name should be changed
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
397 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
398 # XXX Some people are relying on this, so keep it for now
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
399 def test_loadTestsFromName__relative_not_a_module(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
400 class MyTestCase(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
401 def test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
402 pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
403
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
404 class NotAModule(object):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
405 test_2 = MyTestCase
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
406
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
407 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
408 suite = loader.loadTestsFromName('test_2', NotAModule)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
409
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
410 reference = [MyTestCase('test')]
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
411 self.assertEqual(list(suite), reference)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
412
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
413 # "The specifier name is a ``dotted name'' that may resolve either to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
414 # a module, a test case class, a TestSuite instance, a test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
415 # within a test case class, or a callable object which returns a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
416 # TestCase or TestSuite instance."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
417 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
418 # Does it raise an exception if the name resolves to an invalid
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
419 # object?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
420 def test_loadTestsFromName__relative_bad_object(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
421 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
422 m.testcase_1 = object()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
423
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
424 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
425 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
426 loader.loadTestsFromName('testcase_1', m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
427 except TypeError:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
428 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
429 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
430 self.fail("Should have raised TypeError")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
431
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
432 # "The specifier name is a ``dotted name'' that may
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
433 # resolve either to ... a test case class"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
434 def test_loadTestsFromName__relative_TestCase_subclass(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
435 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
436 class MyTestCase(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
437 def test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
438 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
439 m.testcase_1 = MyTestCase
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
440
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
441 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
442 suite = loader.loadTestsFromName('testcase_1', m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
443 self.failUnless(isinstance(suite, loader.suiteClass))
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
444 self.assertEqual(list(suite), [MyTestCase('test')])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
445
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
446 # "The specifier name is a ``dotted name'' that may resolve either to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
447 # a module, a test case class, a TestSuite instance, a test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
448 # within a test case class, or a callable object which returns a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
449 # TestCase or TestSuite instance."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
450 def test_loadTestsFromName__relative_TestSuite(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
451 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
452 class MyTestCase(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
453 def test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
454 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
455 m.testsuite = unittest.TestSuite([MyTestCase('test')])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
456
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
457 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
458 suite = loader.loadTestsFromName('testsuite', m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
459 self.failUnless(isinstance(suite, loader.suiteClass))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
460
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
461 self.assertEqual(list(suite), [MyTestCase('test')])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
462
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
463 # "The specifier name is a ``dotted name'' that may resolve ... to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
464 # ... a test method within a test case class"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
465 def test_loadTestsFromName__relative_testmethod(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
466 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
467 class MyTestCase(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
468 def test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
469 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
470 m.testcase_1 = MyTestCase
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
471
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
472 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
473 suite = loader.loadTestsFromName('testcase_1.test', m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
474 self.failUnless(isinstance(suite, loader.suiteClass))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
475
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
476 self.assertEqual(list(suite), [MyTestCase('test')])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
477
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
478 # "The specifier name is a ``dotted name'' that may resolve either to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
479 # a module, a test case class, a TestSuite instance, a test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
480 # within a test case class, or a callable object which returns a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
481 # TestCase or TestSuite instance."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
482 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
483 # Does loadTestsFromName() raise the proper exception when trying to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
484 # resolve "a test method within a test case class" that doesn't exist
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
485 # for the given name (relative to a provided module)?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
486 def test_loadTestsFromName__relative_invalid_testmethod(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
487 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
488 class MyTestCase(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
489 def test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
490 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
491 m.testcase_1 = MyTestCase
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
492
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
493 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
494 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
495 loader.loadTestsFromName('testcase_1.testfoo', m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
496 except AttributeError, e:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
497 self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
498 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
499 self.fail("Failed to raise AttributeError")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
500
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
501 # "The specifier name is a ``dotted name'' that may resolve ... to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
502 # ... a callable object which returns a ... TestSuite instance"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
503 def test_loadTestsFromName__callable__TestSuite(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
504 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
505 testcase_1 = unittest.FunctionTestCase(lambda: None)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
506 testcase_2 = unittest.FunctionTestCase(lambda: None)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
507 def return_TestSuite():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
508 return unittest.TestSuite([testcase_1, testcase_2])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
509 m.return_TestSuite = return_TestSuite
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
510
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
511 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
512 suite = loader.loadTestsFromName('return_TestSuite', m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
513 self.failUnless(isinstance(suite, loader.suiteClass))
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
514 self.assertEqual(list(suite), [testcase_1, testcase_2])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
515
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
516 # "The specifier name is a ``dotted name'' that may resolve ... to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
517 # ... a callable object which returns a TestCase ... instance"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
518 def test_loadTestsFromName__callable__TestCase_instance(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
519 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
520 testcase_1 = unittest.FunctionTestCase(lambda: None)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
521 def return_TestCase():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
522 return testcase_1
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
523 m.return_TestCase = return_TestCase
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
524
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
525 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
526 suite = loader.loadTestsFromName('return_TestCase', m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
527 self.failUnless(isinstance(suite, loader.suiteClass))
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
528 self.assertEqual(list(suite), [testcase_1])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
529
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
530 # "The specifier name is a ``dotted name'' that may resolve ... to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
531 # ... a callable object which returns a TestCase or TestSuite instance"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
532 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
533 # What happens if the callable returns something else?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
534 def test_loadTestsFromName__callable__wrong_type(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
535 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
536 def return_wrong():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
537 return 6
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
538 m.return_wrong = return_wrong
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
539
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
540 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
541 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
542 suite = loader.loadTestsFromName('return_wrong', m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
543 except TypeError:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
544 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
545 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
546 self.fail("TestLoader.loadTestsFromName failed to raise TypeError")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
547
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
548 # "The specifier can refer to modules and packages which have not been
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
549 # imported; they will be imported as a side-effect"
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
550 def test_loadTestsFromName__module_not_loaded(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
551 # We're going to try to load this module as a side-effect, so it
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
552 # better not be loaded before we try.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
553 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
554 # Why pick audioop? Google shows it isn't used very often, so there's
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
555 # a good chance that it won't be imported when this test is run
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
556 module_name = 'audioop'
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
557
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
558 import sys
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
559 if module_name in sys.modules:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
560 del sys.modules[module_name]
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
561
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
562 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
563 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
564 suite = loader.loadTestsFromName(module_name)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
565
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
566 self.failUnless(isinstance(suite, loader.suiteClass))
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
567 self.assertEqual(list(suite), [])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
568
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
569 # audioop should now be loaded, thanks to loadTestsFromName()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
570 self.failUnless(module_name in sys.modules)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
571 finally:
41589
37d82bee7a16 Fix those parts in the testsuite that assumed that sys.maxint would cause overflow on x64. Now the testsuite is well behaved on that platform.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41133
diff changeset
572 if module_name in sys.modules:
37d82bee7a16 Fix those parts in the testsuite that assumed that sys.maxint would cause overflow on x64. Now the testsuite is well behaved on that platform.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41133
diff changeset
573 del sys.modules[module_name]
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
574
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
575 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
576 ### Tests for TestLoader.loadTestsFromName()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
577
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
578 ### Tests for TestLoader.loadTestsFromNames()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
579 ################################################################
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
580
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
581 # "Similar to loadTestsFromName(), but takes a sequence of names rather
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
582 # than a single name."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
583 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
584 # What happens if that sequence of names is empty?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
585 def test_loadTestsFromNames__empty_name_list(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
586 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
587
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
588 suite = loader.loadTestsFromNames([])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
589 self.failUnless(isinstance(suite, loader.suiteClass))
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
590 self.assertEqual(list(suite), [])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
591
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
592 # "Similar to loadTestsFromName(), but takes a sequence of names rather
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
593 # than a single name."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
594 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
595 # "The method optionally resolves name relative to the given module"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
596 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
597 # What happens if that sequence of names is empty?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
598 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
599 # XXX Should this raise a ValueError or just return an empty TestSuite?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
600 def test_loadTestsFromNames__relative_empty_name_list(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
601 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
602
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
603 suite = loader.loadTestsFromNames([], unittest)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
604 self.failUnless(isinstance(suite, loader.suiteClass))
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
605 self.assertEqual(list(suite), [])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
606
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
607 # "The specifier name is a ``dotted name'' that may resolve either to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
608 # a module, a test case class, a TestSuite instance, a test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
609 # within a test case class, or a callable object which returns a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
610 # TestCase or TestSuite instance."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
611 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
612 # Is ValueError raised in response to an empty name?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
613 def test_loadTestsFromNames__empty_name(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
614 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
615
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
616 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
617 loader.loadTestsFromNames([''])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
618 except ValueError, e:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
619 self.assertEqual(str(e), "Empty module name")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
620 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
621 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
622
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
623 # "The specifier name is a ``dotted name'' that may resolve either to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
624 # a module, a test case class, a TestSuite instance, a test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
625 # within a test case class, or a callable object which returns a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
626 # TestCase or TestSuite instance."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
627 #
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
628 # What happens when presented with an impossible module name?
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
629 def test_loadTestsFromNames__malformed_name(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
630 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
631
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
632 # XXX Should this raise ValueError or ImportError?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
633 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
634 loader.loadTestsFromNames(['abc () //'])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
635 except ValueError:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
636 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
637 except ImportError:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
638 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
639 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
640 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
641
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
642 # "The specifier name is a ``dotted name'' that may resolve either to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
643 # a module, a test case class, a TestSuite instance, a test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
644 # within a test case class, or a callable object which returns a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
645 # TestCase or TestSuite instance."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
646 #
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
647 # What happens when no module can be found for the given name?
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
648 def test_loadTestsFromNames__unknown_module_name(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
649 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
650
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
651 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
652 loader.loadTestsFromNames(['sdasfasfasdf'])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
653 except ImportError, e:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
654 self.assertEqual(str(e), "No module named sdasfasfasdf")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
655 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
656 self.fail("TestLoader.loadTestsFromNames failed to raise ImportError")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
657
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
658 # "The specifier name is a ``dotted name'' that may resolve either to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
659 # a module, a test case class, a TestSuite instance, a test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
660 # within a test case class, or a callable object which returns a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
661 # TestCase or TestSuite instance."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
662 #
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
663 # What happens when the module can be found, but not the attribute?
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
664 def test_loadTestsFromNames__unknown_attr_name(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
665 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
666
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
667 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
668 loader.loadTestsFromNames(['unittest.sdasfasfasdf', 'unittest'])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
669 except AttributeError, e:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
670 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
671 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
672 self.fail("TestLoader.loadTestsFromNames failed to raise AttributeError")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
673
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
674 # "The specifier name is a ``dotted name'' that may resolve either to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
675 # a module, a test case class, a TestSuite instance, a test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
676 # within a test case class, or a callable object which returns a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
677 # TestCase or TestSuite instance."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
678 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
679 # "The method optionally resolves name relative to the given module"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
680 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
681 # What happens when given an unknown attribute on a specified `module`
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
682 # argument?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
683 def test_loadTestsFromNames__unknown_name_relative_1(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
684 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
685
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
686 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
687 loader.loadTestsFromNames(['sdasfasfasdf'], unittest)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
688 except AttributeError, e:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
689 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
690 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
691 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
692
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
693 # "The specifier name is a ``dotted name'' that may resolve either to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
694 # a module, a test case class, a TestSuite instance, a test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
695 # within a test case class, or a callable object which returns a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
696 # TestCase or TestSuite instance."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
697 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
698 # "The method optionally resolves name relative to the given module"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
699 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
700 # Do unknown attributes (relative to a provided module) still raise an
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
701 # exception even in the presence of valid attribute names?
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
702 def test_loadTestsFromNames__unknown_name_relative_2(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
703 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
704
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
705 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
706 loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
707 except AttributeError, e:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
708 self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
709 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
710 self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
711
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
712 # "The specifier name is a ``dotted name'' that may resolve either to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
713 # a module, a test case class, a TestSuite instance, a test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
714 # within a test case class, or a callable object which returns a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
715 # TestCase or TestSuite instance."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
716 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
717 # "The method optionally resolves name relative to the given module"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
718 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
719 # What happens when faced with the empty string?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
720 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
721 # XXX This currently raises AttributeError, though ValueError is probably
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
722 # more appropriate
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
723 def test_loadTestsFromNames__relative_empty_name(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
724 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
725
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
726 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
727 loader.loadTestsFromNames([''], unittest)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
728 except AttributeError:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
729 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
730 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
731 self.fail("Failed to raise ValueError")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
732
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
733 # "The specifier name is a ``dotted name'' that may resolve either to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
734 # a module, a test case class, a TestSuite instance, a test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
735 # within a test case class, or a callable object which returns a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
736 # TestCase or TestSuite instance."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
737 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
738 # "The method optionally resolves name relative to the given module"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
739 #
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
740 # What happens when presented with an impossible attribute name?
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
741 def test_loadTestsFromNames__relative_malformed_name(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
742 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
743
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
744 # XXX Should this raise AttributeError or ValueError?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
745 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
746 loader.loadTestsFromNames(['abc () //'], unittest)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
747 except AttributeError:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
748 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
749 except ValueError:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
750 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
751 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
752 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
753
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
754 # "The method optionally resolves name relative to the given module"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
755 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
756 # Does loadTestsFromNames() make sure the provided `module` is in fact
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
757 # a module?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
758 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
759 # XXX This validation is currently not done. This flexibility should
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
760 # either be documented or a TypeError should be raised.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
761 def test_loadTestsFromNames__relative_not_a_module(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
762 class MyTestCase(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
763 def test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
764 pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
765
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
766 class NotAModule(object):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
767 test_2 = MyTestCase
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
768
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
769 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
770 suite = loader.loadTestsFromNames(['test_2'], NotAModule)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
771
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
772 reference = [unittest.TestSuite([MyTestCase('test')])]
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
773 self.assertEqual(list(suite), reference)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
774
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
775 # "The specifier name is a ``dotted name'' that may resolve either to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
776 # a module, a test case class, a TestSuite instance, a test method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
777 # within a test case class, or a callable object which returns a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
778 # TestCase or TestSuite instance."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
779 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
780 # Does it raise an exception if the name resolves to an invalid
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
781 # object?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
782 def test_loadTestsFromNames__relative_bad_object(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
783 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
784 m.testcase_1 = object()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
785
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
786 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
787 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
788 loader.loadTestsFromNames(['testcase_1'], m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
789 except TypeError:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
790 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
791 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
792 self.fail("Should have raised TypeError")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
793
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
794 # "The specifier name is a ``dotted name'' that may resolve ... to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
795 # ... a test case class"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
796 def test_loadTestsFromNames__relative_TestCase_subclass(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
797 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
798 class MyTestCase(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
799 def test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
800 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
801 m.testcase_1 = MyTestCase
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
802
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
803 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
804 suite = loader.loadTestsFromNames(['testcase_1'], m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
805 self.failUnless(isinstance(suite, loader.suiteClass))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
806
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
807 expected = loader.suiteClass([MyTestCase('test')])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
808 self.assertEqual(list(suite), [expected])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
809
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
810 # "The specifier name is a ``dotted name'' that may resolve ... to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
811 # ... a TestSuite instance"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
812 def test_loadTestsFromNames__relative_TestSuite(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
813 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
814 class MyTestCase(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
815 def test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
816 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
817 m.testsuite = unittest.TestSuite([MyTestCase('test')])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
818
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
819 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
820 suite = loader.loadTestsFromNames(['testsuite'], m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
821 self.failUnless(isinstance(suite, loader.suiteClass))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
822
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
823 self.assertEqual(list(suite), [m.testsuite])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
824
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
825 # "The specifier name is a ``dotted name'' that may resolve ... to ... a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
826 # test method within a test case class"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
827 def test_loadTestsFromNames__relative_testmethod(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
828 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
829 class MyTestCase(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
830 def test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
831 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
832 m.testcase_1 = MyTestCase
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
833
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
834 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
835 suite = loader.loadTestsFromNames(['testcase_1.test'], m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
836 self.failUnless(isinstance(suite, loader.suiteClass))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
837
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
838 ref_suite = unittest.TestSuite([MyTestCase('test')])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
839 self.assertEqual(list(suite), [ref_suite])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
840
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
841 # "The specifier name is a ``dotted name'' that may resolve ... to ... a
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
842 # test method within a test case class"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
843 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
844 # Does the method gracefully handle names that initially look like they
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
845 # resolve to "a test method within a test case class" but don't?
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
846 def test_loadTestsFromNames__relative_invalid_testmethod(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
847 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
848 class MyTestCase(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
849 def test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
850 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
851 m.testcase_1 = MyTestCase
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
852
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
853 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
854 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
855 loader.loadTestsFromNames(['testcase_1.testfoo'], m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
856 except AttributeError, e:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
857 self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
858 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
859 self.fail("Failed to raise AttributeError")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
860
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
861 # "The specifier name is a ``dotted name'' that may resolve ... to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
862 # ... a callable object which returns a ... TestSuite instance"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
863 def test_loadTestsFromNames__callable__TestSuite(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
864 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
865 testcase_1 = unittest.FunctionTestCase(lambda: None)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
866 testcase_2 = unittest.FunctionTestCase(lambda: None)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
867 def return_TestSuite():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
868 return unittest.TestSuite([testcase_1, testcase_2])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
869 m.return_TestSuite = return_TestSuite
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
870
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
871 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
872 suite = loader.loadTestsFromNames(['return_TestSuite'], m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
873 self.failUnless(isinstance(suite, loader.suiteClass))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
874
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
875 expected = unittest.TestSuite([testcase_1, testcase_2])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
876 self.assertEqual(list(suite), [expected])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
877
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
878 # "The specifier name is a ``dotted name'' that may resolve ... to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
879 # ... a callable object which returns a TestCase ... instance"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
880 def test_loadTestsFromNames__callable__TestCase_instance(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
881 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
882 testcase_1 = unittest.FunctionTestCase(lambda: None)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
883 def return_TestCase():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
884 return testcase_1
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
885 m.return_TestCase = return_TestCase
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
886
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
887 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
888 suite = loader.loadTestsFromNames(['return_TestCase'], m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
889 self.failUnless(isinstance(suite, loader.suiteClass))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
890
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
891 ref_suite = unittest.TestSuite([testcase_1])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
892 self.assertEqual(list(suite), [ref_suite])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
893
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
894 # "The specifier name is a ``dotted name'' that may resolve ... to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
895 # ... a callable object which returns a TestCase or TestSuite instance"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
896 #
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
897 # Are staticmethods handled correctly?
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
898 def test_loadTestsFromNames__callable__call_staticmethod(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
899 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
900 class Test1(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
901 def test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
902 pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
903
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
904 testcase_1 = Test1('test')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
905 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
906 @staticmethod
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
907 def foo():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
908 return testcase_1
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
909 m.Foo = Foo
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
910
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
911 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
912 suite = loader.loadTestsFromNames(['Foo.foo'], m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
913 self.failUnless(isinstance(suite, loader.suiteClass))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
914
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
915 ref_suite = unittest.TestSuite([testcase_1])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
916 self.assertEqual(list(suite), [ref_suite])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
917
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
918 # "The specifier name is a ``dotted name'' that may resolve ... to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
919 # ... a callable object which returns a TestCase or TestSuite instance"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
920 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
921 # What happens when the callable returns something else?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
922 def test_loadTestsFromNames__callable__wrong_type(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
923 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
924 def return_wrong():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
925 return 6
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
926 m.return_wrong = return_wrong
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
927
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
928 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
929 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
930 suite = loader.loadTestsFromNames(['return_wrong'], m)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
931 except TypeError:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
932 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
933 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
934 self.fail("TestLoader.loadTestsFromNames failed to raise TypeError")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
935
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
936 # "The specifier can refer to modules and packages which have not been
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
937 # imported; they will be imported as a side-effect"
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
938 def test_loadTestsFromNames__module_not_loaded(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
939 # We're going to try to load this module as a side-effect, so it
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
940 # better not be loaded before we try.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
941 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
942 # Why pick audioop? Google shows it isn't used very often, so there's
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
943 # a good chance that it won't be imported when this test is run
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
944 module_name = 'audioop'
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
945
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
946 import sys
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
947 if module_name in sys.modules:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
948 del sys.modules[module_name]
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
949
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
950 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
951 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
952 suite = loader.loadTestsFromNames([module_name])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
953
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
954 self.failUnless(isinstance(suite, loader.suiteClass))
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
955 self.assertEqual(list(suite), [unittest.TestSuite()])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
956
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
957 # audioop should now be loaded, thanks to loadTestsFromName()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
958 self.failUnless(module_name in sys.modules)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
959 finally:
41589
37d82bee7a16 Fix those parts in the testsuite that assumed that sys.maxint would cause overflow on x64. Now the testsuite is well behaved on that platform.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41133
diff changeset
960 if module_name in sys.modules:
37d82bee7a16 Fix those parts in the testsuite that assumed that sys.maxint would cause overflow on x64. Now the testsuite is well behaved on that platform.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41133
diff changeset
961 del sys.modules[module_name]
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
962
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
963 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
964 ### /Tests for TestLoader.loadTestsFromNames()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
965
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
966 ### Tests for TestLoader.getTestCaseNames()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
967 ################################################################
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
968
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
969 # "Return a sorted sequence of method names found within testCaseClass"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
970 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
971 # Test.foobar is defined to make sure getTestCaseNames() respects
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
972 # loader.testMethodPrefix
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
973 def test_getTestCaseNames(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
974 class Test(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
975 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
976 def test_2(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
977 def foobar(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
978
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
979 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
980
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
981 self.assertEqual(loader.getTestCaseNames(Test), ['test_1', 'test_2'])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
982
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
983 # "Return a sorted sequence of method names found within testCaseClass"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
984 #
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
985 # Does getTestCaseNames() behave appropriately if no tests are found?
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
986 def test_getTestCaseNames__no_tests(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
987 class Test(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
988 def foobar(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
989
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
990 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
991
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
992 self.assertEqual(loader.getTestCaseNames(Test), [])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
993
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
994 # "Return a sorted sequence of method names found within testCaseClass"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
995 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
996 # Are not-TestCases handled gracefully?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
997 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
998 # XXX This should raise a TypeError, not return a list
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
999 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1000 # XXX It's too late in the 2.5 release cycle to fix this, but it should
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1001 # probably be revisited for 2.6
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1002 def test_getTestCaseNames__not_a_TestCase(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1003 class BadCase(int):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1004 def test_foo(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1005 pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1006
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1007 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1008 names = loader.getTestCaseNames(BadCase)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1009
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1010 self.assertEqual(names, ['test_foo'])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1011
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1012 # "Return a sorted sequence of method names found within testCaseClass"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1013 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1014 # Make sure inherited names are handled.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1015 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1016 # TestP.foobar is defined to make sure getTestCaseNames() respects
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1017 # loader.testMethodPrefix
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1018 def test_getTestCaseNames__inheritance(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1019 class TestP(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1020 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1021 def test_2(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1022 def foobar(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1023
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1024 class TestC(TestP):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1025 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1026 def test_3(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1027
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1028 loader = unittest.TestLoader()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1029
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1030 names = ['test_1', 'test_2', 'test_3']
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1031 self.assertEqual(loader.getTestCaseNames(TestC), names)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1032
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1033 ################################################################
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1034 ### /Tests for TestLoader.getTestCaseNames()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1035
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1036 ### Tests for TestLoader.testMethodPrefix
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1037 ################################################################
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1038
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1039 # "String giving the prefix of method names which will be interpreted as
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1040 # test methods"
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1041 #
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1042 # Implicit in the documentation is that testMethodPrefix is respected by
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1043 # all loadTestsFrom* methods.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1044 def test_testMethodPrefix__loadTestsFromTestCase(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1045 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1046 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1047 def test_2(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1048 def foo_bar(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1049
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1050 tests_1 = unittest.TestSuite([Foo('foo_bar')])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1051 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1052
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1053 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1054 loader.testMethodPrefix = 'foo'
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1055 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_1)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1056
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1057 loader.testMethodPrefix = 'test'
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1058 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_2)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1059
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1060 # "String giving the prefix of method names which will be interpreted as
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1061 # test methods"
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1062 #
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1063 # Implicit in the documentation is that testMethodPrefix is respected by
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1064 # all loadTestsFrom* methods.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1065 def test_testMethodPrefix__loadTestsFromModule(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
1066 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1067 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1068 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1069 def test_2(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1070 def foo_bar(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1071 m.Foo = Foo
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1072
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1073 tests_1 = [unittest.TestSuite([Foo('foo_bar')])]
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1074 tests_2 = [unittest.TestSuite([Foo('test_1'), Foo('test_2')])]
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1075
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1076 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1077 loader.testMethodPrefix = 'foo'
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1078 self.assertEqual(list(loader.loadTestsFromModule(m)), tests_1)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1079
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1080 loader.testMethodPrefix = 'test'
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1081 self.assertEqual(list(loader.loadTestsFromModule(m)), tests_2)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1082
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1083 # "String giving the prefix of method names which will be interpreted as
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1084 # test methods"
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1085 #
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1086 # Implicit in the documentation is that testMethodPrefix is respected by
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1087 # all loadTestsFrom* methods.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1088 def test_testMethodPrefix__loadTestsFromName(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
1089 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1090 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1091 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1092 def test_2(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1093 def foo_bar(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1094 m.Foo = Foo
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1095
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1096 tests_1 = unittest.TestSuite([Foo('foo_bar')])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1097 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1098
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1099 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1100 loader.testMethodPrefix = 'foo'
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1101 self.assertEqual(loader.loadTestsFromName('Foo', m), tests_1)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1102
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1103 loader.testMethodPrefix = 'test'
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1104 self.assertEqual(loader.loadTestsFromName('Foo', m), tests_2)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1105
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1106 # "String giving the prefix of method names which will be interpreted as
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1107 # test methods"
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1108 #
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1109 # Implicit in the documentation is that testMethodPrefix is respected by
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1110 # all loadTestsFrom* methods.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1111 def test_testMethodPrefix__loadTestsFromNames(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
1112 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1113 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1114 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1115 def test_2(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1116 def foo_bar(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1117 m.Foo = Foo
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1118
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1119 tests_1 = unittest.TestSuite([unittest.TestSuite([Foo('foo_bar')])])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1120 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1121 tests_2 = unittest.TestSuite([tests_2])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1122
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1123 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1124 loader.testMethodPrefix = 'foo'
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1125 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_1)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1126
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1127 loader.testMethodPrefix = 'test'
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1128 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_2)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1129
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1130 # "The default value is 'test'"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1131 def test_testMethodPrefix__default_value(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1132 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1133 self.failUnless(loader.testMethodPrefix == 'test')
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1134
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1135 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1136 ### /Tests for TestLoader.testMethodPrefix
33314
2f52df173952 Added an __iter__ method for test suites.
Jim Fulton <jim@zope.com>
parents:
diff changeset
1137
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1138 ### Tests for TestLoader.sortTestMethodsUsing
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1139 ################################################################
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1140
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1141 # "Function to be used to compare method names when sorting them in
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1142 # getTestCaseNames() and all the loadTestsFromX() methods"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1143 def test_sortTestMethodsUsing__loadTestsFromTestCase(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1144 def reversed_cmp(x, y):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1145 return -cmp(x, y)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1146
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1147 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1148 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1149 def test_2(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1150
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1151 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1152 loader.sortTestMethodsUsing = reversed_cmp
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1153
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1154 tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1155 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1156
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1157 # "Function to be used to compare method names when sorting them in
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1158 # getTestCaseNames() and all the loadTestsFromX() methods"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1159 def test_sortTestMethodsUsing__loadTestsFromModule(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1160 def reversed_cmp(x, y):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1161 return -cmp(x, y)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1162
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
1163 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1164 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1165 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1166 def test_2(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1167 m.Foo = Foo
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1168
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1169 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1170 loader.sortTestMethodsUsing = reversed_cmp
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1171
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1172 tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1173 self.assertEqual(list(loader.loadTestsFromModule(m)), tests)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1174
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1175 # "Function to be used to compare method names when sorting them in
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1176 # getTestCaseNames() and all the loadTestsFromX() methods"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1177 def test_sortTestMethodsUsing__loadTestsFromName(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1178 def reversed_cmp(x, y):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1179 return -cmp(x, y)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1180
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
1181 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1182 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1183 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1184 def test_2(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1185 m.Foo = Foo
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1186
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1187 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1188 loader.sortTestMethodsUsing = reversed_cmp
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1189
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1190 tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1191 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1192
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1193 # "Function to be used to compare method names when sorting them in
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1194 # getTestCaseNames() and all the loadTestsFromX() methods"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1195 def test_sortTestMethodsUsing__loadTestsFromNames(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1196 def reversed_cmp(x, y):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1197 return -cmp(x, y)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1198
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
1199 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1200 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1201 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1202 def test_2(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1203 m.Foo = Foo
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1204
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1205 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1206 loader.sortTestMethodsUsing = reversed_cmp
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1207
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1208 tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1209 self.assertEqual(list(loader.loadTestsFromNames(['Foo'], m)), tests)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1210
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1211 # "Function to be used to compare method names when sorting them in
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1212 # getTestCaseNames()"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1213 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1214 # Does it actually affect getTestCaseNames()?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1215 def test_sortTestMethodsUsing__getTestCaseNames(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1216 def reversed_cmp(x, y):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1217 return -cmp(x, y)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1218
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1219 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1220 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1221 def test_2(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1222
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1223 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1224 loader.sortTestMethodsUsing = reversed_cmp
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1225
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1226 test_names = ['test_2', 'test_1']
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1227 self.assertEqual(loader.getTestCaseNames(Foo), test_names)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1228
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1229 # "The default value is the built-in cmp() function"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1230 def test_sortTestMethodsUsing__default_value(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1231 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1232 self.failUnless(loader.sortTestMethodsUsing is cmp)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1233
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1234 # "it can be set to None to disable the sort."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1235 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1236 # XXX How is this different from reassigning cmp? Are the tests returned
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1237 # in a random order or something? This behaviour should die
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1238 def test_sortTestMethodsUsing__None(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1239 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1240 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1241 def test_2(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1242
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1243 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1244 loader.sortTestMethodsUsing = None
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1245
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1246 test_names = ['test_2', 'test_1']
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1247 self.assertEqual(set(loader.getTestCaseNames(Foo)), set(test_names))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1248
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1249 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1250 ### /Tests for TestLoader.sortTestMethodsUsing
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1251
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1252 ### Tests for TestLoader.suiteClass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1253 ################################################################
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1254
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1255 # "Callable object that constructs a test suite from a list of tests."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1256 def test_suiteClass__loadTestsFromTestCase(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1257 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1258 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1259 def test_2(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1260 def foo_bar(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1261
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1262 tests = [Foo('test_1'), Foo('test_2')]
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1263
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1264 loader = unittest.TestLoader()
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
1265 loader.classSuiteClass = MyClassSuite
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1266 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1267
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1268 # It is implicit in the documentation for TestLoader.suiteClass that
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1269 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1270 def test_suiteClass__loadTestsFromModule(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
1271 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1272 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1273 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1274 def test_2(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1275 def foo_bar(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1276 m.Foo = Foo
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1277
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
1278 tests = [unittest.ClassTestSuite([Foo('test_1'), Foo('test_2')], Foo)]
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1279
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1280 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1281 loader.suiteClass = list
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1282 self.assertEqual(loader.loadTestsFromModule(m), tests)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1283
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1284 # It is implicit in the documentation for TestLoader.suiteClass that
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1285 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1286 def test_suiteClass__loadTestsFromName(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
1287 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1288 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1289 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1290 def test_2(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1291 def foo_bar(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1292 m.Foo = Foo
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1293
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1294 tests = [Foo('test_1'), Foo('test_2')]
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1295
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1296 loader = unittest.TestLoader()
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
1297 loader.classSuiteClass = MyClassSuite
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1298 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1299
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1300 # It is implicit in the documentation for TestLoader.suiteClass that
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1301 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1302 def test_suiteClass__loadTestsFromNames(self):
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 41589
diff changeset
1303 m = types.ModuleType('m')
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1304 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1305 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1306 def test_2(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1307 def foo_bar(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1308 m.Foo = Foo
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1309
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
1310 tests = [unittest.ClassTestSuite([Foo('test_1'), Foo('test_2')], Foo)]
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1311
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1312 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1313 loader.suiteClass = list
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1314 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1315
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1316 # "The default value is the TestSuite class"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1317 def test_suiteClass__default_value(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1318 loader = unittest.TestLoader()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1319 self.failUnless(loader.suiteClass is unittest.TestSuite)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1320
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1321 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1322 ### /Tests for TestLoader.suiteClass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1323
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1324 ### Support code for Test_TestSuite
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1325 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1326
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1327 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1328 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1329 def test_2(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1330 def test_3(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1331 def runTest(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1332
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1333 def _mk_TestSuite(*names):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1334 return unittest.TestSuite(Foo(n) for n in names)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1335
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1336 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1337 ### /Support code for Test_TestSuite
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1338
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1339 class Test_TestSuite(TestCase, TestEquality):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1340
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1341 ### Set up attributes needed by inherited tests
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1342 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1343
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1344 # Used by TestEquality.test_eq
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1345 eq_pairs = [(unittest.TestSuite(), unittest.TestSuite())
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1346 ,(unittest.TestSuite(), unittest.TestSuite([]))
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1347 ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_1'))]
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1348
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1349 # Used by TestEquality.test_ne
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1350 ne_pairs = [(unittest.TestSuite(), _mk_TestSuite('test_1'))
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1351 ,(unittest.TestSuite([]), _mk_TestSuite('test_1'))
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1352 ,(_mk_TestSuite('test_1', 'test_2'), _mk_TestSuite('test_1', 'test_3'))
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1353 ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_2'))]
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1354
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1355 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1356 ### /Set up attributes needed by inherited tests
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1357
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1358 ### Tests for TestSuite.__init__
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1359 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1360
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1361 # "class TestSuite([tests])"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1362 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1363 # The tests iterable should be optional
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1364 def test_init__tests_optional(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1365 suite = unittest.TestSuite()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1366
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1367 self.assertEqual(suite.countTestCases(), 0)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1368
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1369 # "class TestSuite([tests])"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1370 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1371 # "If tests is given, it must be an iterable of individual test cases
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1372 # or other test suites that will be used to build the suite initially"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1373 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1374 # TestSuite should deal with empty tests iterables by allowing the
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1375 # creation of an empty suite
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1376 def test_init__empty_tests(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1377 suite = unittest.TestSuite([])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1378
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1379 self.assertEqual(suite.countTestCases(), 0)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1380
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1381 # "class TestSuite([tests])"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1382 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1383 # "If tests is given, it must be an iterable of individual test cases
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1384 # or other test suites that will be used to build the suite initially"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1385 #
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1386 # TestSuite should allow any iterable to provide tests
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1387 def test_init__tests_from_any_iterable(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1388 def tests():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1389 yield unittest.FunctionTestCase(lambda: None)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1390 yield unittest.FunctionTestCase(lambda: None)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1391
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1392 suite_1 = unittest.TestSuite(tests())
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1393 self.assertEqual(suite_1.countTestCases(), 2)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1394
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1395 suite_2 = unittest.TestSuite(suite_1)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1396 self.assertEqual(suite_2.countTestCases(), 2)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1397
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1398 suite_3 = unittest.TestSuite(set(suite_1))
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1399 self.assertEqual(suite_3.countTestCases(), 2)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1400
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1401 # "class TestSuite([tests])"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1402 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1403 # "If tests is given, it must be an iterable of individual test cases
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1404 # or other test suites that will be used to build the suite initially"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1405 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1406 # Does TestSuite() also allow other TestSuite() instances to be present
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1407 # in the tests iterable?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1408 def test_init__TestSuite_instances_in_tests(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1409 def tests():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1410 ftc = unittest.FunctionTestCase(lambda: None)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1411 yield unittest.TestSuite([ftc])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1412 yield unittest.FunctionTestCase(lambda: None)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1413
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1414 suite = unittest.TestSuite(tests())
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1415 self.assertEqual(suite.countTestCases(), 2)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1416
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1417 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1418 ### /Tests for TestSuite.__init__
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1419
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1420 # Container types should support the iter protocol
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1421 def test_iter(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1422 test1 = unittest.FunctionTestCase(lambda: None)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1423 test2 = unittest.FunctionTestCase(lambda: None)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1424 suite = unittest.TestSuite((test1, test2))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1425
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1426 self.assertEqual(list(suite), [test1, test2])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1427
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1428 # "Return the number of tests represented by the this test object.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1429 # ...this method is also implemented by the TestSuite class, which can
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1430 # return larger [greater than 1] values"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1431 #
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1432 # Presumably an empty TestSuite returns 0?
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1433 def test_countTestCases_zero_simple(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1434 suite = unittest.TestSuite()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1435
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1436 self.assertEqual(suite.countTestCases(), 0)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1437
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1438 # "Return the number of tests represented by the this test object.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1439 # ...this method is also implemented by the TestSuite class, which can
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1440 # return larger [greater than 1] values"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1441 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1442 # Presumably an empty TestSuite (even if it contains other empty
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1443 # TestSuite instances) returns 0?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1444 def test_countTestCases_zero_nested(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1445 class Test1(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1446 def test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1447 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1448
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1449 suite = unittest.TestSuite([unittest.TestSuite()])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1450
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1451 self.assertEqual(suite.countTestCases(), 0)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1452
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1453 # "Return the number of tests represented by the this test object.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1454 # ...this method is also implemented by the TestSuite class, which can
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1455 # return larger [greater than 1] values"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1456 def test_countTestCases_simple(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1457 test1 = unittest.FunctionTestCase(lambda: None)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1458 test2 = unittest.FunctionTestCase(lambda: None)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1459 suite = unittest.TestSuite((test1, test2))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1460
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1461 self.assertEqual(suite.countTestCases(), 2)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1462
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1463 # "Return the number of tests represented by the this test object.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1464 # ...this method is also implemented by the TestSuite class, which can
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1465 # return larger [greater than 1] values"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1466 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1467 # Make sure this holds for nested TestSuite instances, too
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1468 def test_countTestCases_nested(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1469 class Test1(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1470 def test1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1471 def test2(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1472
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1473 test2 = unittest.FunctionTestCase(lambda: None)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1474 test3 = unittest.FunctionTestCase(lambda: None)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1475 child = unittest.TestSuite((Test1('test2'), test2))
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1476 parent = unittest.TestSuite((test3, child, Test1('test1')))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1477
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1478 self.assertEqual(parent.countTestCases(), 4)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1479
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1480 # "Run the tests associated with this suite, collecting the result into
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1481 # the test result object passed as result."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1482 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1483 # And if there are no tests? What then?
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1484 def test_run__empty_suite(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1485 events = []
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1486 result = LoggingResult(events)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1487
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1488 suite = unittest.TestSuite()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1489
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1490 suite.run(result)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1491
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1492 self.assertEqual(events, [])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1493
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1494 # "Note that unlike TestCase.run(), TestSuite.run() requires the
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1495 # "result object to be passed in."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1496 def test_run__requires_result(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1497 suite = unittest.TestSuite()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1498
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1499 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1500 suite.run()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1501 except TypeError:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1502 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1503 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1504 self.fail("Failed to raise TypeError")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1505
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1506 # "Run the tests associated with this suite, collecting the result into
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1507 # the test result object passed as result."
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1508 def test_run(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1509 events = []
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1510 result = LoggingResult(events)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1511
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1512 class LoggingCase(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1513 def run(self, result):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1514 events.append('run %s' % self._testMethodName)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1515
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1516 def test1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1517 def test2(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1518
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1519 tests = [LoggingCase('test1'), LoggingCase('test2')]
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1520
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1521 unittest.TestSuite(tests).run(result)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1522
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1523 self.assertEqual(events, ['run test1', 'run test2'])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1524
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1525 # "Add a TestCase ... to the suite"
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1526 def test_addTest__TestCase(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1527 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1528 def test(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1529
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1530 test = Foo('test')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1531 suite = unittest.TestSuite()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1532
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1533 suite.addTest(test)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1534
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1535 self.assertEqual(suite.countTestCases(), 1)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1536 self.assertEqual(list(suite), [test])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1537
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1538 # "Add a ... TestSuite to the suite"
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1539 def test_addTest__TestSuite(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1540 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1541 def test(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1542
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1543 suite_2 = unittest.TestSuite([Foo('test')])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1544
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1545 suite = unittest.TestSuite()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1546 suite.addTest(suite_2)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1547
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1548 self.assertEqual(suite.countTestCases(), 1)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1549 self.assertEqual(list(suite), [suite_2])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1550
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1551 # "Add all the tests from an iterable of TestCase and TestSuite
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1552 # instances to this test suite."
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1553 #
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1554 # "This is equivalent to iterating over tests, calling addTest() for
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1555 # each element"
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1556 def test_addTests(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1557 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1558 def test_1(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1559 def test_2(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1560
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1561 test_1 = Foo('test_1')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1562 test_2 = Foo('test_2')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1563 inner_suite = unittest.TestSuite([test_2])
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1564
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1565 def gen():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1566 yield test_1
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1567 yield test_2
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1568 yield inner_suite
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1569
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1570 suite_1 = unittest.TestSuite()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1571 suite_1.addTests(gen())
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1572
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1573 self.assertEqual(list(suite_1), list(gen()))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1574
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1575 # "This is equivalent to iterating over tests, calling addTest() for
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1576 # each element"
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1577 suite_2 = unittest.TestSuite()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1578 for t in gen():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1579 suite_2.addTest(t)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1580
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1581 self.assertEqual(suite_1, suite_2)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1582
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1583 # "Add all the tests from an iterable of TestCase and TestSuite
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1584 # instances to this test suite."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1585 #
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1586 # What happens if it doesn't get an iterable?
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1587 def test_addTest__noniterable(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1588 suite = unittest.TestSuite()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1589
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1590 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1591 suite.addTests(5)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1592 except TypeError:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1593 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1594 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1595 self.fail("Failed to raise TypeError")
41059
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1596
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1597 def test_addTest__noncallable(self):
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1598 suite = unittest.TestSuite()
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1599 self.assertRaises(TypeError, suite.addTest, 5)
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1600
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1601 def test_addTest__casesuiteclass(self):
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1602 suite = unittest.TestSuite()
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1603 self.assertRaises(TypeError, suite.addTest, Test_TestSuite)
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1604 self.assertRaises(TypeError, suite.addTest, unittest.TestSuite)
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1605
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1606 def test_addTests__string(self):
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1607 suite = unittest.TestSuite()
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1608 self.assertRaises(TypeError, suite.addTests, "foo")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1609
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1610
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1611 class Test_FunctionTestCase(TestCase):
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1612
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1613 # "Return the number of tests represented by the this test object. For
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1614 # TestCase instances, this will always be 1"
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1615 def test_countTestCases(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1616 test = unittest.FunctionTestCase(lambda: None)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1617
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1618 self.assertEqual(test.countTestCases(), 1)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1619
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1620 # "When a setUp() method is defined, the test runner will run that method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1621 # prior to each test. Likewise, if a tearDown() method is defined, the
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1622 # test runner will invoke that method after each test. In the example,
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1623 # setUp() was used to create a fresh sequence for each test."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1624 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1625 # Make sure the proper call order is maintained, even if setUp() raises
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1626 # an exception.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1627 def test_run_call_order__error_in_setUp(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1628 events = []
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1629 result = LoggingResult(events)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1630
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1631 def setUp():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1632 events.append('setUp')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1633 raise RuntimeError('raised by setUp')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1634
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1635 def test():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1636 events.append('test')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1637
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1638 def tearDown():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1639 events.append('tearDown')
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1640
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1641 expected = ['startTest', 'setUp', 'addError', 'stopTest']
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1642 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1643 self.assertEqual(events, expected)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1644
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1645 # "When a setUp() method is defined, the test runner will run that method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1646 # prior to each test. Likewise, if a tearDown() method is defined, the
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1647 # test runner will invoke that method after each test. In the example,
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1648 # setUp() was used to create a fresh sequence for each test."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1649 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1650 # Make sure the proper call order is maintained, even if the test raises
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1651 # an error (as opposed to a failure).
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1652 def test_run_call_order__error_in_test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1653 events = []
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1654 result = LoggingResult(events)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1655
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1656 def setUp():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1657 events.append('setUp')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1658
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1659 def test():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1660 events.append('test')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1661 raise RuntimeError('raised by test')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1662
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1663 def tearDown():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1664 events.append('tearDown')
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1665
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1666 expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown',
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1667 'stopTest']
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1668 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1669 self.assertEqual(events, expected)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1670
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1671 # "When a setUp() method is defined, the test runner will run that method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1672 # prior to each test. Likewise, if a tearDown() method is defined, the
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1673 # test runner will invoke that method after each test. In the example,
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1674 # setUp() was used to create a fresh sequence for each test."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1675 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1676 # Make sure the proper call order is maintained, even if the test signals
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1677 # a failure (as opposed to an error).
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1678 def test_run_call_order__failure_in_test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1679 events = []
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1680 result = LoggingResult(events)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1681
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1682 def setUp():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1683 events.append('setUp')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1684
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1685 def test():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1686 events.append('test')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1687 self.fail('raised by test')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1688
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1689 def tearDown():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1690 events.append('tearDown')
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1691
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1692 expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown',
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1693 'stopTest']
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1694 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1695 self.assertEqual(events, expected)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1696
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1697 # "When a setUp() method is defined, the test runner will run that method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1698 # prior to each test. Likewise, if a tearDown() method is defined, the
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1699 # test runner will invoke that method after each test. In the example,
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1700 # setUp() was used to create a fresh sequence for each test."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1701 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1702 # Make sure the proper call order is maintained, even if tearDown() raises
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1703 # an exception.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1704 def test_run_call_order__error_in_tearDown(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1705 events = []
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1706 result = LoggingResult(events)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1707
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1708 def setUp():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1709 events.append('setUp')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1710
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1711 def test():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1712 events.append('test')
33314
2f52df173952 Added an __iter__ method for test suites.
Jim Fulton <jim@zope.com>
parents:
diff changeset
1713
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1714 def tearDown():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1715 events.append('tearDown')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1716 raise RuntimeError('raised by tearDown')
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1717
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1718 expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1719 'stopTest']
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1720 unittest.FunctionTestCase(test, setUp, tearDown).run(result)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1721 self.assertEqual(events, expected)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1722
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1723 # "Return a string identifying the specific test case."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1724 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1725 # Because of the vague nature of the docs, I'm not going to lock this
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1726 # test down too much. Really all that can be asserted is that the id()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1727 # will be a string (either 8-byte or unicode -- again, because the docs
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1728 # just say "string")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1729 def test_id(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1730 test = unittest.FunctionTestCase(lambda: None)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1731
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1732 self.failUnless(isinstance(test.id(), basestring))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1733
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1734 # "Returns a one-line description of the test, or None if no description
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1735 # has been provided. The default implementation of this method returns
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1736 # the first line of the test method's docstring, if available, or None."
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1737 def test_shortDescription__no_docstring(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1738 test = unittest.FunctionTestCase(lambda: None)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1739
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1740 self.assertEqual(test.shortDescription(), None)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1741
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1742 # "Returns a one-line description of the test, or None if no description
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1743 # has been provided. The default implementation of this method returns
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1744 # the first line of the test method's docstring, if available, or None."
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1745 def test_shortDescription__singleline_docstring(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1746 desc = "this tests foo"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1747 test = unittest.FunctionTestCase(lambda: None, description=desc)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1748
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1749 self.assertEqual(test.shortDescription(), "this tests foo")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1750
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1751 class Test_TestResult(TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1752 # Note: there are not separate tests for TestResult.wasSuccessful(),
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1753 # TestResult.errors, TestResult.failures, TestResult.testsRun or
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1754 # TestResult.shouldStop because these only have meaning in terms of
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1755 # other TestResult methods.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1756 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1757 # Accordingly, tests for the aforenamed attributes are incorporated
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1758 # in with the tests for the defining methods.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1759 ################################################################
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1760
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1761 def test_init(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1762 result = unittest.TestResult()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1763
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1764 self.failUnless(result.wasSuccessful())
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1765 self.assertEqual(len(result.errors), 0)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1766 self.assertEqual(len(result.failures), 0)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1767 self.assertEqual(result.testsRun, 0)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1768 self.assertEqual(result.shouldStop, False)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1769
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1770 # "This method can be called to signal that the set of tests being
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1771 # run should be aborted by setting the TestResult's shouldStop
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1772 # attribute to True."
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1773 def test_stop(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1774 result = unittest.TestResult()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1775
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1776 result.stop()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1777
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1778 self.assertEqual(result.shouldStop, True)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1779
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1780 # "Called when the test case test is about to be run. The default
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1781 # implementation simply increments the instance's testsRun counter."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1782 def test_startTest(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1783 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1784 def test_1(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1785 pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1786
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1787 test = Foo('test_1')
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1788
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1789 result = unittest.TestResult()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1790
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1791 result.startTest(test)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1792
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1793 self.failUnless(result.wasSuccessful())
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1794 self.assertEqual(len(result.errors), 0)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1795 self.assertEqual(len(result.failures), 0)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1796 self.assertEqual(result.testsRun, 1)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1797 self.assertEqual(result.shouldStop, False)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1798
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1799 result.stopTest(test)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1800
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1801 # "Called after the test case test has been executed, regardless of
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1802 # the outcome. The default implementation does nothing."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1803 def test_stopTest(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1804 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1805 def test_1(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1806 pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1807
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1808 test = Foo('test_1')
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1809
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1810 result = unittest.TestResult()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1811
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1812 result.startTest(test)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1813
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1814 self.failUnless(result.wasSuccessful())
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1815 self.assertEqual(len(result.errors), 0)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1816 self.assertEqual(len(result.failures), 0)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1817 self.assertEqual(result.testsRun, 1)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1818 self.assertEqual(result.shouldStop, False)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1819
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1820 result.stopTest(test)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1821
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1822 # Same tests as above; make sure nothing has changed
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1823 self.failUnless(result.wasSuccessful())
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1824 self.assertEqual(len(result.errors), 0)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1825 self.assertEqual(len(result.failures), 0)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1826 self.assertEqual(result.testsRun, 1)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1827 self.assertEqual(result.shouldStop, False)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1828
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1829 # "Called before and after tests are run. The default implementation does nothing."
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1830 def test_startTestRun_stopTestRun(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1831 result = unittest.TestResult()
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1832 result.startTestRun()
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1833 result.stopTestRun()
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1834
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1835 # "addSuccess(test)"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1836 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1837 # "Called when the test case test succeeds"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1838 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1839 # "wasSuccessful() - Returns True if all tests run so far have passed,
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1840 # otherwise returns False"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1841 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1842 # "testsRun - The total number of tests run so far."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1843 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1844 # "errors - A list containing 2-tuples of TestCase instances and
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1845 # formatted tracebacks. Each tuple represents a test which raised an
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1846 # unexpected exception. Contains formatted
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1847 # tracebacks instead of sys.exc_info() results."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1848 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1849 # "failures - A list containing 2-tuples of TestCase instances and
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1850 # formatted tracebacks. Each tuple represents a test where a failure was
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1851 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1852 # methods. Contains formatted tracebacks instead
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1853 # of sys.exc_info() results."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1854 def test_addSuccess(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1855 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1856 def test_1(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1857 pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1858
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1859 test = Foo('test_1')
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1860
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1861 result = unittest.TestResult()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1862
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1863 result.startTest(test)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1864 result.addSuccess(test)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1865 result.stopTest(test)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1866
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1867 self.failUnless(result.wasSuccessful())
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1868 self.assertEqual(len(result.errors), 0)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1869 self.assertEqual(len(result.failures), 0)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1870 self.assertEqual(result.testsRun, 1)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1871 self.assertEqual(result.shouldStop, False)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1872
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1873 # "addFailure(test, err)"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1874 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1875 # "Called when the test case test signals a failure. err is a tuple of
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1876 # the form returned by sys.exc_info(): (type, value, traceback)"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1877 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1878 # "wasSuccessful() - Returns True if all tests run so far have passed,
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1879 # otherwise returns False"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1880 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1881 # "testsRun - The total number of tests run so far."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1882 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1883 # "errors - A list containing 2-tuples of TestCase instances and
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1884 # formatted tracebacks. Each tuple represents a test which raised an
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1885 # unexpected exception. Contains formatted
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1886 # tracebacks instead of sys.exc_info() results."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1887 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1888 # "failures - A list containing 2-tuples of TestCase instances and
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1889 # formatted tracebacks. Each tuple represents a test where a failure was
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1890 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1891 # methods. Contains formatted tracebacks instead
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1892 # of sys.exc_info() results."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1893 def test_addFailure(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1894 import sys
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1895
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1896 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1897 def test_1(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1898 pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1899
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1900 test = Foo('test_1')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1901 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1902 test.fail("foo")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1903 except:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1904 exc_info_tuple = sys.exc_info()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1905
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1906 result = unittest.TestResult()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1907
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1908 result.startTest(test)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1909 result.addFailure(test, exc_info_tuple)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1910 result.stopTest(test)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1911
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1912 self.failIf(result.wasSuccessful())
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1913 self.assertEqual(len(result.errors), 0)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1914 self.assertEqual(len(result.failures), 1)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1915 self.assertEqual(result.testsRun, 1)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1916 self.assertEqual(result.shouldStop, False)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1917
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1918 test_case, formatted_exc = result.failures[0]
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1919 self.failUnless(test_case is test)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1920 self.failUnless(isinstance(formatted_exc, str))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1921
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1922 # "addError(test, err)"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1923 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1924 # "Called when the test case test raises an unexpected exception err
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1925 # is a tuple of the form returned by sys.exc_info():
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1926 # (type, value, traceback)"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1927 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1928 # "wasSuccessful() - Returns True if all tests run so far have passed,
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1929 # otherwise returns False"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1930 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1931 # "testsRun - The total number of tests run so far."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1932 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1933 # "errors - A list containing 2-tuples of TestCase instances and
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1934 # formatted tracebacks. Each tuple represents a test which raised an
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1935 # unexpected exception. Contains formatted
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1936 # tracebacks instead of sys.exc_info() results."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1937 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1938 # "failures - A list containing 2-tuples of TestCase instances and
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1939 # formatted tracebacks. Each tuple represents a test where a failure was
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1940 # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1941 # methods. Contains formatted tracebacks instead
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1942 # of sys.exc_info() results."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1943 def test_addError(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1944 import sys
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1945
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1946 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1947 def test_1(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1948 pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1949
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1950 test = Foo('test_1')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1951 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1952 raise TypeError()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1953 except:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1954 exc_info_tuple = sys.exc_info()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1955
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1956 result = unittest.TestResult()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1957
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1958 result.startTest(test)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1959 result.addError(test, exc_info_tuple)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1960 result.stopTest(test)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1961
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1962 self.failIf(result.wasSuccessful())
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1963 self.assertEqual(len(result.errors), 1)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1964 self.assertEqual(len(result.failures), 0)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1965 self.assertEqual(result.testsRun, 1)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1966 self.assertEqual(result.shouldStop, False)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1967
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1968 test_case, formatted_exc = result.errors[0]
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1969 self.failUnless(test_case is test)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1970 self.failUnless(isinstance(formatted_exc, str))
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1971
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1972 ### Support code for Test_TestCase
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1973 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1974
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1975 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1976 def runTest(self): pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1977 def test1(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1978
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1979 class Bar(Foo):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
1980 def test2(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
1981
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1982 class LoggingTestCase(unittest.TestCase):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1983 """A test case which logs its calls."""
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1984
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1985 def __init__(self, events):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1986 super(LoggingTestCase, self).__init__('test')
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1987 self.events = events
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1988
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1989 def setUp(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1990 self.events.append('setUp')
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1991
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1992 def test(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1993 self.events.append('test')
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1994
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1995 def tearDown(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1996 self.events.append('tearDown')
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1997
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1998 class ResultWithNoStartTestRunStopTestRun(object):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1999 """An object honouring TestResult before startTestRun/stopTestRun."""
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2000
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2001 def __init__(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2002 self.failures = []
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2003 self.errors = []
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2004 self.testsRun = 0
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2005 self.skipped = []
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2006 self.expectedFailures = []
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2007 self.unexpectedSuccesses = []
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2008 self.shouldStop = False
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2009
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2010 def startTest(self, test):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2011 pass
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2012
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2013 def stopTest(self, test):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2014 pass
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2015
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2016 def addError(self, test):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2017 pass
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2018
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2019 def addFailure(self, test):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2020 pass
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2021
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2022 def addSuccess(self, test):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2023 pass
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2024
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2025 def wasSuccessful(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2026 return True
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2027
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2028
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2029 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2030 ### /Support code for Test_TestCase
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2031
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2032 class Test_TestCase(TestCase, TestEquality, TestHashing):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2033
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2034 ### Set up attributes used by inherited tests
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2035 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2036
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2037 # Used by TestHashing.test_hash and TestEquality.test_eq
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2038 eq_pairs = [(Foo('test1'), Foo('test1'))]
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2039
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2040 # Used by TestEquality.test_ne
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2041 ne_pairs = [(Foo('test1'), Foo('runTest'))
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2042 ,(Foo('test1'), Bar('test1'))
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2043 ,(Foo('test1'), Bar('test2'))]
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2044
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2045 ################################################################
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2046 ### /Set up attributes used by inherited tests
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2047
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2048
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2049 # "class TestCase([methodName])"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2050 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2051 # "Each instance of TestCase will run a single test method: the
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2052 # method named methodName."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2053 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2054 # "methodName defaults to "runTest"."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2055 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2056 # Make sure it really is optional, and that it defaults to the proper
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2057 # thing.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2058 def test_init__no_test_name(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2059 class Test(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2060 def runTest(self): raise MyException()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2061 def test(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2062
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2063 self.assertEqual(Test().id()[-13:], '.Test.runTest')
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2064
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2065 # "class TestCase([methodName])"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2066 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2067 # "Each instance of TestCase will run a single test method: the
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2068 # method named methodName."
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2069 def test_init__test_name__valid(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2070 class Test(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2071 def runTest(self): raise MyException()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2072 def test(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2073
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2074 self.assertEqual(Test('test').id()[-10:], '.Test.test')
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2075
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2076 # "class TestCase([methodName])"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2077 # ...
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2078 # "Each instance of TestCase will run a single test method: the
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2079 # method named methodName."
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2080 def test_init__test_name__invalid(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2081 class Test(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2082 def runTest(self): raise MyException()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2083 def test(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2084
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2085 try:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2086 Test('testfoo')
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2087 except ValueError:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2088 pass
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2089 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2090 self.fail("Failed to raise ValueError")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2091
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2092 # "Return the number of tests represented by the this test object. For
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2093 # TestCase instances, this will always be 1"
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2094 def test_countTestCases(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2095 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2096 def test(self): pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2097
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2098 self.assertEqual(Foo('test').countTestCases(), 1)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2099
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2100 # "Return the default type of test result object to be used to run this
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2101 # test. For TestCase instances, this will always be
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2102 # unittest.TestResult; subclasses of TestCase should
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2103 # override this as necessary."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2104 def test_defaultTestResult(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2105 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2106 def runTest(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2107 pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2108
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2109 result = Foo().defaultTestResult()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2110 self.assertEqual(type(result), unittest.TestResult)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2111
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2112 # "When a setUp() method is defined, the test runner will run that method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2113 # prior to each test. Likewise, if a tearDown() method is defined, the
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2114 # test runner will invoke that method after each test. In the example,
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2115 # setUp() was used to create a fresh sequence for each test."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2116 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2117 # Make sure the proper call order is maintained, even if setUp() raises
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2118 # an exception.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2119 def test_run_call_order__error_in_setUp(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2120 events = []
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2121 result = LoggingResult(events)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2122
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2123 class Foo(LoggingTestCase):
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2124 def setUp(self):
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2125 super(Foo, self).setUp()
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2126 raise RuntimeError('raised by Foo.setUp')
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2127
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2128 Foo(events).run(result)
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2129 expected = ['startTest', 'setUp', 'addError', 'stopTest']
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2130 self.assertEqual(events, expected)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2131
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2132 # "With a temporary result stopTestRun is called when setUp errors.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2133 def test_run_call_order__error_in_setUp_default_result(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2134 events = []
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2135
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2136 class Foo(LoggingTestCase):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2137 def defaultTestResult(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2138 return LoggingResult(self.events)
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2139
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2140 def setUp(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2141 super(Foo, self).setUp()
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2142 raise RuntimeError('raised by Foo.setUp')
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2143
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2144 Foo(events).run()
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2145 expected = ['startTestRun', 'startTest', 'setUp', 'addError',
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2146 'stopTest', 'stopTestRun']
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2147 self.assertEqual(events, expected)
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2148
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2149 # "When a setUp() method is defined, the test runner will run that method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2150 # prior to each test. Likewise, if a tearDown() method is defined, the
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2151 # test runner will invoke that method after each test. In the example,
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2152 # setUp() was used to create a fresh sequence for each test."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2153 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2154 # Make sure the proper call order is maintained, even if the test raises
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2155 # an error (as opposed to a failure).
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2156 def test_run_call_order__error_in_test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2157 events = []
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2158 result = LoggingResult(events)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2159
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2160 class Foo(LoggingTestCase):
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2161 def test(self):
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2162 super(Foo, self).test()
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2163 raise RuntimeError('raised by Foo.test')
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2164
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2165 expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown',
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2166 'stopTest']
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2167 Foo(events).run(result)
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2168 self.assertEqual(events, expected)
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2169
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2170 # "With a default result, an error in the test still results in stopTestRun
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2171 # being called."
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2172 def test_run_call_order__error_in_test_default_result(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2173 events = []
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2174
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2175 class Foo(LoggingTestCase):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2176 def defaultTestResult(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2177 return LoggingResult(self.events)
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2178
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2179 def test(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2180 super(Foo, self).test()
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2181 raise RuntimeError('raised by Foo.test')
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2182
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2183 expected = ['startTestRun', 'startTest', 'setUp', 'test', 'addError',
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2184 'tearDown', 'stopTest', 'stopTestRun']
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2185 Foo(events).run()
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2186 self.assertEqual(events, expected)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2187
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2188 # "When a setUp() method is defined, the test runner will run that method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2189 # prior to each test. Likewise, if a tearDown() method is defined, the
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2190 # test runner will invoke that method after each test. In the example,
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2191 # setUp() was used to create a fresh sequence for each test."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2192 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2193 # Make sure the proper call order is maintained, even if the test signals
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2194 # a failure (as opposed to an error).
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2195 def test_run_call_order__failure_in_test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2196 events = []
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2197 result = LoggingResult(events)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2198
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2199 class Foo(LoggingTestCase):
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2200 def test(self):
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2201 super(Foo, self).test()
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2202 self.fail('raised by Foo.test')
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2203
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2204 expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown',
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2205 'stopTest']
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2206 Foo(events).run(result)
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2207 self.assertEqual(events, expected)
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2208
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2209 # "When a test fails with a default result stopTestRun is still called."
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2210 def test_run_call_order__failure_in_test_default_result(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2211
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2212 class Foo(LoggingTestCase):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2213 def defaultTestResult(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2214 return LoggingResult(self.events)
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2215 def test(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2216 super(Foo, self).test()
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2217 self.fail('raised by Foo.test')
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2218
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2219 expected = ['startTestRun', 'startTest', 'setUp', 'test', 'addFailure',
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2220 'tearDown', 'stopTest', 'stopTestRun']
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2221 events = []
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2222 Foo(events).run()
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2223 self.assertEqual(events, expected)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2224
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2225 # "When a setUp() method is defined, the test runner will run that method
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2226 # prior to each test. Likewise, if a tearDown() method is defined, the
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2227 # test runner will invoke that method after each test. In the example,
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2228 # setUp() was used to create a fresh sequence for each test."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2229 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2230 # Make sure the proper call order is maintained, even if tearDown() raises
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2231 # an exception.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2232 def test_run_call_order__error_in_tearDown(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2233 events = []
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2234 result = LoggingResult(events)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2235
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2236 class Foo(LoggingTestCase):
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2237 def tearDown(self):
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2238 super(Foo, self).tearDown()
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2239 raise RuntimeError('raised by Foo.tearDown')
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2240
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2241 Foo(events).run(result)
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2242 expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2243 'stopTest']
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2244 self.assertEqual(events, expected)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2245
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2246 # "When tearDown errors with a default result stopTestRun is still called."
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2247 def test_run_call_order__error_in_tearDown_default_result(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2248
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2249 class Foo(LoggingTestCase):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2250 def defaultTestResult(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2251 return LoggingResult(self.events)
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2252 def tearDown(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2253 super(Foo, self).tearDown()
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2254 raise RuntimeError('raised by Foo.tearDown')
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2255
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2256 events = []
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2257 Foo(events).run()
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2258 expected = ['startTestRun', 'startTest', 'setUp', 'test', 'tearDown',
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2259 'addError', 'stopTest', 'stopTestRun']
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2260 self.assertEqual(events, expected)
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2261
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2262 # "TestCase.run() still works when the defaultTestResult is a TestResult
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2263 # that does not support startTestRun and stopTestRun.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2264 def test_run_call_order_default_result(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2265
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2266 class Foo(unittest.TestCase):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2267 def defaultTestResult(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2268 return ResultWithNoStartTestRunStopTestRun()
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2269 def test(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2270 pass
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2271
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2272 Foo('test').run()
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2273
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2274 # "This class attribute gives the exception raised by the test() method.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2275 # If a test framework needs to use a specialized exception, possibly to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2276 # carry additional information, it must subclass this exception in
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2277 # order to ``play fair'' with the framework. The initial value of this
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2278 # attribute is AssertionError"
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2279 def test_failureException__default(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2280 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2281 def test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2282 pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2283
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2284 self.failUnless(Foo('test').failureException is AssertionError)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2285
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2286 # "This class attribute gives the exception raised by the test() method.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2287 # If a test framework needs to use a specialized exception, possibly to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2288 # carry additional information, it must subclass this exception in
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2289 # order to ``play fair'' with the framework."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2290 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2291 # Make sure TestCase.run() respects the designated failureException
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2292 def test_failureException__subclassing__explicit_raise(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2293 events = []
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2294 result = LoggingResult(events)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2295
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2296 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2297 def test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2298 raise RuntimeError()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2299
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2300 failureException = RuntimeError
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2301
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2302 self.failUnless(Foo('test').failureException is RuntimeError)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2303
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2304
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2305 Foo('test').run(result)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2306 expected = ['startTest', 'addFailure', 'stopTest']
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2307 self.assertEqual(events, expected)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2308
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2309 # "This class attribute gives the exception raised by the test() method.
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2310 # If a test framework needs to use a specialized exception, possibly to
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2311 # carry additional information, it must subclass this exception in
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2312 # order to ``play fair'' with the framework."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2313 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2314 # Make sure TestCase.run() respects the designated failureException
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2315 def test_failureException__subclassing__implicit_raise(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2316 events = []
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2317 result = LoggingResult(events)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2318
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2319 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2320 def test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2321 self.fail("foo")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2322
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2323 failureException = RuntimeError
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2324
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2325 self.failUnless(Foo('test').failureException is RuntimeError)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2326
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2327
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2328 Foo('test').run(result)
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2329 expected = ['startTest', 'addFailure', 'stopTest']
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2330 self.assertEqual(events, expected)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2331
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2332 # "The default implementation does nothing."
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2333 def test_setUp(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2334 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2335 def runTest(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2336 pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2337
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2338 # ... and nothing should happen
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2339 Foo().setUp()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2340
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2341 # "The default implementation does nothing."
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2342 def test_tearDown(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2343 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2344 def runTest(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2345 pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2346
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2347 # ... and nothing should happen
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2348 Foo().tearDown()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2349
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2350 # "Return a string identifying the specific test case."
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2351 #
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2352 # Because of the vague nature of the docs, I'm not going to lock this
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2353 # test down too much. Really all that can be asserted is that the id()
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2354 # will be a string (either 8-byte or unicode -- again, because the docs
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2355 # just say "string")
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2356 def test_id(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2357 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2358 def runTest(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2359 pass
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2360
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2361 self.failUnless(isinstance(Foo().id(), basestring))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2362
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2363 # "If result is omitted or None, a temporary result object is created
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2364 # and used, but is not made available to the caller. As TestCase owns the
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2365 # temporary result startTestRun and stopTestRun are called.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2366
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2367 def test_run__uses_defaultTestResult(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2368 events = []
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2369
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2370 class Foo(unittest.TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2371 def test(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2372 events.append('test')
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2373
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2374 def defaultTestResult(self):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2375 return LoggingResult(events)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2376
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2377 # Make run() find a result object on its own
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2378 Foo('test').run()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
2379
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2380 expected = ['startTestRun', 'startTest', 'test', 'addSuccess',
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
2381 'stopTest', 'stopTestRun']
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
2382 self.assertEqual(events, expected)
33314
2f52df173952 Added an __iter__ method for test suites.
Jim Fulton <jim@zope.com>
parents:
diff changeset
2383
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2384 def testShortDescriptionWithoutDocstring(self):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2385 self.assertEqual(
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2386 self.shortDescription(),
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2387 'testShortDescriptionWithoutDocstring (' + __name__ +
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2388 '.Test_TestCase)')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2389
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2390 def testShortDescriptionWithOneLineDocstring(self):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2391 """Tests shortDescription() for a method with a docstring."""
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2392 self.assertEqual(
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2393 self.shortDescription(),
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2394 ('testShortDescriptionWithOneLineDocstring '
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2395 '(' + __name__ + '.Test_TestCase)\n'
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2396 'Tests shortDescription() for a method with a docstring.'))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2397
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2398 def testShortDescriptionWithMultiLineDocstring(self):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2399 """Tests shortDescription() for a method with a longer docstring.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2400
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2401 This method ensures that only the first line of a docstring is
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2402 returned used in the short description, no matter how long the
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2403 whole thing is.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2404 """
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2405 self.assertEqual(
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2406 self.shortDescription(),
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2407 ('testShortDescriptionWithMultiLineDocstring '
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2408 '(' + __name__ + '.Test_TestCase)\n'
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2409 'Tests shortDescription() for a method with a longer '
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2410 'docstring.'))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2411
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2412 def testAddTypeEqualityFunc(self):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2413 class SadSnake(object):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2414 """Dummy class for test_addTypeEqualityFunc."""
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2415 s1, s2 = SadSnake(), SadSnake()
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2416 self.assertFalse(s1 == s2)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2417 def AllSnakesCreatedEqual(a, b, msg=None):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2418 return type(a) == type(b) == SadSnake
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2419 self.addTypeEqualityFunc(SadSnake, AllSnakesCreatedEqual)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2420 self.assertEqual(s1, s2)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2421 # No this doesn't clean up and remove the SadSnake equality func
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2422 # from this TestCase instance but since its a local nothing else
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2423 # will ever notice that.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2424
52894
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
2425 def testAssertIs(self):
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
2426 thing = object()
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
2427 self.assertIs(thing, thing)
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
2428 self.assertRaises(self.failureException, self.assertIs, thing, object())
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
2429
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
2430 def testAssertIsNot(self):
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
2431 thing = object()
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
2432 self.assertIsNot(thing, object())
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
2433 self.assertRaises(self.failureException, self.assertIsNot, thing, thing)
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
2434
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2435 def testAssertIn(self):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2436 animals = {'monkey': 'banana', 'cow': 'grass', 'seal': 'fish'}
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2437
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2438 self.assertIn('a', 'abc')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2439 self.assertIn(2, [1, 2, 3])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2440 self.assertIn('monkey', animals)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2441
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2442 self.assertNotIn('d', 'abc')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2443 self.assertNotIn(0, [1, 2, 3])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2444 self.assertNotIn('otter', animals)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2445
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2446 self.assertRaises(self.failureException, self.assertIn, 'x', 'abc')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2447 self.assertRaises(self.failureException, self.assertIn, 4, [1, 2, 3])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2448 self.assertRaises(self.failureException, self.assertIn, 'elephant',
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2449 animals)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2450
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2451 self.assertRaises(self.failureException, self.assertNotIn, 'c', 'abc')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2452 self.assertRaises(self.failureException, self.assertNotIn, 1, [1, 2, 3])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2453 self.assertRaises(self.failureException, self.assertNotIn, 'cow',
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2454 animals)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2455
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2456 def testAssertDictContainsSubset(self):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2457 self.assertDictContainsSubset({}, {})
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2458 self.assertDictContainsSubset({}, {'a': 1})
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2459 self.assertDictContainsSubset({'a': 1}, {'a': 1})
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2460 self.assertDictContainsSubset({'a': 1}, {'a': 1, 'b': 2})
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2461 self.assertDictContainsSubset({'a': 1, 'b': 2}, {'a': 1, 'b': 2})
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2462
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2463 self.assertRaises(unittest.TestCase.failureException,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2464 self.assertDictContainsSubset, {'a': 2}, {'a': 1},
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2465 '.*Mismatched values:.*')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2466
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2467 self.assertRaises(unittest.TestCase.failureException,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2468 self.assertDictContainsSubset, {'c': 1}, {'a': 1},
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2469 '.*Missing:.*')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2470
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2471 self.assertRaises(unittest.TestCase.failureException,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2472 self.assertDictContainsSubset, {'a': 1, 'c': 1},
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2473 {'a': 1}, '.*Missing:.*')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2474
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2475 self.assertRaises(unittest.TestCase.failureException,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2476 self.assertDictContainsSubset, {'a': 1, 'c': 1},
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2477 {'a': 1}, '.*Missing:.*Mismatched values:.*')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2478
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2479 def testAssertEqual(self):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2480 equal_pairs = [
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2481 ((), ()),
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2482 ({}, {}),
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2483 ([], []),
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2484 (set(), set()),
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2485 (frozenset(), frozenset())]
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2486 for a, b in equal_pairs:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2487 # This mess of try excepts is to test the assertEqual behavior
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2488 # itself.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2489 try:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2490 self.assertEqual(a, b)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2491 except self.failureException:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2492 self.fail('assertEqual(%r, %r) failed' % (a, b))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2493 try:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2494 self.assertEqual(a, b, msg='foo')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2495 except self.failureException:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2496 self.fail('assertEqual(%r, %r) with msg= failed' % (a, b))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2497 try:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2498 self.assertEqual(a, b, 'foo')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2499 except self.failureException:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2500 self.fail('assertEqual(%r, %r) with third parameter failed' %
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2501 (a, b))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2502
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2503 unequal_pairs = [
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2504 ((), []),
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2505 ({}, set()),
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2506 (set([4,1]), frozenset([4,2])),
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2507 (frozenset([4,5]), set([2,3])),
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2508 (set([3,4]), set([5,4]))]
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2509 for a, b in unequal_pairs:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2510 self.assertRaises(self.failureException, self.assertEqual, a, b)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2511 self.assertRaises(self.failureException, self.assertEqual, a, b,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2512 'foo')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2513 self.assertRaises(self.failureException, self.assertEqual, a, b,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2514 msg='foo')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2515
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2516 def testEquality(self):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2517 self.assertListEqual([], [])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2518 self.assertTupleEqual((), ())
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2519 self.assertSequenceEqual([], ())
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2520
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2521 a = [0, 'a', []]
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2522 b = []
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2523 self.assertRaises(unittest.TestCase.failureException,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2524 self.assertListEqual, a, b)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2525 self.assertRaises(unittest.TestCase.failureException,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2526 self.assertListEqual, tuple(a), tuple(b))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2527 self.assertRaises(unittest.TestCase.failureException,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2528 self.assertSequenceEqual, a, tuple(b))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2529
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2530 b.extend(a)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2531 self.assertListEqual(a, b)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2532 self.assertTupleEqual(tuple(a), tuple(b))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2533 self.assertSequenceEqual(a, tuple(b))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2534 self.assertSequenceEqual(tuple(a), b)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2535
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2536 self.assertRaises(self.failureException, self.assertListEqual,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2537 a, tuple(b))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2538 self.assertRaises(self.failureException, self.assertTupleEqual,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2539 tuple(a), b)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2540 self.assertRaises(self.failureException, self.assertListEqual, None, b)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2541 self.assertRaises(self.failureException, self.assertTupleEqual, None,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2542 tuple(b))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2543 self.assertRaises(self.failureException, self.assertSequenceEqual,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2544 None, tuple(b))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2545 self.assertRaises(self.failureException, self.assertListEqual, 1, 1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2546 self.assertRaises(self.failureException, self.assertTupleEqual, 1, 1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2547 self.assertRaises(self.failureException, self.assertSequenceEqual,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2548 1, 1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2549
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2550 self.assertDictEqual({}, {})
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2551
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2552 c = { 'x': 1 }
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2553 d = {}
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2554 self.assertRaises(unittest.TestCase.failureException,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2555 self.assertDictEqual, c, d)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2556
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2557 d.update(c)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2558 self.assertDictEqual(c, d)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2559
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2560 d['x'] = 0
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2561 self.assertRaises(unittest.TestCase.failureException,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2562 self.assertDictEqual, c, d, 'These are unequal')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2563
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2564 self.assertRaises(self.failureException, self.assertDictEqual, None, d)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2565 self.assertRaises(self.failureException, self.assertDictEqual, [], d)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2566 self.assertRaises(self.failureException, self.assertDictEqual, 1, 1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2567
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2568 self.assertSameElements([1, 2, 3], [3, 2, 1])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2569 self.assertSameElements([1, 2] + [3] * 100, [1] * 100 + [2, 3])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2570 self.assertSameElements(['foo', 'bar', 'baz'], ['bar', 'baz', 'foo'])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2571 self.assertRaises(self.failureException, self.assertSameElements,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2572 [10], [10, 11])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2573 self.assertRaises(self.failureException, self.assertSameElements,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2574 [10, 11], [10])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2575
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2576 # Test that sequences of unhashable objects can be tested for sameness:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2577 self.assertSameElements([[1, 2], [3, 4]], [[3, 4], [1, 2]])
52894
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
2578
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2579 self.assertSameElements([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2580 self.assertRaises(self.failureException, self.assertSameElements,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2581 [[1]], [[2]])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2582
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2583 def testAssertSetEqual(self):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2584 set1 = set()
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2585 set2 = set()
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2586 self.assertSetEqual(set1, set2)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2587
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2588 self.assertRaises(self.failureException, self.assertSetEqual, None, set2)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2589 self.assertRaises(self.failureException, self.assertSetEqual, [], set2)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2590 self.assertRaises(self.failureException, self.assertSetEqual, set1, None)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2591 self.assertRaises(self.failureException, self.assertSetEqual, set1, [])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2592
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2593 set1 = set(['a'])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2594 set2 = set()
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2595 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2596
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2597 set1 = set(['a'])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2598 set2 = set(['a'])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2599 self.assertSetEqual(set1, set2)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2600
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2601 set1 = set(['a'])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2602 set2 = set(['a', 'b'])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2603 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2604
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2605 set1 = set(['a'])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2606 set2 = frozenset(['a', 'b'])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2607 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2608
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2609 set1 = set(['a', 'b'])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2610 set2 = frozenset(['a', 'b'])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2611 self.assertSetEqual(set1, set2)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2612
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2613 set1 = set()
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2614 set2 = "foo"
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2615 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2616 self.assertRaises(self.failureException, self.assertSetEqual, set2, set1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2617
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2618 # make sure any string formatting is tuple-safe
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2619 set1 = set([(0, 1), (2, 3)])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2620 set2 = set([(4, 5)])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2621 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2622
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2623 def testInequality(self):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2624 # Try ints
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2625 self.assertGreater(2, 1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2626 self.assertGreaterEqual(2, 1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2627 self.assertGreaterEqual(1, 1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2628 self.assertLess(1, 2)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2629 self.assertLessEqual(1, 2)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2630 self.assertLessEqual(1, 1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2631 self.assertRaises(self.failureException, self.assertGreater, 1, 2)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2632 self.assertRaises(self.failureException, self.assertGreater, 1, 1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2633 self.assertRaises(self.failureException, self.assertGreaterEqual, 1, 2)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2634 self.assertRaises(self.failureException, self.assertLess, 2, 1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2635 self.assertRaises(self.failureException, self.assertLess, 1, 1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2636 self.assertRaises(self.failureException, self.assertLessEqual, 2, 1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2637
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2638 # Try Floats
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2639 self.assertGreater(1.1, 1.0)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2640 self.assertGreaterEqual(1.1, 1.0)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2641 self.assertGreaterEqual(1.0, 1.0)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2642 self.assertLess(1.0, 1.1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2643 self.assertLessEqual(1.0, 1.1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2644 self.assertLessEqual(1.0, 1.0)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2645 self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2646 self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.0)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2647 self.assertRaises(self.failureException, self.assertGreaterEqual, 1.0, 1.1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2648 self.assertRaises(self.failureException, self.assertLess, 1.1, 1.0)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2649 self.assertRaises(self.failureException, self.assertLess, 1.0, 1.0)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2650 self.assertRaises(self.failureException, self.assertLessEqual, 1.1, 1.0)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2651
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2652 # Try Strings
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2653 self.assertGreater('bug', 'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2654 self.assertGreaterEqual('bug', 'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2655 self.assertGreaterEqual('ant', 'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2656 self.assertLess('ant', 'bug')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2657 self.assertLessEqual('ant', 'bug')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2658 self.assertLessEqual('ant', 'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2659 self.assertRaises(self.failureException, self.assertGreater, 'ant', 'bug')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2660 self.assertRaises(self.failureException, self.assertGreater, 'ant', 'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2661 self.assertRaises(self.failureException, self.assertGreaterEqual, 'ant', 'bug')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2662 self.assertRaises(self.failureException, self.assertLess, 'bug', 'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2663 self.assertRaises(self.failureException, self.assertLess, 'ant', 'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2664 self.assertRaises(self.failureException, self.assertLessEqual, 'bug', 'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2665
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2666 # Try Unicode
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2667 self.assertGreater(u'bug', u'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2668 self.assertGreaterEqual(u'bug', u'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2669 self.assertGreaterEqual(u'ant', u'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2670 self.assertLess(u'ant', u'bug')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2671 self.assertLessEqual(u'ant', u'bug')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2672 self.assertLessEqual(u'ant', u'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2673 self.assertRaises(self.failureException, self.assertGreater, u'ant', u'bug')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2674 self.assertRaises(self.failureException, self.assertGreater, u'ant', u'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2675 self.assertRaises(self.failureException, self.assertGreaterEqual, u'ant',
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2676 u'bug')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2677 self.assertRaises(self.failureException, self.assertLess, u'bug', u'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2678 self.assertRaises(self.failureException, self.assertLess, u'ant', u'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2679 self.assertRaises(self.failureException, self.assertLessEqual, u'bug', u'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2680
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2681 # Try Mixed String/Unicode
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2682 self.assertGreater('bug', u'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2683 self.assertGreater(u'bug', 'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2684 self.assertGreaterEqual('bug', u'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2685 self.assertGreaterEqual(u'bug', 'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2686 self.assertGreaterEqual('ant', u'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2687 self.assertGreaterEqual(u'ant', 'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2688 self.assertLess('ant', u'bug')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2689 self.assertLess(u'ant', 'bug')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2690 self.assertLessEqual('ant', u'bug')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2691 self.assertLessEqual(u'ant', 'bug')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2692 self.assertLessEqual('ant', u'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2693 self.assertLessEqual(u'ant', 'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2694 self.assertRaises(self.failureException, self.assertGreater, 'ant', u'bug')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2695 self.assertRaises(self.failureException, self.assertGreater, u'ant', 'bug')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2696 self.assertRaises(self.failureException, self.assertGreater, 'ant', u'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2697 self.assertRaises(self.failureException, self.assertGreater, u'ant', 'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2698 self.assertRaises(self.failureException, self.assertGreaterEqual, 'ant',
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2699 u'bug')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2700 self.assertRaises(self.failureException, self.assertGreaterEqual, u'ant',
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2701 'bug')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2702 self.assertRaises(self.failureException, self.assertLess, 'bug', u'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2703 self.assertRaises(self.failureException, self.assertLess, u'bug', 'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2704 self.assertRaises(self.failureException, self.assertLess, 'ant', u'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2705 self.assertRaises(self.failureException, self.assertLess, u'ant', 'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2706 self.assertRaises(self.failureException, self.assertLessEqual, 'bug', u'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2707 self.assertRaises(self.failureException, self.assertLessEqual, u'bug', 'ant')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2708
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2709 def testAssertMultiLineEqual(self):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2710 sample_text = b"""\
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2711 http://www.python.org/doc/2.3/lib/module-unittest.html
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2712 test case
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2713 A test case is the smallest unit of testing. [...]
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2714 """
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2715 revised_sample_text = b"""\
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2716 http://www.python.org/doc/2.4.1/lib/module-unittest.html
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2717 test case
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2718 A test case is the smallest unit of testing. [...] You may provide your
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2719 own implementation that does not subclass from TestCase, of course.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2720 """
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2721 sample_text_error = b"""
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2722 - http://www.python.org/doc/2.3/lib/module-unittest.html
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2723 ? ^
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2724 + http://www.python.org/doc/2.4.1/lib/module-unittest.html
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2725 ? ^^^
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2726 test case
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2727 - A test case is the smallest unit of testing. [...]
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2728 + A test case is the smallest unit of testing. [...] You may provide your
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2729 ? +++++++++++++++++++++
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2730 + own implementation that does not subclass from TestCase, of course.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2731 """
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2732
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2733 for type_changer in (lambda x: x, lambda x: x.decode('utf8')):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2734 try:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2735 self.assertMultiLineEqual(type_changer(sample_text),
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2736 type_changer(revised_sample_text))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2737 except self.failureException, e:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2738 # no fair testing ourself with ourself, use assertEqual..
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2739 self.assertEqual(sample_text_error, str(e).encode('utf8'))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2740
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2741 def testAssertIsNone(self):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2742 self.assertIsNone(None)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2743 self.assertRaises(self.failureException, self.assertIsNone, False)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2744 self.assertIsNotNone('DjZoPloGears on Rails')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2745 self.assertRaises(self.failureException, self.assertIsNotNone, None)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2746
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2747 def testAssertRegexpMatches(self):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2748 self.assertRegexpMatches('asdfabasdf', r'ab+')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2749 self.assertRaises(self.failureException, self.assertRegexpMatches,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2750 'saaas', r'aaaa')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2751
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2752 def testAssertRaisesRegexp(self):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2753 class ExceptionMock(Exception):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2754 pass
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2755
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2756 def Stub():
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2757 raise ExceptionMock('We expect')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2758
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2759 self.assertRaisesRegexp(ExceptionMock, re.compile('expect$'), Stub)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2760 self.assertRaisesRegexp(ExceptionMock, 'expect$', Stub)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2761 self.assertRaisesRegexp(ExceptionMock, u'expect$', Stub)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2762
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2763 def testAssertNotRaisesRegexp(self):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2764 self.assertRaisesRegexp(
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2765 self.failureException, '^Exception not raised$',
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2766 self.assertRaisesRegexp, Exception, re.compile('x'),
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2767 lambda: None)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2768 self.assertRaisesRegexp(
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2769 self.failureException, '^Exception not raised$',
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2770 self.assertRaisesRegexp, Exception, 'x',
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2771 lambda: None)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2772 self.assertRaisesRegexp(
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2773 self.failureException, '^Exception not raised$',
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2774 self.assertRaisesRegexp, Exception, u'x',
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2775 lambda: None)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2776
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2777 def testAssertRaisesRegexpMismatch(self):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2778 def Stub():
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2779 raise Exception('Unexpected')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2780
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2781 self.assertRaisesRegexp(
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2782 self.failureException,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2783 r'"\^Expected\$" does not match "Unexpected"',
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2784 self.assertRaisesRegexp, Exception, '^Expected$',
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2785 Stub)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2786 self.assertRaisesRegexp(
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2787 self.failureException,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2788 r'"\^Expected\$" does not match "Unexpected"',
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2789 self.assertRaisesRegexp, Exception, u'^Expected$',
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2790 Stub)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2791 self.assertRaisesRegexp(
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2792 self.failureException,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2793 r'"\^Expected\$" does not match "Unexpected"',
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2794 self.assertRaisesRegexp, Exception,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2795 re.compile('^Expected$'), Stub)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2796
52563
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2797 def testSynonymAssertMethodNames(self):
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2798 """Test undocumented method name synonyms.
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2799
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2800 Please do not use these methods names in your own code.
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2801
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2802 This test confirms their continued existence and functionality
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2803 in order to avoid breaking existing code.
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2804 """
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2805 self.assertNotEquals(3, 5)
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2806 self.assertEquals(3, 3)
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2807 self.assertAlmostEquals(2.0, 2.0)
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2808 self.assertNotAlmostEquals(3.0, 5.0)
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2809 self.assert_(True)
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2810
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2811 def testPendingDeprecationMethodNames(self):
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2812 """Test fail* methods pending deprecation, they will warn in 3.2.
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2813
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2814 Do not use these methods. They will go away in 3.3.
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2815 """
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2816 self.failIfEqual(3, 5)
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2817 self.failUnlessEqual(3, 3)
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2818 self.failUnlessAlmostEqual(2.0, 2.0)
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2819 self.failIfAlmostEqual(3.0, 5.0)
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2820 self.failUnless(True)
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2821 self.failUnlessRaises(TypeError, lambda _: 3.14 + u'spam')
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2822 self.failIf(False)
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
2823
52729
1abf9e30c19f Store the functions in the _type_equality_funcs as wrapped objects that are deep copyable.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52719
diff changeset
2824 def testDeepcopy(self):
1abf9e30c19f Store the functions in the _type_equality_funcs as wrapped objects that are deep copyable.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52719
diff changeset
2825 # Issue: 5660
1abf9e30c19f Store the functions in the _type_equality_funcs as wrapped objects that are deep copyable.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52719
diff changeset
2826 class TestableTest(TestCase):
1abf9e30c19f Store the functions in the _type_equality_funcs as wrapped objects that are deep copyable.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52719
diff changeset
2827 def testNothing(self):
1abf9e30c19f Store the functions in the _type_equality_funcs as wrapped objects that are deep copyable.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52719
diff changeset
2828 pass
1abf9e30c19f Store the functions in the _type_equality_funcs as wrapped objects that are deep copyable.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52719
diff changeset
2829
1abf9e30c19f Store the functions in the _type_equality_funcs as wrapped objects that are deep copyable.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52719
diff changeset
2830 test = TestableTest('testNothing')
1abf9e30c19f Store the functions in the _type_equality_funcs as wrapped objects that are deep copyable.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52719
diff changeset
2831
1abf9e30c19f Store the functions in the _type_equality_funcs as wrapped objects that are deep copyable.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52719
diff changeset
2832 # This shouldn't blow up
1abf9e30c19f Store the functions in the _type_equality_funcs as wrapped objects that are deep copyable.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52719
diff changeset
2833 deepcopy(test)
1abf9e30c19f Store the functions in the _type_equality_funcs as wrapped objects that are deep copyable.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52719
diff changeset
2834
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2835
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2836 class Test_TestSkipping(TestCase):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2837
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2838 def test_skipping(self):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2839 class Foo(unittest.TestCase):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2840 def test_skip_me(self):
52351
557290ffa784 rename TestCase.skip() to skipTest() because it causes annoying problems with trial #5571
Benjamin Peterson <benjamin@python.org>
parents: 52300
diff changeset
2841 self.skipTest("skip")
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2842 events = []
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2843 result = LoggingResult(events)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2844 test = Foo("test_skip_me")
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2845 test.run(result)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2846 self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2847 self.assertEqual(result.skipped, [(test, "skip")])
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2848
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2849 # Try letting setUp skip the test now.
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2850 class Foo(unittest.TestCase):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2851 def setUp(self):
52351
557290ffa784 rename TestCase.skip() to skipTest() because it causes annoying problems with trial #5571
Benjamin Peterson <benjamin@python.org>
parents: 52300
diff changeset
2852 self.skipTest("testing")
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2853 def test_nothing(self): pass
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2854 events = []
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2855 result = LoggingResult(events)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2856 test = Foo("test_nothing")
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2857 test.run(result)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2858 self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2859 self.assertEqual(result.skipped, [(test, "testing")])
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2860 self.assertEqual(result.testsRun, 1)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2861
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2862 def test_skipping_decorators(self):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2863 op_table = ((unittest.skipUnless, False, True),
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2864 (unittest.skipIf, True, False))
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2865 for deco, do_skip, dont_skip in op_table:
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2866 class Foo(unittest.TestCase):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2867 @deco(do_skip, "testing")
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2868 def test_skip(self): pass
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2869
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2870 @deco(dont_skip, "testing")
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2871 def test_dont_skip(self): pass
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2872 test_do_skip = Foo("test_skip")
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2873 test_dont_skip = Foo("test_dont_skip")
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2874 suite = unittest.ClassTestSuite([test_do_skip, test_dont_skip], Foo)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2875 events = []
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2876 result = LoggingResult(events)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2877 suite.run(result)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2878 self.assertEqual(len(result.skipped), 1)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2879 expected = ['startTest', 'addSkip', 'stopTest',
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2880 'startTest', 'addSuccess', 'stopTest']
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2881 self.assertEqual(events, expected)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2882 self.assertEqual(result.testsRun, 2)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2883 self.assertEqual(result.skipped, [(test_do_skip, "testing")])
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2884 self.assertTrue(result.wasSuccessful())
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2885
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2886 def test_skip_class(self):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2887 @unittest.skip("testing")
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2888 class Foo(unittest.TestCase):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2889 def test_1(self):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2890 record.append(1)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2891 record = []
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2892 result = unittest.TestResult()
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2893 suite = unittest.ClassTestSuite([Foo("test_1")], Foo)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2894 suite.run(result)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2895 self.assertEqual(result.skipped, [(suite, "testing")])
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2896 self.assertEqual(record, [])
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2897
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2898 def test_expected_failure(self):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2899 class Foo(unittest.TestCase):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2900 @unittest.expectedFailure
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2901 def test_die(self):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2902 self.fail("help me!")
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2903 events = []
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2904 result = LoggingResult(events)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2905 test = Foo("test_die")
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2906 test.run(result)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2907 self.assertEqual(events,
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2908 ['startTest', 'addExpectedFailure', 'stopTest'])
52300
68ac2552f44e comply with the evilJavaNamingScheme for attribute names
Benjamin Peterson <benjamin@python.org>
parents: 52297
diff changeset
2909 self.assertEqual(result.expectedFailures[0][0], test)
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2910 self.assertTrue(result.wasSuccessful())
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2911
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2912 def test_unexpected_success(self):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2913 class Foo(unittest.TestCase):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2914 @unittest.expectedFailure
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2915 def test_die(self):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2916 pass
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2917 events = []
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2918 result = LoggingResult(events)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2919 test = Foo("test_die")
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2920 test.run(result)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2921 self.assertEqual(events,
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2922 ['startTest', 'addUnexpectedSuccess', 'stopTest'])
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2923 self.assertFalse(result.failures)
52300
68ac2552f44e comply with the evilJavaNamingScheme for attribute names
Benjamin Peterson <benjamin@python.org>
parents: 52297
diff changeset
2924 self.assertEqual(result.unexpectedSuccesses, [test])
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2925 self.assertTrue(result.wasSuccessful())
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2926
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2927
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50333
diff changeset
2928
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44034
diff changeset
2929 class Test_Assertions(TestCase):
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44034
diff changeset
2930 def test_AlmostEqual(self):
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44034
diff changeset
2931 self.failUnlessAlmostEqual(1.00000001, 1.0)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44034
diff changeset
2932 self.failIfAlmostEqual(1.0000001, 1.0)
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2933 self.assertRaises(self.failureException,
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44034
diff changeset
2934 self.failUnlessAlmostEqual, 1.0000001, 1.0)
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2935 self.assertRaises(self.failureException,
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44034
diff changeset
2936 self.failIfAlmostEqual, 1.00000001, 1.0)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44034
diff changeset
2937
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44034
diff changeset
2938 self.failUnlessAlmostEqual(1.1, 1.0, places=0)
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2939 self.assertRaises(self.failureException,
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44034
diff changeset
2940 self.failUnlessAlmostEqual, 1.1, 1.0, places=1)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44034
diff changeset
2941
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44034
diff changeset
2942 self.failUnlessAlmostEqual(0, .1+.1j, places=0)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44034
diff changeset
2943 self.failIfAlmostEqual(0, .1+.1j, places=1)
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2944 self.assertRaises(self.failureException,
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44034
diff changeset
2945 self.failUnlessAlmostEqual, 0, .1+.1j, places=1)
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2946 self.assertRaises(self.failureException,
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44034
diff changeset
2947 self.failIfAlmostEqual, 0, .1+.1j, places=0)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44034
diff changeset
2948
50333
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2949 def test_assertRaises(self):
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2950 def _raise(e):
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2951 raise e
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2952 self.assertRaises(KeyError, _raise, KeyError)
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2953 self.assertRaises(KeyError, _raise, KeyError("key"))
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2954 try:
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2955 self.assertRaises(KeyError, lambda: None)
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2956 except self.failureException as e:
50333
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2957 self.assert_("KeyError not raised" in e, str(e))
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2958 else:
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2959 self.fail("assertRaises() didn't fail")
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2960 try:
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2961 self.assertRaises(KeyError, _raise, ValueError)
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2962 except ValueError:
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2963 pass
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2964 else:
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2965 self.fail("assertRaises() didn't let exception pass through")
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2966 with self.assertRaises(KeyError):
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2967 raise KeyError
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2968 with self.assertRaises(KeyError):
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2969 raise KeyError("key")
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2970 try:
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2971 with self.assertRaises(KeyError):
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2972 pass
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
2973 except self.failureException as e:
50333
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2974 self.assert_("KeyError not raised" in e, str(e))
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2975 else:
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2976 self.fail("assertRaises() didn't fail")
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2977 try:
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2978 with self.assertRaises(KeyError):
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2979 raise ValueError
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2980 except ValueError:
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2981 pass
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2982 else:
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2983 self.fail("assertRaises() didn't let exception pass through")
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2984
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 44432
diff changeset
2985
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
2986 class TestLongMessage(TestCase):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
2987 """Test that the individual asserts honour longMessage.
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
2988 This actually tests all the message behaviour for
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
2989 asserts that use longMessage."""
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
2990
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
2991 def setUp(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
2992 class TestableTestFalse(TestCase):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
2993 longMessage = False
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
2994 failureException = self.failureException
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
2995
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
2996 def testTest(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
2997 pass
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
2998
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
2999 class TestableTestTrue(TestCase):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3000 longMessage = True
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3001 failureException = self.failureException
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3002
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3003 def testTest(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3004 pass
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3005
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3006 self.testableTrue = TestableTestTrue('testTest')
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3007 self.testableFalse = TestableTestFalse('testTest')
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3008
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3009 def testDefault(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3010 self.assertFalse(TestCase.longMessage)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3011
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3012 def test_formatMsg(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3013 self.assertEquals(self.testableFalse._formatMessage(None, "foo"), "foo")
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3014 self.assertEquals(self.testableFalse._formatMessage("foo", "bar"), "foo")
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3015
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3016 self.assertEquals(self.testableTrue._formatMessage(None, "foo"), "foo")
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3017 self.assertEquals(self.testableTrue._formatMessage("foo", "bar"), "bar : foo")
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3018
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3019 def assertMessages(self, methodName, args, errors):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3020 def getMethod(i):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3021 useTestableFalse = i < 2
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3022 if useTestableFalse:
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3023 test = self.testableFalse
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3024 else:
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3025 test = self.testableTrue
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3026 return getattr(test, methodName)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3027
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3028 for i, expected_regexp in enumerate(errors):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3029 testMethod = getMethod(i)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3030 kwargs = {}
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3031 withMsg = i % 2
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3032 if withMsg:
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3033 kwargs = {"msg": "oops"}
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3034
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3035 with self.assertRaisesRegexp(self.failureException,
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3036 expected_regexp=expected_regexp):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3037 testMethod(*args, **kwargs)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3038
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3039 def testAssertTrue(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3040 self.assertMessages('assertTrue', (False,),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3041 ["^False is not True$", "^oops$", "^False is not True$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3042 "^False is not True : oops$"])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3043
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3044 def testAssertFalse(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3045 self.assertMessages('assertFalse', (True,),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3046 ["^True is not False$", "^oops$", "^True is not False$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3047 "^True is not False : oops$"])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3048
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3049 def testNotEqual(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3050 self.assertMessages('assertNotEqual', (1, 1),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3051 ["^1 == 1$", "^oops$", "^1 == 1$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3052 "^1 == 1 : oops$"])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3053
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3054 def testAlmostEqual(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3055 self.assertMessages('assertAlmostEqual', (1, 2),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3056 ["^1 != 2 within 7 places$", "^oops$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3057 "^1 != 2 within 7 places$", "^1 != 2 within 7 places : oops$"])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3058
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3059 def testNotAlmostEqual(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3060 self.assertMessages('assertNotAlmostEqual', (1, 1),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3061 ["^1 == 1 within 7 places$", "^oops$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3062 "^1 == 1 within 7 places$", "^1 == 1 within 7 places : oops$"])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3063
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3064 def test_baseAssertEqual(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3065 self.assertMessages('_baseAssertEqual', (1, 2),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3066 ["^1 != 2$", "^oops$", "^1 != 2$", "^1 != 2 : oops$"])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3067
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3068 def testAssertSequenceEqual(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3069 # Error messages are multiline so not testing on full message
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3070 # assertTupleEqual and assertListEqual delegate to this method
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3071 self.assertMessages('assertSequenceEqual', ([], [None]),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3072 ["\+ \[None\]$", "^oops$", r"\+ \[None\]$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3073 r"\+ \[None\] : oops$"])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3074
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3075 def testAssertSetEqual(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3076 self.assertMessages('assertSetEqual', (set(), set([None])),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3077 ["None$", "^oops$", "None$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3078 "None : oops$"])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3079
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3080 def testAssertIn(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3081 self.assertMessages('assertIn', (None, []),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3082 ['^None not found in \[\]$', "^oops$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3083 '^None not found in \[\]$',
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3084 '^None not found in \[\] : oops$'])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3085
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3086 def testAssertNotIn(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3087 self.assertMessages('assertNotIn', (None, [None]),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3088 ['^None unexpectedly found in \[None\]$', "^oops$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3089 '^None unexpectedly found in \[None\]$',
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3090 '^None unexpectedly found in \[None\] : oops$'])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3091
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3092 def testAssertDictEqual(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3093 self.assertMessages('assertDictEqual', ({}, {'key': 'value'}),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3094 [r"\+ \{'key': 'value'\}$", "^oops$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3095 "\+ \{'key': 'value'\}$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3096 "\+ \{'key': 'value'\} : oops$"])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3097
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3098 def testAssertDictContainsSubset(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3099 self.assertMessages('assertDictContainsSubset', ({'key': 'value'}, {}),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3100 ["^Missing: 'key'$", "^oops$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3101 "^Missing: 'key'$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3102 "^Missing: 'key' : oops$"])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3103
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3104 def testAssertSameElements(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3105 self.assertMessages('assertSameElements', ([], [None]),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3106 [r"\[None\]$", "^oops$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3107 r"\[None\]$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3108 r"\[None\] : oops$"])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3109
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3110 def testAssertMultiLineEqual(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3111 self.assertMessages('assertMultiLineEqual', ("", "foo"),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3112 [r"\+ foo$", "^oops$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3113 r"\+ foo$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3114 r"\+ foo : oops$"])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3115
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3116 def testAssertLess(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3117 self.assertMessages('assertLess', (2, 1),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3118 ["^2 not less than 1$", "^oops$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3119 "^2 not less than 1$", "^2 not less than 1 : oops$"])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3120
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3121 def testAssertLessEqual(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3122 self.assertMessages('assertLessEqual', (2, 1),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3123 ["^2 not less than or equal to 1$", "^oops$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3124 "^2 not less than or equal to 1$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3125 "^2 not less than or equal to 1 : oops$"])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3126
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3127 def testAssertGreater(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3128 self.assertMessages('assertGreater', (1, 2),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3129 ["^1 not greater than 2$", "^oops$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3130 "^1 not greater than 2$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3131 "^1 not greater than 2 : oops$"])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3132
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3133 def testAssertGreaterEqual(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3134 self.assertMessages('assertGreaterEqual', (1, 2),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3135 ["^1 not greater than or equal to 2$", "^oops$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3136 "^1 not greater than or equal to 2$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3137 "^1 not greater than or equal to 2 : oops$"])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3138
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3139 def testAssertIsNone(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3140 self.assertMessages('assertIsNone', ('not None',),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3141 ["^'not None' is not None$", "^oops$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3142 "^'not None' is not None$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3143 "^'not None' is not None : oops$"])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3144
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3145 def testAssertIsNotNone(self):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3146 self.assertMessages('assertIsNotNone', (None,),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3147 ["^unexpectedly None$", "^oops$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3148 "^unexpectedly None$",
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3149 "^unexpectedly None : oops$"])
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3150
52894
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
3151 def testAssertIs(self):
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
3152 self.assertMessages('assertIs', (None, 'foo'),
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
3153 ["^None is not 'foo'$", "^oops$",
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
3154 "^None is not 'foo'$",
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
3155 "^None is not 'foo' : oops$"])
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
3156
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
3157 def testAssertIsNot(self):
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
3158 self.assertMessages('assertIsNot', (None, None),
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
3159 ["^unexpectedly identical: None$", "^oops$",
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
3160 "^unexpectedly identical: None$",
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
3161 "^unexpectedly identical: None : oops$"])
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
3162
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52563
diff changeset
3163
53689
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3164 class TestCleanUp(TestCase):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3165
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3166 def testCleanUp(self):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3167 class TestableTest(TestCase):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3168 def testNothing(self):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3169 pass
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3170
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3171 test = TestableTest('testNothing')
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3172 self.assertEqual(test._cleanups, [])
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3173
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3174 cleanups = []
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3175
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3176 def cleanup1(*args, **kwargs):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3177 cleanups.append((1, args, kwargs))
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3178
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3179 def cleanup2(*args, **kwargs):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3180 cleanups.append((2, args, kwargs))
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3181
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3182 test.addCleanup(cleanup1, 1, 2, 3, four='hello', five='goodbye')
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3183 test.addCleanup(cleanup2)
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3184
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3185 self.assertEqual(test._cleanups,
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3186 [(cleanup1, (1, 2, 3), dict(four='hello', five='goodbye')),
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3187 (cleanup2, (), {})])
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3188
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3189 result = test.doCleanups()
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3190 self.assertTrue(result)
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3191
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3192 self.assertEqual(cleanups, [(2, (), {}), (1, (1, 2, 3), dict(four='hello', five='goodbye'))])
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3193
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3194 def testCleanUpWithErrors(self):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3195 class TestableTest(TestCase):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3196 def testNothing(self):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3197 pass
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3198
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3199 class MockResult(object):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3200 errors = []
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3201 def addError(self, test, exc_info):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3202 self.errors.append((test, exc_info))
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3203
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3204 result = MockResult()
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3205 test = TestableTest('testNothing')
54234
eb9b1ac3d9d8 Rename TestCase._result to _resultForDoCleanups to avoid potential clashes in TestCase subclasses. Issue 6072.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 54006
diff changeset
3206 test._resultForDoCleanups = result
53689
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3207
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3208 exc1 = Exception('foo')
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3209 exc2 = Exception('bar')
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3210 def cleanup1():
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3211 raise exc1
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3212
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3213 def cleanup2():
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3214 raise exc2
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3215
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3216 test.addCleanup(cleanup1)
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3217 test.addCleanup(cleanup2)
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3218
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3219 self.assertFalse(test.doCleanups())
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3220
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3221 (test1, (Type1, instance1, _)), (test2, (Type2, instance2, _)) = reversed(MockResult.errors)
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3222 self.assertEqual((test1, Type1, instance1), (test, Exception, exc1))
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3223 self.assertEqual((test2, Type2, instance2), (test, Exception, exc2))
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3224
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3225 def testCleanupInRun(self):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3226 blowUp = False
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3227 ordering = []
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3228
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3229 class TestableTest(TestCase):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3230 def setUp(self):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3231 ordering.append('setUp')
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3232 if blowUp:
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3233 raise Exception('foo')
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3234
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3235 def testNothing(self):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3236 ordering.append('test')
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3237
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3238 def tearDown(self):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3239 ordering.append('tearDown')
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3240
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3241 test = TestableTest('testNothing')
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3242
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3243 def cleanup1():
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3244 ordering.append('cleanup1')
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3245 def cleanup2():
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3246 ordering.append('cleanup2')
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3247 test.addCleanup(cleanup1)
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3248 test.addCleanup(cleanup2)
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3249
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3250 def success(some_test):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3251 self.assertEqual(some_test, test)
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3252 ordering.append('success')
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3253
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3254 result = unittest.TestResult()
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3255 result.addSuccess = success
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3256
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3257 test.run(result)
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3258 self.assertEqual(ordering, ['setUp', 'test', 'tearDown',
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3259 'cleanup2', 'cleanup1', 'success'])
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3260
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3261 blowUp = True
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3262 ordering = []
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3263 test = TestableTest('testNothing')
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3264 test.addCleanup(cleanup1)
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3265 test.run(result)
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3266 self.assertEqual(ordering, ['setUp', 'cleanup1'])
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3267
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3268
53664
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3269 class Test_TestProgram(TestCase):
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3270
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3271 # Horrible white box test
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3272 def testNoExit(self):
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3273 result = object()
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3274 test = object()
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3275
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3276 class FakeRunner(object):
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3277 def run(self, test):
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3278 self.test = test
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3279 return result
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3280
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3281 runner = FakeRunner()
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3282
54006
ccfca2587885 Adds a verbosity keyword argument to unittest.main plus a minor fix allowing you to specify test modules / classes
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53695
diff changeset
3283 oldParseArgs = TestProgram.parseArgs
ccfca2587885 Adds a verbosity keyword argument to unittest.main plus a minor fix allowing you to specify test modules / classes
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53695
diff changeset
3284 def restoreParseArgs():
53664
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3285 TestProgram.parseArgs = oldParseArgs
54006
ccfca2587885 Adds a verbosity keyword argument to unittest.main plus a minor fix allowing you to specify test modules / classes
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53695
diff changeset
3286 TestProgram.parseArgs = lambda *args: None
ccfca2587885 Adds a verbosity keyword argument to unittest.main plus a minor fix allowing you to specify test modules / classes
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53695
diff changeset
3287 self.addCleanup(restoreParseArgs)
ccfca2587885 Adds a verbosity keyword argument to unittest.main plus a minor fix allowing you to specify test modules / classes
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53695
diff changeset
3288
ccfca2587885 Adds a verbosity keyword argument to unittest.main plus a minor fix allowing you to specify test modules / classes
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53695
diff changeset
3289 def removeTest():
53664
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3290 del TestProgram.test
54006
ccfca2587885 Adds a verbosity keyword argument to unittest.main plus a minor fix allowing you to specify test modules / classes
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53695
diff changeset
3291 TestProgram.test = test
ccfca2587885 Adds a verbosity keyword argument to unittest.main plus a minor fix allowing you to specify test modules / classes
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53695
diff changeset
3292 self.addCleanup(removeTest)
ccfca2587885 Adds a verbosity keyword argument to unittest.main plus a minor fix allowing you to specify test modules / classes
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53695
diff changeset
3293
ccfca2587885 Adds a verbosity keyword argument to unittest.main plus a minor fix allowing you to specify test modules / classes
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53695
diff changeset
3294 program = TestProgram(testRunner=runner, exit=False, verbosity=2)
ccfca2587885 Adds a verbosity keyword argument to unittest.main plus a minor fix allowing you to specify test modules / classes
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53695
diff changeset
3295
ccfca2587885 Adds a verbosity keyword argument to unittest.main plus a minor fix allowing you to specify test modules / classes
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53695
diff changeset
3296 self.assertEqual(program.result, result)
ccfca2587885 Adds a verbosity keyword argument to unittest.main plus a minor fix allowing you to specify test modules / classes
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53695
diff changeset
3297 self.assertEqual(runner.test, test)
ccfca2587885 Adds a verbosity keyword argument to unittest.main plus a minor fix allowing you to specify test modules / classes
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53695
diff changeset
3298 self.assertEqual(program.verbosity, 2)
53664
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3299
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3300
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3301 class FooBar(unittest.TestCase):
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3302 def testPass(self):
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3303 assert True
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3304 def testFail(self):
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3305 assert False
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3306
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3307 class FooBarLoader(unittest.TestLoader):
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3308 """Test loader that returns a suite containing FooBar."""
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3309 def loadTestsFromModule(self, module):
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3310 return self.suiteClass(
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3311 [self.loadTestsFromTestCase(Test_TestProgram.FooBar)])
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3312
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3313
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3314 def test_NonExit(self):
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3315 program = unittest.main(exit=False,
53670
e62bc57a62ea don't let sys.argv be used in the tests
Benjamin Peterson <benjamin@python.org>
parents: 53664
diff changeset
3316 argv=["foobar"],
e62bc57a62ea don't let sys.argv be used in the tests
Benjamin Peterson <benjamin@python.org>
parents: 53664
diff changeset
3317 testRunner=unittest.TextTestRunner(stream=StringIO()),
e62bc57a62ea don't let sys.argv be used in the tests
Benjamin Peterson <benjamin@python.org>
parents: 53664
diff changeset
3318 testLoader=self.FooBarLoader())
53664
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3319 self.assertTrue(hasattr(program, 'result'))
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3320
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3321
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3322 def test_Exit(self):
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3323 self.assertRaises(
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3324 SystemExit,
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3325 unittest.main,
53670
e62bc57a62ea don't let sys.argv be used in the tests
Benjamin Peterson <benjamin@python.org>
parents: 53664
diff changeset
3326 argv=["foobar"],
53664
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3327 testRunner=unittest.TextTestRunner(stream=StringIO()),
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3328 exit=True,
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3329 testLoader=self.FooBarLoader())
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3330
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3331
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3332 def test_ExitAsDefault(self):
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3333 self.assertRaises(
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3334 SystemExit,
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3335 unittest.main,
53670
e62bc57a62ea don't let sys.argv be used in the tests
Benjamin Peterson <benjamin@python.org>
parents: 53664
diff changeset
3336 argv=["foobar"],
53664
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3337 testRunner=unittest.TextTestRunner(stream=StringIO()),
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3338 testLoader=self.FooBarLoader())
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3339
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3340
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3341 class Test_TextTestRunner(TestCase):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3342 """Tests for TextTestRunner."""
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3343
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3344 def test_works_with_result_without_startTestRun_stopTestRun(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3345 class OldTextResult(ResultWithNoStartTestRunStopTestRun):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3346 separator2 = ''
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3347 def printErrors(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3348 pass
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3349
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3350 class Runner(unittest.TextTestRunner):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3351 def __init__(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3352 super(Runner, self).__init__(StringIO())
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3353
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3354 def _makeResult(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3355 return OldTextResult()
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3356
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3357 runner = Runner()
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3358 runner.run(unittest.TestSuite())
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3359
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3360 def test_startTestRun_stopTestRun_called(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3361 class LoggingTextResult(LoggingResult):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3362 separator2 = ''
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3363 def printErrors(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3364 pass
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3365
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3366 class LoggingRunner(unittest.TextTestRunner):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3367 def __init__(self, events):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3368 super(LoggingRunner, self).__init__(StringIO())
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3369 self._events = events
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3370
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3371 def _makeResult(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3372 return LoggingTextResult(self._events)
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3373
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3374 events = []
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3375 runner = LoggingRunner(events)
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3376 runner.run(unittest.TestSuite())
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3377 expected = ['startTestRun', 'stopTestRun']
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3378 self.assertEqual(events, expected)
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3379
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
3380
33314
2f52df173952 Added an __iter__ method for test suites.
Jim Fulton <jim@zope.com>
parents:
diff changeset
3381 ######################################################################
2f52df173952 Added an __iter__ method for test suites.
Jim Fulton <jim@zope.com>
parents:
diff changeset
3382 ## Main
2f52df173952 Added an __iter__ method for test suites.
Jim Fulton <jim@zope.com>
parents:
diff changeset
3383 ######################################################################
2f52df173952 Added an __iter__ method for test suites.
Jim Fulton <jim@zope.com>
parents:
diff changeset
3384
2f52df173952 Added an __iter__ method for test suites.
Jim Fulton <jim@zope.com>
parents:
diff changeset
3385 def test_main():
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
3386 test_support.run_unittest(Test_TestCase, Test_TestLoader,
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44034
diff changeset
3387 Test_TestSuite, Test_TestResult, Test_FunctionTestCase,
53664
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52894
diff changeset
3388 Test_TestSkipping, Test_Assertions, TestLongMessage,
53689
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53670
diff changeset
3389 Test_TestProgram, TestCleanUp)
33314
2f52df173952 Added an __iter__ method for test suites.
Jim Fulton <jim@zope.com>
parents:
diff changeset
3390
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 33314
diff changeset
3391 if __name__ == "__main__":
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41059
diff changeset
3392 test_main()