annotate Lib/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
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 #!/usr/bin/env python
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
2 '''
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
3 Python unit testing framework, based on Erich Gamma's JUnit and Kent Beck's
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4 Smalltalk testing framework.
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
5
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6 This module contains the core framework classes that form the basis of
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
7 specific test cases and suites (TestCase, TestSuite etc.), and also a
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
8 text-based utility class for running the tests and reporting the results
20616
a3d6b31e8bb9 A few formatting nits:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19782
diff changeset
9 (TextTestRunner).
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
10
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
11 Simple usage:
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
12
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
13 import unittest
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
14
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
15 class IntegerArithmenticTestCase(unittest.TestCase):
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
16 def testAdd(self): ## test method names begin 'test*'
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
17 self.assertEqual((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
18 self.assertEqual(0 + 1, 1)
19759
8e87cdded96f Changed TestResult to store only the text representation of an error.
Steve Purcell <steve@pythonconsulting.com>
parents: 19060
diff changeset
19 def testMultiply(self):
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
20 self.assertEqual((0 * 10), 0)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
21 self.assertEqual((5 * 8), 40)
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
22
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
23 if __name__ == '__main__':
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
24 unittest.main()
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
25
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
26 Further information is available in the bundled documentation, and from
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
27
52312
6edb7421638c update docstring
Benjamin Peterson <benjamin@python.org>
parents: 52311
diff changeset
28 http://docs.python.org/library/unittest.html
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
29
30494
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
30 Copyright (c) 1999-2003 Steve Purcell
52312
6edb7421638c update docstring
Benjamin Peterson <benjamin@python.org>
parents: 52311
diff changeset
31 Copyright (c) 2003-2009 Python Software Foundation
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
32 This module is free software, and you may redistribute it and/or modify
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
33 it under the same terms as Python itself, so long as this copyright message
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
34 and disclaimer are retained in their original form.
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
36 IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37 SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
38 THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
39 DAMAGE.
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41 THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
42 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
43 PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS,
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
44 AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
45 SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
46 '''
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
47
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
48 import difflib
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
49 import functools
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
50 import os
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
51 import pprint
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
52 import re
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
53 import sys
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
54 import time
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55 import traceback
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
56 import types
52576
ab9b7d5db935 Issue an actual PendingDeprecationWarning for the TestCase.fail* methods.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52563
diff changeset
57 import warnings
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
58
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
59 ##############################################################################
30424
95ab55d14256 Explicitly define public symbols via __all__: see discussion with Raymond
Steve Purcell <steve@pythonconsulting.com>
parents: 30420
diff changeset
60 # Exported classes and functions
95ab55d14256 Explicitly define public symbols via __all__: see discussion with Raymond
Steve Purcell <steve@pythonconsulting.com>
parents: 30420
diff changeset
61 ##############################################################################
52313
12adb8197647 add new skipping things to __all__
Benjamin Peterson <benjamin@python.org>
parents: 52312
diff changeset
62 __all__ = ['TestResult', 'TestCase', 'TestSuite', 'ClassTestSuite',
12adb8197647 add new skipping things to __all__
Benjamin Peterson <benjamin@python.org>
parents: 52312
diff changeset
63 'TextTestRunner', 'TestLoader', 'FunctionTestCase', 'main',
52316
d2134133bf0c fix typo
Benjamin Peterson <benjamin@python.org>
parents: 52313
diff changeset
64 'defaultTestLoader', 'SkipTest', 'skip', 'skipIf', 'skipUnless',
52313
12adb8197647 add new skipping things to __all__
Benjamin Peterson <benjamin@python.org>
parents: 52312
diff changeset
65 'expectedFailure']
30424
95ab55d14256 Explicitly define public symbols via __all__: see discussion with Raymond
Steve Purcell <steve@pythonconsulting.com>
parents: 30420
diff changeset
66
30494
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
67 # Expose obsolete functions for backwards compatibility
30424
95ab55d14256 Explicitly define public symbols via __all__: see discussion with Raymond
Steve Purcell <steve@pythonconsulting.com>
parents: 30420
diff changeset
68 __all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases'])
95ab55d14256 Explicitly define public symbols via __all__: see discussion with Raymond
Steve Purcell <steve@pythonconsulting.com>
parents: 30420
diff changeset
69
95ab55d14256 Explicitly define public symbols via __all__: see discussion with Raymond
Steve Purcell <steve@pythonconsulting.com>
parents: 30420
diff changeset
70
95ab55d14256 Explicitly define public symbols via __all__: see discussion with Raymond
Steve Purcell <steve@pythonconsulting.com>
parents: 30420
diff changeset
71 ##############################################################################
30494
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
72 # Backward compatibility
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
73 ##############################################################################
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
74
47924
328f33baffa3 Suppress -3 warnings in unittest.py
Raymond Hettinger <python@rcn.com>
parents: 46890
diff changeset
75 def _CmpToKey(mycmp):
328f33baffa3 Suppress -3 warnings in unittest.py
Raymond Hettinger <python@rcn.com>
parents: 46890
diff changeset
76 'Convert a cmp= function into a key= function'
328f33baffa3 Suppress -3 warnings in unittest.py
Raymond Hettinger <python@rcn.com>
parents: 46890
diff changeset
77 class K(object):
328f33baffa3 Suppress -3 warnings in unittest.py
Raymond Hettinger <python@rcn.com>
parents: 46890
diff changeset
78 def __init__(self, obj):
328f33baffa3 Suppress -3 warnings in unittest.py
Raymond Hettinger <python@rcn.com>
parents: 46890
diff changeset
79 self.obj = obj
328f33baffa3 Suppress -3 warnings in unittest.py
Raymond Hettinger <python@rcn.com>
parents: 46890
diff changeset
80 def __lt__(self, other):
328f33baffa3 Suppress -3 warnings in unittest.py
Raymond Hettinger <python@rcn.com>
parents: 46890
diff changeset
81 return mycmp(self.obj, other.obj) == -1
328f33baffa3 Suppress -3 warnings in unittest.py
Raymond Hettinger <python@rcn.com>
parents: 46890
diff changeset
82 return K
30494
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
83
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
84 ##############################################################################
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
85 # Test framework core
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
86 ##############################################################################
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
87
24692
a28c9c336ab7 Fix to ensure consistent 'repr' and 'str' results between Python
Steve Purcell <steve@pythonconsulting.com>
parents: 24659
diff changeset
88 def _strclass(cls):
a28c9c336ab7 Fix to ensure consistent 'repr' and 'str' results between Python
Steve Purcell <steve@pythonconsulting.com>
parents: 24659
diff changeset
89 return "%s.%s" % (cls.__module__, cls.__name__)
a28c9c336ab7 Fix to ensure consistent 'repr' and 'str' results between Python
Steve Purcell <steve@pythonconsulting.com>
parents: 24659
diff changeset
90
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
91
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
92 class SkipTest(Exception):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
93 """
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
94 Raise this exception in a test to skip it.
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
95
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
96 Usually you can use TestResult.skip() or one of the skipping decorators
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
97 instead of raising this directly.
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
98 """
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
99 pass
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
100
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
101 class _ExpectedFailure(Exception):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
102 """
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
103 Raise this when a test is expected to fail.
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
104
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
105 This is an implementation detail.
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
106 """
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
107
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
108 def __init__(self, exc_info):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
109 super(_ExpectedFailure, self).__init__()
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
110 self.exc_info = exc_info
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
111
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
112 class _UnexpectedSuccess(Exception):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
113 """
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
114 The test was supposed to fail, but it didn't!
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
115 """
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
116 pass
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
117
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
118 def _id(obj):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
119 return obj
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
120
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
121 def skip(reason):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
122 """
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
123 Unconditionally skip a test.
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
124 """
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
125 def decorator(test_item):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
126 if isinstance(test_item, type) and issubclass(test_item, TestCase):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
127 test_item.__unittest_skip__ = True
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
128 test_item.__unittest_skip_why__ = reason
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
129 return test_item
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
130 @functools.wraps(test_item)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
131 def skip_wrapper(*args, **kwargs):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
132 raise SkipTest(reason)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
133 return skip_wrapper
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
134 return decorator
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
135
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
136 def skipIf(condition, reason):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
137 """
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
138 Skip a test if the condition is true.
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
139 """
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
140 if condition:
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
141 return skip(reason)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
142 return _id
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
143
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
144 def skipUnless(condition, reason):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
145 """
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
146 Skip a test unless the condition is true.
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
147 """
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
148 if not condition:
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
149 return skip(reason)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
150 return _id
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
151
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
152
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
153 def expectedFailure(func):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
154 @functools.wraps(func)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
155 def wrapper(*args, **kwargs):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
156 try:
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
157 func(*args, **kwargs)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
158 except Exception:
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
159 raise _ExpectedFailure(sys.exc_info())
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
160 raise _UnexpectedSuccess
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
161 return wrapper
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
162
31135
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
163 __unittest = 1
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
164
50338
7c7624a33f8b Issue #2153: modernize coding style of unittest.py, remove obsolete compatibility stuff.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50333
diff changeset
165 class TestResult(object):
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
166 """Holder for test result information.
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
167
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
168 Test results are automatically managed by the TestCase and TestSuite
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
169 classes, and do not need to be explicitly manipulated by writers of tests.
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
170
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
171 Each instance holds the total number of tests run, and collections of
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
172 failures and errors that occurred among those test runs. The collections
19759
8e87cdded96f Changed TestResult to store only the text representation of an error.
Steve Purcell <steve@pythonconsulting.com>
parents: 19060
diff changeset
173 contain tuples of (testcase, exceptioninfo), where exceptioninfo is the
19782
9de2f7e1b6e8 Add missing period in docstring.
Fred Drake <fdrake@acm.org>
parents: 19759
diff changeset
174 formatted traceback of the error that occurred.
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
175 """
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
176 def __init__(self):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
177 self.failures = []
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
178 self.errors = []
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
179 self.testsRun = 0
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
180 self.skipped = []
52300
68ac2552f44e comply with the evilJavaNamingScheme for attribute names
Benjamin Peterson <benjamin@python.org>
parents: 52297
diff changeset
181 self.expectedFailures = []
68ac2552f44e comply with the evilJavaNamingScheme for attribute names
Benjamin Peterson <benjamin@python.org>
parents: 52297
diff changeset
182 self.unexpectedSuccesses = []
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
183 self.shouldStop = False
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
184
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
185 def startTest(self, test):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
186 "Called when the given test is about to be run"
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
187 self.testsRun = self.testsRun + 1
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
188
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
189 def startTestRun(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
190 """Called once before any tests are executed.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
191
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
192 See startTest for a method called before each test.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
193 """
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
194
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
195 def stopTest(self, test):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
196 "Called when the given test has been run"
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
197 pass
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
198
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
199 def stopTestRun(self):
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
200 """Called once after all tests are executed.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
201
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
202 See stopTest for a method called after each test.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
203 """
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
204
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
205 def addError(self, test, err):
19759
8e87cdded96f Changed TestResult to store only the text representation of an error.
Steve Purcell <steve@pythonconsulting.com>
parents: 19060
diff changeset
206 """Called when an error has occurred. 'err' is a tuple of values as
8e87cdded96f Changed TestResult to store only the text representation of an error.
Steve Purcell <steve@pythonconsulting.com>
parents: 19060
diff changeset
207 returned by sys.exc_info().
8e87cdded96f Changed TestResult to store only the text representation of an error.
Steve Purcell <steve@pythonconsulting.com>
parents: 19060
diff changeset
208 """
31135
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
209 self.errors.append((test, self._exc_info_to_string(err, test)))
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
210
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
211 def addFailure(self, test, err):
19759
8e87cdded96f Changed TestResult to store only the text representation of an error.
Steve Purcell <steve@pythonconsulting.com>
parents: 19060
diff changeset
212 """Called when an error has occurred. 'err' is a tuple of values as
8e87cdded96f Changed TestResult to store only the text representation of an error.
Steve Purcell <steve@pythonconsulting.com>
parents: 19060
diff changeset
213 returned by sys.exc_info()."""
31135
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
214 self.failures.append((test, self._exc_info_to_string(err, test)))
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
215
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
216 def addSuccess(self, test):
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
217 "Called when a test has completed successfully"
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
218 pass
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
219
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
220 def addSkip(self, test, reason):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
221 """Called when a test is skipped."""
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
222 self.skipped.append((test, reason))
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
223
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
224 def addExpectedFailure(self, test, err):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
225 """Called when an expected failure/error occured."""
52300
68ac2552f44e comply with the evilJavaNamingScheme for attribute names
Benjamin Peterson <benjamin@python.org>
parents: 52297
diff changeset
226 self.expectedFailures.append(
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
227 (test, self._exc_info_to_string(err, test)))
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
228
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
229 def addUnexpectedSuccess(self, test):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
230 """Called when a test was expected to fail, but succeed."""
52300
68ac2552f44e comply with the evilJavaNamingScheme for attribute names
Benjamin Peterson <benjamin@python.org>
parents: 52297
diff changeset
231 self.unexpectedSuccesses.append(test)
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
232
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
233 def wasSuccessful(self):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
234 "Tells whether or not this result was a success"
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
235 return len(self.failures) == len(self.errors) == 0
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
236
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
237 def stop(self):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
238 "Indicates that the tests should be aborted"
30494
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
239 self.shouldStop = True
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17133
diff changeset
240
31135
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
241 def _exc_info_to_string(self, err, test):
19759
8e87cdded96f Changed TestResult to store only the text representation of an error.
Steve Purcell <steve@pythonconsulting.com>
parents: 19060
diff changeset
242 """Converts a sys.exc_info()-style tuple of values into a string."""
31135
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
243 exctype, value, tb = err
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
244 # Skip test runner traceback levels
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
245 while tb and self._is_relevant_tb_level(tb):
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
246 tb = tb.tb_next
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
247 if exctype is test.failureException:
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
248 # Skip assert*() traceback levels
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
249 length = self._count_relevant_tb_levels(tb)
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
250 return ''.join(traceback.format_exception(exctype, value, tb, length))
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
251 return ''.join(traceback.format_exception(exctype, value, tb))
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
252
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
253 def _is_relevant_tb_level(self, tb):
48019
b931a0226466 #3390: replace a remaining has_key().
Georg Brandl <georg@python.org>
parents: 47924
diff changeset
254 return '__unittest' in tb.tb_frame.f_globals
31135
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
255
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
256 def _count_relevant_tb_levels(self, tb):
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
257 length = 0
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
258 while tb and not self._is_relevant_tb_level(tb):
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
259 length += 1
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
260 tb = tb.tb_next
89d26e03c653 Variation of Thomas Heller's patch (722638) for improving readability
Steve Purcell <steve@pythonconsulting.com>
parents: 30788
diff changeset
261 return length
19759
8e87cdded96f Changed TestResult to store only the text representation of an error.
Steve Purcell <steve@pythonconsulting.com>
parents: 19060
diff changeset
262
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
263 def __repr__(self):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
264 return "<%s run=%i errors=%i failures=%i>" % \
24692
a28c9c336ab7 Fix to ensure consistent 'repr' and 'str' results between Python
Steve Purcell <steve@pythonconsulting.com>
parents: 24659
diff changeset
265 (_strclass(self.__class__), self.testsRun, len(self.errors),
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
266 len(self.failures))
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
267
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
268
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
269 class _AssertRaisesContext(object):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
270 """A context manager used to implement TestCase.assertRaises* methods."""
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
271
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
272 def __init__(self, expected, test_case, expected_regexp=None):
50333
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
273 self.expected = expected
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
274 self.failureException = test_case.failureException
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
275 self.expected_regex = expected_regexp
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
276
50333
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
277 def __enter__(self):
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
278 pass
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
279
53238
9755d8dd3366 fix a few nits in unittest.py #5771
Benjamin Peterson <benjamin@python.org>
parents: 52894
diff changeset
280 def __exit__(self, exc_type, exc_value, tb):
50333
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
281 if exc_type is None:
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
282 try:
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
283 exc_name = self.expected.__name__
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
284 except AttributeError:
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
285 exc_name = str(self.expected)
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
286 raise self.failureException(
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
287 "{0} not raised".format(exc_name))
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
288 if not issubclass(exc_type, self.expected):
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
289 # let unexpected exceptions pass through
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
290 return False
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
291 if self.expected_regex is None:
50333
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
292 return True
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
293
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
294 expected_regexp = self.expected_regex
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
295 if isinstance(expected_regexp, basestring):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
296 expected_regexp = re.compile(expected_regexp)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
297 if not expected_regexp.search(str(exc_value)):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
298 raise self.failureException('"%s" does not match "%s"' %
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
299 (expected_regexp.pattern, str(exc_value)))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
300 return True
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
301
50333
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
302
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
303 class _AssertWrapper(object):
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
304 """Wrap entries in the _type_equality_funcs registry to make them deep
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
305 copyable."""
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
306
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
307 def __init__(self, function):
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
308 self.function = function
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
309
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
310 def __deepcopy__(self, memo):
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
311 memo[id(self)] = 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
312
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
313
50338
7c7624a33f8b Issue #2153: modernize coding style of unittest.py, remove obsolete compatibility stuff.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50333
diff changeset
314 class TestCase(object):
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
315 """A class whose instances are single test cases.
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
316
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
317 By default, the test code itself should be placed in a method named
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
318 'runTest'.
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
319
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17133
diff changeset
320 If the fixture may be used for many test cases, create as
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
321 many test methods as are needed. When instantiating such a TestCase
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
322 subclass, specify in the constructor arguments the name of the test method
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
323 that the instance is to execute.
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
324
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17133
diff changeset
325 Test authors should subclass TestCase for their own tests. Construction
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
326 and deconstruction of the test's environment ('fixture') can be
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
327 implemented by overriding the 'setUp' and 'tearDown' methods respectively.
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
328
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
329 If it is necessary to override the __init__ method, the base class
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
330 __init__ method must always be called. It is important that subclasses
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
331 should not change the signature of their __init__ method, since instances
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
332 of the classes are instantiated automatically by parts of the framework
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
333 in order to be run.
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
334 """
17440
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
335
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
336 # This attribute determines which exception will be raised when
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
337 # the instance's assertion methods fail; test methods raising this
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
338 # exception will be deemed to have 'failed' rather than 'errored'
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
339
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
340 failureException = AssertionError
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
341
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
342 # This attribute determines whether long messages (including repr of
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
343 # objects used in assert methods) will be printed on failure in *addition*
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
344 # to any explicit message passed.
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
345
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
346 longMessage = False
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
347
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
348
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
349 def __init__(self, methodName='runTest'):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
350 """Create an instance of the class that will use the named test
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
351 method when executed. Raises a ValueError if the instance does
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
352 not have a method with the specified name.
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
353 """
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
354 self._testMethodName = methodName
54234
eb9b1ac3d9d8 Rename TestCase._result to _resultForDoCleanups to avoid potential clashes in TestCase subclasses. Issue 6072.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 54019
diff changeset
355 self._resultForDoCleanups = None
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
356 try:
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
357 testMethod = getattr(self, methodName)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
358 except AttributeError:
50338
7c7624a33f8b Issue #2153: modernize coding style of unittest.py, remove obsolete compatibility stuff.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50333
diff changeset
359 raise ValueError("no such test method in %s: %s" % \
7c7624a33f8b Issue #2153: modernize coding style of unittest.py, remove obsolete compatibility stuff.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50333
diff changeset
360 (self.__class__, methodName))
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
361 self._testMethodDoc = testMethod.__doc__
53689
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
362 self._cleanups = []
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
363
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
364 # Map types to custom assertEqual functions that will compare
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
365 # instances of said type in more detail to generate a more useful
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
366 # error message.
52691
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
367 self._type_equality_funcs = {}
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
368 self.addTypeEqualityFunc(dict, self.assertDictEqual)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
369 self.addTypeEqualityFunc(list, self.assertListEqual)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
370 self.addTypeEqualityFunc(tuple, self.assertTupleEqual)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
371 self.addTypeEqualityFunc(set, self.assertSetEqual)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
372 self.addTypeEqualityFunc(frozenset, self.assertSetEqual)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
373
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
374 def addTypeEqualityFunc(self, typeobj, function):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
375 """Add a type specific assertEqual style function to compare a type.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
376
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
377 This method is for use by TestCase subclasses that need to register
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
378 their own type equality functions to provide nicer error messages.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
379
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
380 Args:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
381 typeobj: The data type to call this function on when both values
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
382 are of the same type in assertEqual().
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
383 function: The callable taking two arguments and an optional
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
384 msg= argument that raises self.failureException with a
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
385 useful error message when the two arguments are not equal.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
386 """
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
387 self._type_equality_funcs[typeobj] = _AssertWrapper(function)
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
388
53689
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
389 def addCleanup(self, function, *args, **kwargs):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
390 """Add a function, with arguments, to be called when the test is
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
391 completed. Functions added are called on a LIFO basis and are
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
392 called after tearDown on test failure or success.
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
393
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
394 Cleanup items are called even if setUp fails (unlike tearDown)."""
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
395 self._cleanups.append((function, args, kwargs))
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
396
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
397 def setUp(self):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
398 "Hook method for setting up the test fixture before exercising it."
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
399 pass
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
400
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
401 def tearDown(self):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
402 "Hook method for deconstructing the test fixture after testing it."
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
403 pass
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
404
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
405 def countTestCases(self):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
406 return 1
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
407
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
408 def defaultTestResult(self):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
409 return TestResult()
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
410
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
411 def shortDescription(self):
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
412 """Returns both the test method name and first line of its docstring.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
413
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
414 If no docstring is given, only returns the method name.
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
415
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
416 This method overrides unittest.TestCase.shortDescription(), which
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
417 only returns the first line of the docstring, obscuring the name
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
418 of the test upon failure.
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
419 """
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
420 desc = str(self)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
421 doc_first_line = None
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
422
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
423 if self._testMethodDoc:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
424 doc_first_line = self._testMethodDoc.split("\n")[0].strip()
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
425 if doc_first_line:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
426 desc = '\n'.join((desc, doc_first_line))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
427 return desc
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
428
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
429 def id(self):
36346
d48f4a4a306f Patch #1388073: Make unittest.TestCase easier to subclass
Georg Brandl <georg@python.org>
parents: 34551
diff changeset
430 return "%s.%s" % (_strclass(self.__class__), self._testMethodName)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
431
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
432 def __eq__(self, other):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
433 if type(self) is not type(other):
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
434 return NotImplemented
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
435
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
436 return self._testMethodName == other._testMethodName
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
437
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
438 def __ne__(self, other):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
439 return not self == other
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
440
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
441 def __hash__(self):
41096
1af38af314f2 Hashing simplification pointed out by Thomas Wouters.
Collin Winter <collinw@gmail.com>
parents: 41059
diff changeset
442 return hash((type(self), self._testMethodName))
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
443
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
444 def __str__(self):
36346
d48f4a4a306f Patch #1388073: Make unittest.TestCase easier to subclass
Georg Brandl <georg@python.org>
parents: 34551
diff changeset
445 return "%s (%s)" % (self._testMethodName, _strclass(self.__class__))
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
446
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
447 def __repr__(self):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
448 return "<%s testMethod=%s>" % \
36346
d48f4a4a306f Patch #1388073: Make unittest.TestCase easier to subclass
Georg Brandl <georg@python.org>
parents: 34551
diff changeset
449 (_strclass(self.__class__), self._testMethodName)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
450
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
451 def run(self, result=None):
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
452 orig_result = result
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
453 if result is None:
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
454 result = self.defaultTestResult()
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
455 startTestRun = getattr(result, 'startTestRun', None)
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
456 if startTestRun is not None:
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
457 startTestRun()
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
458
54234
eb9b1ac3d9d8 Rename TestCase._result to _resultForDoCleanups to avoid potential clashes in TestCase subclasses. Issue 6072.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 54019
diff changeset
459 self._resultForDoCleanups = result
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
460 result.startTest(self)
36346
d48f4a4a306f Patch #1388073: Make unittest.TestCase easier to subclass
Georg Brandl <georg@python.org>
parents: 34551
diff changeset
461 testMethod = getattr(self, self._testMethodName)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
462 try:
53689
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
463 success = False
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
464 try:
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
465 self.setUp()
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
466 except SkipTest as e:
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
467 result.addSkip(self, str(e))
50338
7c7624a33f8b Issue #2153: modernize coding style of unittest.py, remove obsolete compatibility stuff.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50333
diff changeset
468 except Exception:
52340
3fdc4aa36d76 remove uneeded function
Benjamin Peterson <benjamin@python.org>
parents: 52333
diff changeset
469 result.addError(self, sys.exc_info())
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
470 else:
53689
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
471 try:
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
472 testMethod()
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
473 except self.failureException:
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
474 result.addFailure(self, sys.exc_info())
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
475 except _ExpectedFailure as e:
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
476 result.addExpectedFailure(self, e.exc_info)
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
477 except _UnexpectedSuccess:
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
478 result.addUnexpectedSuccess(self)
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
479 except SkipTest as e:
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
480 result.addSkip(self, str(e))
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
481 except Exception:
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
482 result.addError(self, sys.exc_info())
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
483 else:
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
484 success = True
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
485
53689
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
486 try:
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
487 self.tearDown()
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
488 except Exception:
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
489 result.addError(self, sys.exc_info())
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
490 success = False
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
491
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
492 cleanUpSuccess = self.doCleanups()
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
493 success = success and cleanUpSuccess
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
494 if success:
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
495 result.addSuccess(self)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
496 finally:
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
497 result.stopTest(self)
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
498 if orig_result is None:
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
499 stopTestRun = getattr(result, 'stopTestRun', None)
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
500 if stopTestRun is not None:
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
501 stopTestRun()
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
502
53689
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
503 def doCleanups(self):
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
504 """Execute all cleanup functions. Normally called for you after
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
505 tearDown."""
54234
eb9b1ac3d9d8 Rename TestCase._result to _resultForDoCleanups to avoid potential clashes in TestCase subclasses. Issue 6072.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 54019
diff changeset
506 result = self._resultForDoCleanups
53689
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
507 ok = True
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
508 while self._cleanups:
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
509 function, args, kwargs = self._cleanups.pop(-1)
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
510 try:
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
511 function(*args, **kwargs)
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
512 except Exception:
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
513 ok = False
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
514 result.addError(self, sys.exc_info())
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
515 return ok
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
516
34094
24f0da920010 SF bug #1078905: Docs for unittest run() methods are misleading
Raymond Hettinger <python@rcn.com>
parents: 33930
diff changeset
517 def __call__(self, *args, **kwds):
24f0da920010 SF bug #1078905: Docs for unittest run() methods are misleading
Raymond Hettinger <python@rcn.com>
parents: 33930
diff changeset
518 return self.run(*args, **kwds)
30494
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
519
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
520 def debug(self):
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
521 """Run the test without collecting errors in a TestResult"""
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
522 self.setUp()
36346
d48f4a4a306f Patch #1388073: Make unittest.TestCase easier to subclass
Georg Brandl <georg@python.org>
parents: 34551
diff changeset
523 getattr(self, self._testMethodName)()
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
524 self.tearDown()
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
525
52351
557290ffa784 rename TestCase.skip() to skipTest() because it causes annoying problems with trial #5571
Benjamin Peterson <benjamin@python.org>
parents: 52340
diff changeset
526 def skipTest(self, reason):
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
527 """Skip this test."""
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
528 raise SkipTest(reason)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
529
17440
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
530 def fail(self, msg=None):
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
531 """Fail immediately, with the given message."""
50338
7c7624a33f8b Issue #2153: modernize coding style of unittest.py, remove obsolete compatibility stuff.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50333
diff changeset
532 raise self.failureException(msg)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
533
52563
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
534 def assertFalse(self, expr, msg=None):
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
535 "Fail the test if the expression is true."
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
536 if expr:
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
537 msg = self._formatMessage(msg, "%r is not False" % expr)
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
538 raise self.failureException(msg)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
539
52563
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
540 def assertTrue(self, expr, msg=None):
17440
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
541 """Fail the test unless the expression is true."""
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
542 if not expr:
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
543 msg = self._formatMessage(msg, "%r is not True" % expr)
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
544 raise self.failureException(msg)
17440
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
545
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
546 def _formatMessage(self, msg, standardMsg):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
547 """Honour the longMessage attribute when generating failure messages.
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
548 If longMessage is False this means:
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
549 * Use only an explicit message if it is provided
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
550 * Otherwise use the standard message for the assert
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
551
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
552 If longMessage is True:
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
553 * Use the standard message
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
554 * If an explicit message is provided, plus ' : ' and the explicit message
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
555 """
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
556 if not self.longMessage:
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
557 return msg or standardMsg
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
558 if msg is None:
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
559 return standardMsg
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
560 return standardMsg + ' : ' + msg
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
561
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
562
52563
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
563 def assertRaises(self, excClass, callableObj=None, *args, **kwargs):
17440
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
564 """Fail unless an exception of class excClass is thrown
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
565 by callableObj when invoked with arguments args and keyword
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
566 arguments kwargs. If a different type of exception is
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
567 thrown, it will not be caught, and the test case will be
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
568 deemed to have suffered an error, exactly as for an
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
569 unexpected exception.
50333
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
570
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
571 If called with callableObj omitted or None, will return a
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
572 context object used like this::
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
573
52563
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
574 with self.assertRaises(some_error_class):
50333
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
575 do_something()
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
576 """
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
577 context = _AssertRaisesContext(excClass, self)
50333
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
578 if callableObj is None:
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
579 return context
341bf76eb6eb Issue #4444: Allow assertRaises() to be used as a context handler.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48213
diff changeset
580 with context:
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 26823
diff changeset
581 callableObj(*args, **kwargs)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
582
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
583 def _getAssertEqualityFunc(self, first, second):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
584 """Get a detailed comparison function for the types of the two args.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
585
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
586 Returns: A callable accepting (first, second, msg=None) that will
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
587 raise a failure exception if first != second with a useful human
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
588 readable error message for those types.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
589 """
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
590 #
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
591 # NOTE(gregory.p.smith): I considered isinstance(first, type(second))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
592 # and vice versa. I opted for the conservative approach in case
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
593 # subclasses are not intended to be compared in detail to their super
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
594 # class instances using a type equality func. This means testing
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
595 # subtypes won't automagically use the detailed comparison. Callers
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
596 # should use their type specific assertSpamEqual method to compare
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
597 # subclasses if the detailed comparison is desired and appropriate.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
598 # See the discussion in http://bugs.python.org/issue2578.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
599 #
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
600 if type(first) is type(second):
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
601 asserter = self._type_equality_funcs.get(type(first))
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
602 if asserter is not None:
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
603 return asserter.function
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
604
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
605 return self._baseAssertEqual
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
606
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
607 def _baseAssertEqual(self, first, second, msg=None):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
608 """The default assertEqual implementation, not type specific."""
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
609 if not first == second:
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
610 standardMsg = '%r != %r' % (first, second)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
611 msg = self._formatMessage(msg, standardMsg)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
612 raise self.failureException(msg)
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
613
52563
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
614 def assertEqual(self, first, second, msg=None):
28506
117ace8521a1 SF bug #715145: unittest.py still uses != in failUnlessEqual
Raymond Hettinger <python@rcn.com>
parents: 28089
diff changeset
615 """Fail if the two objects are unequal as determined by the '=='
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
616 operator.
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
617 """
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
618 assertion_func = self._getAssertEqualityFunc(first, second)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
619 assertion_func(first, second, msg=msg)
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
620
52563
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
621 def assertNotEqual(self, first, second, msg=None):
17440
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
622 """Fail if the two objects are equal as determined by the '=='
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
623 operator.
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
624 """
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
625 if not first != second:
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
626 msg = self._formatMessage(msg, '%r == %r' % (first, second))
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
627 raise self.failureException(msg)
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
628
52563
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
629 def assertAlmostEqual(self, first, second, places=7, msg=None):
26823
2e2195fe6c28 Incorporate Skip's suggestion to use SciPy's validation test near
Raymond Hettinger <python@rcn.com>
parents: 25500
diff changeset
630 """Fail if the two objects are unequal as determined by their
2e2195fe6c28 Incorporate Skip's suggestion to use SciPy's validation test near
Raymond Hettinger <python@rcn.com>
parents: 25500
diff changeset
631 difference rounded to the given number of decimal places
2e2195fe6c28 Incorporate Skip's suggestion to use SciPy's validation test near
Raymond Hettinger <python@rcn.com>
parents: 25500
diff changeset
632 (default 7) and comparing to zero.
2e2195fe6c28 Incorporate Skip's suggestion to use SciPy's validation test near
Raymond Hettinger <python@rcn.com>
parents: 25500
diff changeset
633
30785
d996e2dcbd3a Incorporated patch 819077, from George Yoshida:
Steve Purcell <steve@pythonconsulting.com>
parents: 30767
diff changeset
634 Note that decimal places (from zero) are usually not the same
26823
2e2195fe6c28 Incorporate Skip's suggestion to use SciPy's validation test near
Raymond Hettinger <python@rcn.com>
parents: 25500
diff changeset
635 as significant digits (measured from the most signficant digit).
2e2195fe6c28 Incorporate Skip's suggestion to use SciPy's validation test near
Raymond Hettinger <python@rcn.com>
parents: 25500
diff changeset
636 """
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41096
diff changeset
637 if round(abs(second-first), places) != 0:
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
638 standardMsg = '%r != %r within %r places' % (first, second, places)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
639 msg = self._formatMessage(msg, standardMsg)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
640 raise self.failureException(msg)
26823
2e2195fe6c28 Incorporate Skip's suggestion to use SciPy's validation test near
Raymond Hettinger <python@rcn.com>
parents: 25500
diff changeset
641
52563
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
642 def assertNotAlmostEqual(self, first, second, places=7, msg=None):
26823
2e2195fe6c28 Incorporate Skip's suggestion to use SciPy's validation test near
Raymond Hettinger <python@rcn.com>
parents: 25500
diff changeset
643 """Fail if the two objects are equal as determined by their
2e2195fe6c28 Incorporate Skip's suggestion to use SciPy's validation test near
Raymond Hettinger <python@rcn.com>
parents: 25500
diff changeset
644 difference rounded to the given number of decimal places
2e2195fe6c28 Incorporate Skip's suggestion to use SciPy's validation test near
Raymond Hettinger <python@rcn.com>
parents: 25500
diff changeset
645 (default 7) and comparing to zero.
2e2195fe6c28 Incorporate Skip's suggestion to use SciPy's validation test near
Raymond Hettinger <python@rcn.com>
parents: 25500
diff changeset
646
30788
c2ae91a87a48 Another instance of the same typo.
Steve Purcell <steve@pythonconsulting.com>
parents: 30785
diff changeset
647 Note that decimal places (from zero) are usually not the same
26823
2e2195fe6c28 Incorporate Skip's suggestion to use SciPy's validation test near
Raymond Hettinger <python@rcn.com>
parents: 25500
diff changeset
648 as significant digits (measured from the most signficant digit).
2e2195fe6c28 Incorporate Skip's suggestion to use SciPy's validation test near
Raymond Hettinger <python@rcn.com>
parents: 25500
diff changeset
649 """
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 41096
diff changeset
650 if round(abs(second-first), places) == 0:
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
651 standardMsg = '%r == %r within %r places' % (first, second, places)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
652 msg = self._formatMessage(msg, standardMsg)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
653 raise self.failureException(msg)
26823
2e2195fe6c28 Incorporate Skip's suggestion to use SciPy's validation test near
Raymond Hettinger <python@rcn.com>
parents: 25500
diff changeset
654
30494
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
655 # Synonyms for assertion methods
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
656
52563
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
657 # The plurals are undocumented. Keep them that way to discourage use.
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
658 # Do not add more. Do not remove.
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
659 # Going through a deprecation cycle on these would annoy many people.
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
660 assertEquals = assertEqual
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
661 assertNotEquals = assertNotEqual
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
662 assertAlmostEquals = assertAlmostEqual
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
663 assertNotAlmostEquals = assertNotAlmostEqual
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
664 assert_ = assertTrue
26823
2e2195fe6c28 Incorporate Skip's suggestion to use SciPy's validation test near
Raymond Hettinger <python@rcn.com>
parents: 25500
diff changeset
665
52563
510ed057b95f Rename the actual method definitions to the official assertFoo names.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52540
diff changeset
666 # These fail* assertion method names are pending deprecation and will
52576
ab9b7d5db935 Issue an actual PendingDeprecationWarning for the TestCase.fail* methods.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52563
diff changeset
667 # be a DeprecationWarning in 3.2; http://bugs.python.org/issue2578
52691
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
668 def _deprecate(original_func):
52576
ab9b7d5db935 Issue an actual PendingDeprecationWarning for the TestCase.fail* methods.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52563
diff changeset
669 def deprecated_func(*args, **kwargs):
ab9b7d5db935 Issue an actual PendingDeprecationWarning for the TestCase.fail* methods.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52563
diff changeset
670 warnings.warn(
ab9b7d5db935 Issue an actual PendingDeprecationWarning for the TestCase.fail* methods.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52563
diff changeset
671 'Please use {0} instead.'.format(original_func.__name__),
ab9b7d5db935 Issue an actual PendingDeprecationWarning for the TestCase.fail* methods.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52563
diff changeset
672 PendingDeprecationWarning, 2)
ab9b7d5db935 Issue an actual PendingDeprecationWarning for the TestCase.fail* methods.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52563
diff changeset
673 return original_func(*args, **kwargs)
ab9b7d5db935 Issue an actual PendingDeprecationWarning for the TestCase.fail* methods.
Gregory P. Smith <greg@mad-scientist.com>
parents: 52563
diff changeset
674 return deprecated_func
17440
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
675
52691
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
676 failUnlessEqual = _deprecate(assertEqual)
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
677 failIfEqual = _deprecate(assertNotEqual)
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
678 failUnlessAlmostEqual = _deprecate(assertAlmostEqual)
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
679 failIfAlmostEqual = _deprecate(assertNotAlmostEqual)
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
680 failUnless = _deprecate(assertTrue)
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
681 failUnlessRaises = _deprecate(assertRaises)
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
682 failIf = _deprecate(assertFalse)
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
683
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
684 def assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
685 """An equality assertion for ordered sequences (like lists and tuples).
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
686
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
687 For the purposes of this function, a valid orderd sequence type is one
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
688 which can be indexed, has a length, and has an equality operator.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
689
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
690 Args:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
691 seq1: The first sequence to compare.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
692 seq2: The second sequence to compare.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
693 seq_type: The expected datatype of the sequences, or None if no
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
694 datatype should be enforced.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
695 msg: Optional message to use on failure instead of a list of
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
696 differences.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
697 """
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
698 if seq_type != None:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
699 seq_type_name = seq_type.__name__
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
700 if not isinstance(seq1, seq_type):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
701 raise self.failureException('First sequence is not a %s: %r'
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
702 % (seq_type_name, seq1))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
703 if not isinstance(seq2, seq_type):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
704 raise self.failureException('Second sequence is not a %s: %r'
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
705 % (seq_type_name, seq2))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
706 else:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
707 seq_type_name = "sequence"
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
708
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
709 differing = None
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
710 try:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
711 len1 = len(seq1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
712 except (TypeError, NotImplementedError):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
713 differing = 'First %s has no length. Non-sequence?' % (
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
714 seq_type_name)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
715
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
716 if differing is None:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
717 try:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
718 len2 = len(seq2)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
719 except (TypeError, NotImplementedError):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
720 differing = 'Second %s has no length. Non-sequence?' % (
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
721 seq_type_name)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
722
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
723 if differing is None:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
724 if seq1 == seq2:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
725 return
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
726
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
727 for i in xrange(min(len1, len2)):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
728 try:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
729 item1 = seq1[i]
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
730 except (TypeError, IndexError, NotImplementedError):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
731 differing = ('Unable to index element %d of first %s\n' %
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
732 (i, seq_type_name))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
733 break
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
734
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
735 try:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
736 item2 = seq2[i]
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
737 except (TypeError, IndexError, NotImplementedError):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
738 differing = ('Unable to index element %d of second %s\n' %
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
739 (i, seq_type_name))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
740 break
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
741
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
742 if item1 != item2:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
743 differing = ('First differing element %d:\n%s\n%s\n' %
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
744 (i, item1, item2))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
745 break
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
746 else:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
747 if (len1 == len2 and seq_type is None and
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
748 type(seq1) != type(seq2)):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
749 # The sequences are the same, but have differing types.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
750 return
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
751 # A catch-all message for handling arbitrary user-defined
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
752 # sequences.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
753 differing = '%ss differ:\n' % seq_type_name.capitalize()
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
754 if len1 > len2:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
755 differing = ('First %s contains %d additional '
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
756 'elements.\n' % (seq_type_name, len1 - len2))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
757 try:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
758 differing += ('First extra element %d:\n%s\n' %
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
759 (len2, seq1[len2]))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
760 except (TypeError, IndexError, NotImplementedError):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
761 differing += ('Unable to index element %d '
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
762 'of first %s\n' % (len2, seq_type_name))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
763 elif len1 < len2:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
764 differing = ('Second %s contains %d additional '
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
765 'elements.\n' % (seq_type_name, len2 - len1))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
766 try:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
767 differing += ('First extra element %d:\n%s\n' %
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
768 (len1, seq2[len1]))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
769 except (TypeError, IndexError, NotImplementedError):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
770 differing += ('Unable to index element %d '
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
771 'of second %s\n' % (len1, seq_type_name))
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
772 standardMsg = differing + '\n'.join(difflib.ndiff(pprint.pformat(seq1).splitlines(),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
773 pprint.pformat(seq2).splitlines()))
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
774 msg = self._formatMessage(msg, standardMsg)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
775 self.fail(msg)
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
776
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
777 def assertListEqual(self, list1, list2, msg=None):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
778 """A list-specific equality assertion.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
779
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
780 Args:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
781 list1: The first list to compare.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
782 list2: The second list to compare.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
783 msg: Optional message to use on failure instead of a list of
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
784 differences.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
785
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
786 """
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
787 self.assertSequenceEqual(list1, list2, msg, seq_type=list)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
788
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
789 def assertTupleEqual(self, tuple1, tuple2, msg=None):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
790 """A tuple-specific equality assertion.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
791
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
792 Args:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
793 tuple1: The first tuple to compare.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
794 tuple2: The second tuple to compare.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
795 msg: Optional message to use on failure instead of a list of
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
796 differences.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
797 """
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
798 self.assertSequenceEqual(tuple1, tuple2, msg, seq_type=tuple)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
799
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
800 def assertSetEqual(self, set1, set2, msg=None):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
801 """A set-specific equality assertion.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
802
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
803 Args:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
804 set1: The first set to compare.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
805 set2: The second set to compare.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
806 msg: Optional message to use on failure instead of a list of
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
807 differences.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
808
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
809 For more general containership equality, assertSameElements will work
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
810 with things other than sets. This uses ducktyping to support
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
811 different types of sets, and is optimized for sets specifically
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
812 (parameters must support a difference method).
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
813 """
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
814 try:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
815 difference1 = set1.difference(set2)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
816 except TypeError, e:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
817 self.fail('invalid type when attempting set difference: %s' % e)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
818 except AttributeError, e:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
819 self.fail('first argument does not support set difference: %s' % e)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
820
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
821 try:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
822 difference2 = set2.difference(set1)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
823 except TypeError, e:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
824 self.fail('invalid type when attempting set difference: %s' % e)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
825 except AttributeError, e:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
826 self.fail('second argument does not support set difference: %s' % e)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
827
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
828 if not (difference1 or difference2):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
829 return
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
830
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
831 lines = []
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
832 if difference1:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
833 lines.append('Items in the first set but not the second:')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
834 for item in difference1:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
835 lines.append(repr(item))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
836 if difference2:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
837 lines.append('Items in the second set but not the first:')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
838 for item in difference2:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
839 lines.append(repr(item))
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
840
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
841 standardMsg = '\n'.join(lines)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
842 self.fail(self._formatMessage(msg, standardMsg))
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
843
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
844 def assertIn(self, member, container, msg=None):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
845 """Just like self.assertTrue(a in b), but with a nicer default message."""
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
846 if member not in container:
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
847 standardMsg = '%r not found in %r' % (member, container)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
848 self.fail(self._formatMessage(msg, standardMsg))
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
849
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
850 def assertNotIn(self, member, container, msg=None):
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
851 """Just like self.assertTrue(a not in b), but with a nicer default message."""
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
852 if member in container:
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
853 standardMsg = '%r unexpectedly found in %r' % (member, container)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
854 self.fail(self._formatMessage(msg, standardMsg))
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
855
52894
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52858
diff changeset
856 def assertIs(self, expr1, expr2, msg=None):
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52858
diff changeset
857 """Just like self.assertTrue(a is b), but with a nicer default message."""
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52858
diff changeset
858 if expr1 is not expr2:
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52858
diff changeset
859 standardMsg = '%r is not %r' % (expr1, expr2)
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52858
diff changeset
860 self.fail(self._formatMessage(msg, standardMsg))
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52858
diff changeset
861
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52858
diff changeset
862 def assertIsNot(self, expr1, expr2, msg=None):
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52858
diff changeset
863 """Just like self.assertTrue(a is not b), but with a nicer default message."""
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52858
diff changeset
864 if expr1 is expr2:
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52858
diff changeset
865 standardMsg = 'unexpectedly identical: %r' % (expr1,)
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52858
diff changeset
866 self.fail(self._formatMessage(msg, standardMsg))
672b0c041ba6 Adding assertIs and assertIsNot methods to unittest.TestCase
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52858
diff changeset
867
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
868 def assertDictEqual(self, d1, d2, msg=None):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
869 self.assert_(isinstance(d1, dict), 'First argument is not a dictionary')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
870 self.assert_(isinstance(d2, dict), 'Second argument is not a dictionary')
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
871
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
872 if d1 != d2:
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
873 standardMsg = ('\n' + '\n'.join(difflib.ndiff(
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
874 pprint.pformat(d1).splitlines(),
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
875 pprint.pformat(d2).splitlines())))
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
876 self.fail(self._formatMessage(msg, standardMsg))
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
877
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
878 def assertDictContainsSubset(self, expected, actual, msg=None):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
879 """Checks whether actual is a superset of expected."""
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
880 missing = []
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
881 mismatched = []
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
882 for key, value in expected.iteritems():
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
883 if key not in actual:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
884 missing.append(key)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
885 elif value != actual[key]:
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
886 mismatched.append('%s, expected: %s, actual: %s' % (key, value, actual[key]))
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
887
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
888 if not (missing or mismatched):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
889 return
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
890
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
891 standardMsg = ''
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
892 if missing:
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
893 standardMsg = 'Missing: %r' % ','.join(missing)
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
894 if mismatched:
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
895 if standardMsg:
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
896 standardMsg += '; '
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
897 standardMsg += 'Mismatched values: %s' % ','.join(mismatched)
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
898
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
899 self.fail(self._formatMessage(msg, standardMsg))
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
900
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
901 def assertSameElements(self, expected_seq, actual_seq, msg=None):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
902 """An unordered sequence specific comparison.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
903
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
904 Raises with an error message listing which elements of expected_seq
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
905 are missing from actual_seq and vice versa if any.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
906 """
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
907 try:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
908 expected = set(expected_seq)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
909 actual = set(actual_seq)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
910 missing = list(expected.difference(actual))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
911 unexpected = list(actual.difference(expected))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
912 missing.sort()
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
913 unexpected.sort()
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
914 except TypeError:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
915 # Fall back to slower list-compare if any of the objects are
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
916 # not hashable.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
917 expected = list(expected_seq)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
918 actual = list(actual_seq)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
919 expected.sort()
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
920 actual.sort()
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
921 missing, unexpected = _SortedListDifference(expected, actual)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
922 errors = []
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
923 if missing:
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
924 errors.append('Expected, but missing:\n %r' % missing)
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
925 if unexpected:
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
926 errors.append('Unexpected, but present:\n %r' % unexpected)
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
927 if errors:
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
928 standardMsg = '\n'.join(errors)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
929 self.fail(self._formatMessage(msg, standardMsg))
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
930
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
931 def assertMultiLineEqual(self, first, second, msg=None):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
932 """Assert that two multi-line strings are equal."""
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
933 self.assert_(isinstance(first, basestring), (
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
934 'First argument is not a string'))
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
935 self.assert_(isinstance(second, basestring), (
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
936 'Second argument is not a string'))
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
937
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
938 if first != second:
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
939 standardMsg = '\n' + ''.join(difflib.ndiff(first.splitlines(True), second.splitlines(True)))
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
940 self.fail(self._formatMessage(msg, standardMsg))
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
941
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
942 def assertLess(self, a, b, msg=None):
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
943 """Just like self.assertTrue(a < b), but with a nicer default message."""
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
944 if not a < b:
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
945 standardMsg = '%r not less than %r' % (a, b)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
946 self.fail(self._formatMessage(msg, standardMsg))
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
947
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
948 def assertLessEqual(self, a, b, msg=None):
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
949 """Just like self.assertTrue(a <= b), but with a nicer default message."""
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
950 if not a <= b:
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
951 standardMsg = '%r not less than or equal to %r' % (a, b)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
952 self.fail(self._formatMessage(msg, standardMsg))
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
953
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
954 def assertGreater(self, a, b, msg=None):
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
955 """Just like self.assertTrue(a > b), but with a nicer default message."""
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
956 if not a > b:
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
957 standardMsg = '%r not greater than %r' % (a, b)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
958 self.fail(self._formatMessage(msg, standardMsg))
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
959
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
960 def assertGreaterEqual(self, a, b, msg=None):
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
961 """Just like self.assertTrue(a >= b), but with a nicer default message."""
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
962 if not a >= b:
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
963 standardMsg = '%r not greater than or equal to %r' % (a, b)
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
964 self.fail(self._formatMessage(msg, standardMsg))
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
965
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
966 def assertIsNone(self, obj, msg=None):
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
967 """Same as self.assertTrue(obj is None), with a nicer default message."""
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
968 if obj is not None:
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
969 standardMsg = '%r is not None' % obj
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
970 self.fail(self._formatMessage(msg, standardMsg))
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
971
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
972 def assertIsNotNone(self, obj, msg=None):
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
973 """Included for symmetry with assertIsNone."""
52719
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
974 if obj is None:
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
975 standardMsg = 'unexpectedly None'
1b50f78cddcb Better exception messages for unittest assert methods.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52691
diff changeset
976 self.fail(self._formatMessage(msg, standardMsg))
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
977
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
978 def assertRaisesRegexp(self, expected_exception, expected_regexp,
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
979 callable_obj=None, *args, **kwargs):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
980 """Asserts that the message in a raised exception matches a regexp.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
981
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
982 Args:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
983 expected_exception: Exception class expected to be raised.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
984 expected_regexp: Regexp (re pattern object or string) expected
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
985 to be found in error message.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
986 callable_obj: Function to be called.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
987 args: Extra args.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
988 kwargs: Extra kwargs.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
989 """
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
990 context = _AssertRaisesContext(expected_exception, self, expected_regexp)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
991 if callable_obj is None:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
992 return context
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
993 with context:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
994 callable_obj(*args, **kwargs)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
995
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
996 def assertRegexpMatches(self, text, expected_regex, msg=None):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
997 if isinstance(expected_regex, basestring):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
998 expected_regex = re.compile(expected_regex)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
999 if not expected_regex.search(text):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1000 msg = msg or "Regexp didn't match"
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1001 msg = '%s: %r not found in %r' % (msg, expected_regex.pattern, text)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1002 raise self.failureException(msg)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1003
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1004
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1005 def _SortedListDifference(expected, actual):
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1006 """Finds elements in only one or the other of two, sorted input lists.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1007
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1008 Returns a two-element tuple of lists. The first list contains those
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1009 elements in the "expected" list but not in the "actual" list, and the
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1010 second contains those elements in the "actual" list but not in the
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1011 "expected" list. Duplicate elements in either input list are ignored.
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1012 """
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1013 i = j = 0
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1014 missing = []
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1015 unexpected = []
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1016 while True:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1017 try:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1018 e = expected[i]
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1019 a = actual[j]
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1020 if e < a:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1021 missing.append(e)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1022 i += 1
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1023 while expected[i] == e:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1024 i += 1
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1025 elif e > a:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1026 unexpected.append(a)
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1027 j += 1
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1028 while actual[j] == a:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1029 j += 1
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1030 else:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1031 i += 1
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1032 try:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1033 while expected[i] == e:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1034 i += 1
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1035 finally:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1036 j += 1
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1037 while actual[j] == a:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1038 j += 1
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1039 except IndexError:
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1040 missing.extend(expected[i:])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1041 unexpected.extend(actual[j:])
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1042 break
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1043 return missing, unexpected
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 52351
diff changeset
1044
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1045
50338
7c7624a33f8b Issue #2153: modernize coding style of unittest.py, remove obsolete compatibility stuff.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50333
diff changeset
1046 class TestSuite(object):
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1047 """A test suite is a composite test consisting of a number of TestCases.
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1048
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1049 For use, create an instance of TestSuite, then add test case instances.
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1050 When all tests have been added, the suite can be passed to a test
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1051 runner, such as TextTestRunner. It will run the individual test cases
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1052 in the order in which they were added, aggregating the results. When
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1053 subclassing, do not forget to call the base class constructor.
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1054 """
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1055 def __init__(self, tests=()):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1056 self._tests = []
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1057 self.addTests(tests)
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1058
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1059 def __repr__(self):
52858
de5f18639a6c Change the way unittest.TestSuite use their tests to always access them through iteration. Non behavior changing, this allows you to create custom subclasses that override __iter__.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
1060 return "<%s tests=%s>" % (_strclass(self.__class__), list(self))
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1061
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1062 def __eq__(self, other):
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1063 if not isinstance(other, self.__class__):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1064 return NotImplemented
53664
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53238
diff changeset
1065 return list(self) == list(other)
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1066
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1067 def __ne__(self, other):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1068 return not self == other
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1069
48213
7527da2b05e8 Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 48019
diff changeset
1070 # Can't guarantee hash invariant, so flag as unhashable
7527da2b05e8 Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 48019
diff changeset
1071 __hash__ = None
7527da2b05e8 Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 48019
diff changeset
1072
33314
2f52df173952 Added an __iter__ method for test suites.
Jim Fulton <jim@zope.com>
parents: 31515
diff changeset
1073 def __iter__(self):
2f52df173952 Added an __iter__ method for test suites.
Jim Fulton <jim@zope.com>
parents: 31515
diff changeset
1074 return iter(self._tests)
2f52df173952 Added an __iter__ method for test suites.
Jim Fulton <jim@zope.com>
parents: 31515
diff changeset
1075
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1076 def countTestCases(self):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1077 cases = 0
52858
de5f18639a6c Change the way unittest.TestSuite use their tests to always access them through iteration. Non behavior changing, this allows you to create custom subclasses that override __iter__.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
1078 for test in self:
30494
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
1079 cases += test.countTestCases()
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1080 return cases
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1081
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1082 def addTest(self, test):
41059
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41054
diff changeset
1083 # sanity checks
47924
328f33baffa3 Suppress -3 warnings in unittest.py
Raymond Hettinger <python@rcn.com>
parents: 46890
diff changeset
1084 if not hasattr(test, '__call__'):
41059
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41054
diff changeset
1085 raise TypeError("the test to add must be callable")
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1086 if isinstance(test, type) and issubclass(test, (TestCase, TestSuite)):
41059
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41054
diff changeset
1087 raise TypeError("TestCases and TestSuites must be instantiated "
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41054
diff changeset
1088 "before passing them to addTest()")
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1089 self._tests.append(test)
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1090
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1091 def addTests(self, tests):
41059
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41054
diff changeset
1092 if isinstance(tests, basestring):
46cfabb7d544 Add some sanity checks to unittest.TestSuite's addTest(s) methods.
Georg Brandl <georg@python.org>
parents: 41054
diff changeset
1093 raise TypeError("tests must be an iterable of tests, not a string")
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1094 for test in tests:
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1095 self.addTest(test)
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1096
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1097 def run(self, result):
52858
de5f18639a6c Change the way unittest.TestSuite use their tests to always access them through iteration. Non behavior changing, this allows you to create custom subclasses that override __iter__.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
1098 for test in self:
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1099 if result.shouldStop:
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1100 break
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1101 test(result)
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1102 return result
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1103
34094
24f0da920010 SF bug #1078905: Docs for unittest run() methods are misleading
Raymond Hettinger <python@rcn.com>
parents: 33930
diff changeset
1104 def __call__(self, *args, **kwds):
24f0da920010 SF bug #1078905: Docs for unittest run() methods are misleading
Raymond Hettinger <python@rcn.com>
parents: 33930
diff changeset
1105 return self.run(*args, **kwds)
24f0da920010 SF bug #1078905: Docs for unittest run() methods are misleading
Raymond Hettinger <python@rcn.com>
parents: 33930
diff changeset
1106
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1107 def debug(self):
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1108 """Run the tests without collecting errors in a TestResult"""
52858
de5f18639a6c Change the way unittest.TestSuite use their tests to always access them through iteration. Non behavior changing, this allows you to create custom subclasses that override __iter__.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 52729
diff changeset
1109 for test in self:
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1110 test.debug()
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1111
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1112
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1113 class ClassTestSuite(TestSuite):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1114 """
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1115 Suite of tests derived from a single TestCase class.
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1116 """
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1117
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1118 def __init__(self, tests, class_collected_from):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1119 super(ClassTestSuite, self).__init__(tests)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1120 self.collected_from = class_collected_from
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1121
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1122 def id(self):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1123 module = getattr(self.collected_from, "__module__", None)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1124 if module is not None:
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1125 return "{0}.{1}".format(module, self.collected_from.__name__)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1126 return self.collected_from.__name__
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1127
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1128 def run(self, result):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1129 if getattr(self.collected_from, "__unittest_skip__", False):
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1130 # ClassTestSuite result pretends to be a TestCase enough to be
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1131 # reported.
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1132 result.startTest(self)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1133 try:
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1134 result.addSkip(self, self.collected_from.__unittest_skip_why__)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1135 finally:
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1136 result.stopTest(self)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1137 else:
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1138 result = super(ClassTestSuite, self).run(result)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1139 return result
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1140
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1141 shortDescription = id
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1142
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1143
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1144 class FunctionTestCase(TestCase):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1145 """A test case that wraps a test function.
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1146
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1147 This is useful for slipping pre-existing test functions into the
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1148 unittest framework. Optionally, set-up and tidy-up functions can be
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1149 supplied. As with TestCase, the tidy-up ('tearDown') function will
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1150 always be called if the set-up ('setUp') function ran successfully.
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1151 """
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1152
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1153 def __init__(self, testFunc, setUp=None, tearDown=None, description=None):
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1154 super(FunctionTestCase, self).__init__()
52691
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1155 self._setUpFunc = setUp
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1156 self._tearDownFunc = tearDown
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1157 self._testFunc = testFunc
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1158 self._description = description
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1159
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1160 def setUp(self):
52691
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1161 if self._setUpFunc is not None:
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1162 self._setUpFunc()
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1163
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1164 def tearDown(self):
52691
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1165 if self._tearDownFunc is not None:
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1166 self._tearDownFunc()
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1167
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1168 def runTest(self):
52691
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1169 self._testFunc()
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1170
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1171 def id(self):
52691
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1172 return self._testFunc.__name__
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1173
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1174 def __eq__(self, other):
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1175 if not isinstance(other, self.__class__):
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1176 return NotImplemented
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1177
52691
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1178 return self._setUpFunc == other._setUpFunc and \
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1179 self._tearDownFunc == other._tearDownFunc and \
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1180 self._testFunc == other._testFunc and \
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1181 self._description == other._description
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1182
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1183 def __ne__(self, other):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1184 return not self == other
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1185
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1186 def __hash__(self):
52691
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1187 return hash((type(self), self._setUpFunc, self._tearDownFunc,
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1188 self._testFunc, self._description))
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1189
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1190 def __str__(self):
53238
9755d8dd3366 fix a few nits in unittest.py #5771
Benjamin Peterson <benjamin@python.org>
parents: 52894
diff changeset
1191 return "%s (%s)" % (_strclass(self.__class__), self._testFunc.__name__)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1192
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1193 def __repr__(self):
52691
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1194 return "<%s testFunc=%s>" % (_strclass(self.__class__), self._testFunc)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1195
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1196 def shortDescription(self):
52691
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1197 if self._description is not None:
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1198 return self._description
2dfc33717493 remove double underscores
Benjamin Peterson <benjamin@python.org>
parents: 52576
diff changeset
1199 doc = self._testFunc.__doc__
30494
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
1200 return doc and doc.split("\n")[0].strip() or None
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1201
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1202
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1203
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1204 ##############################################################################
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1205 # Locating and loading tests
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1206 ##############################################################################
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1207
50338
7c7624a33f8b Issue #2153: modernize coding style of unittest.py, remove obsolete compatibility stuff.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50333
diff changeset
1208 class TestLoader(object):
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1209 """
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1210 This class is responsible for loading tests according to various criteria
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1211 and returning them wrapped in a TestSuite
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1212 """
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1213 testMethodPrefix = 'test'
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1214 sortTestMethodsUsing = cmp
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1215 suiteClass = TestSuite
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1216 classSuiteClass = ClassTestSuite
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1217
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1218 def loadTestsFromTestCase(self, testCaseClass):
17440
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
1219 """Return a suite of all tests cases contained in testCaseClass"""
33930
4fa1df3b062f Patch #1061904 / bug #878275: give a nicer error message when someone
Johannes Gijsbers <jlg@dds.nl>
parents: 33314
diff changeset
1220 if issubclass(testCaseClass, TestSuite):
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1221 raise TypeError("Test cases should not be derived from TestSuite." \
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1222 " Maybe you meant to derive from TestCase?")
30494
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
1223 testCaseNames = self.getTestCaseNames(testCaseClass)
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
1224 if not testCaseNames and hasattr(testCaseClass, 'runTest'):
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
1225 testCaseNames = ['runTest']
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1226 suite = self.classSuiteClass(map(testCaseClass, testCaseNames),
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1227 testCaseClass)
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1228 return suite
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1229
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1230 def loadTestsFromModule(self, module):
17440
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
1231 """Return a suite of all tests cases contained in the given module"""
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1232 tests = []
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1233 for name in dir(module):
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1234 obj = getattr(module, name)
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1235 if isinstance(obj, type) and issubclass(obj, TestCase):
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1236 tests.append(self.loadTestsFromTestCase(obj))
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1237 return self.suiteClass(tests)
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1238
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1239 def loadTestsFromName(self, name, module=None):
17440
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
1240 """Return a suite of all tests cases given a string specifier.
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
1241
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
1242 The name may resolve either to a module, a test case class, a
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
1243 test method within a test case class, or a callable object which
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
1244 returns a TestCase or TestSuite instance.
17467
4b9e684bebb8 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17440
diff changeset
1245
17440
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
1246 The method optionally resolves the names relative to a given module.
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
1247 """
30494
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
1248 parts = name.split('.')
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1249 if module is None:
30494
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
1250 parts_copy = parts[:]
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
1251 while parts_copy:
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
1252 try:
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
1253 module = __import__('.'.join(parts_copy))
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
1254 break
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
1255 except ImportError:
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
1256 del parts_copy[-1]
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1257 if not parts_copy:
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1258 raise
30767
790ed5c599c3 Fixed bug introduced in revision 1.27
Armin Rigo <arigo@tunes.org>
parents: 30600
diff changeset
1259 parts = parts[1:]
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1260 obj = module
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1261 for part in parts:
30494
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
1262 parent, obj = obj, getattr(obj, part)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1263
50338
7c7624a33f8b Issue #2153: modernize coding style of unittest.py, remove obsolete compatibility stuff.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50333
diff changeset
1264 if isinstance(obj, types.ModuleType):
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1265 return self.loadTestsFromModule(obj)
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1266 elif isinstance(obj, type) and issubclass(obj, TestCase):
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1267 return self.loadTestsFromTestCase(obj)
50338
7c7624a33f8b Issue #2153: modernize coding style of unittest.py, remove obsolete compatibility stuff.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50333
diff changeset
1268 elif (isinstance(obj, types.UnboundMethodType) and
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1269 isinstance(parent, type) and
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1270 issubclass(parent, TestCase)):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1271 return TestSuite([parent(obj.__name__)])
30785
d996e2dcbd3a Incorporated patch 819077, from George Yoshida:
Steve Purcell <steve@pythonconsulting.com>
parents: 30767
diff changeset
1272 elif isinstance(obj, TestSuite):
30494
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
1273 return obj
47924
328f33baffa3 Suppress -3 warnings in unittest.py
Raymond Hettinger <python@rcn.com>
parents: 46890
diff changeset
1274 elif hasattr(obj, '__call__'):
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1275 test = obj()
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1276 if isinstance(test, TestSuite):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1277 return test
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1278 elif isinstance(test, TestCase):
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1279 return TestSuite([test])
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1280 else:
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1281 raise TypeError("calling %s returned %s, not a test" %
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1282 (obj, test))
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1283 else:
41052
7da23bf8949f Patches #1550273, #1550272: fix a few bugs in unittest and add a
Georg Brandl <georg@python.org>
parents: 36346
diff changeset
1284 raise TypeError("don't know how to make test from: %s" % obj)
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1285
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1286 def loadTestsFromNames(self, names, module=None):
17440
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
1287 """Return a suite of all tests cases found using the given sequence
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
1288 of string specifiers. See 'loadTestsFromName()'.
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
1289 """
30494
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
1290 suites = [self.loadTestsFromName(name, module) for name in names]
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1291 return self.suiteClass(suites)
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1292
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1293 def getTestCaseNames(self, testCaseClass):
17440
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
1294 """Return a sorted sequence of method names found within testCaseClass
1cdff2838a17 - New fail*() methods, and comprehensive set of assert*() synonyms
Steve Purcell <steve@pythonconsulting.com>
parents: 17352
diff changeset
1295 """
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1296 def isTestMethod(attrname, testCaseClass=testCaseClass,
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1297 prefix=self.testMethodPrefix):
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1298 return attrname.startswith(prefix) and \
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1299 hasattr(getattr(testCaseClass, attrname), '__call__')
30494
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
1300 testFnNames = filter(isTestMethod, dir(testCaseClass))
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1301 if self.sortTestMethodsUsing:
47924
328f33baffa3 Suppress -3 warnings in unittest.py
Raymond Hettinger <python@rcn.com>
parents: 46890
diff changeset
1302 testFnNames.sort(key=_CmpToKey(self.sortTestMethodsUsing))
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1303 return testFnNames
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1304
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1305
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1306
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1307 defaultTestLoader = TestLoader()
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1308
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1309
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1310 ##############################################################################
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1311 # Patches for old functions: these functions should be considered obsolete
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1312 ##############################################################################
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1313
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1314 def _makeLoader(prefix, sortUsing, suiteClass=None):
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1315 loader = TestLoader()
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1316 loader.sortTestMethodsUsing = sortUsing
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1317 loader.testMethodPrefix = prefix
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1318 if suiteClass: loader.suiteClass = suiteClass
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1319 return loader
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1320
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1321 def getTestCaseNames(testCaseClass, prefix, sortUsing=cmp):
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1322 return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass)
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1323
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1324 def makeSuite(testCaseClass, prefix='test', sortUsing=cmp, suiteClass=TestSuite):
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1325 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(testCaseClass)
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1326
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1327 def findTestCases(module, prefix='test', sortUsing=cmp, suiteClass=TestSuite):
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1328 return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(module)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1329
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1330
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1331 ##############################################################################
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1332 # Text UI
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1333 ##############################################################################
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1334
50338
7c7624a33f8b Issue #2153: modernize coding style of unittest.py, remove obsolete compatibility stuff.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50333
diff changeset
1335 class _WritelnDecorator(object):
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1336 """Used to decorate file-like objects with a handy 'writeln' method"""
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1337 def __init__(self,stream):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1338 self.stream = stream
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1339
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1340 def __getattr__(self, attr):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1341 return getattr(self.stream,attr)
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1342
30420
13ec8c479ff1 SF bug #804115: bad argument handling(unittest.py)
Raymond Hettinger <python@rcn.com>
parents: 29846
diff changeset
1343 def writeln(self, arg=None):
52327
efffc7eaad6d another style nit
Benjamin Peterson <benjamin@python.org>
parents: 52326
diff changeset
1344 if arg:
efffc7eaad6d another style nit
Benjamin Peterson <benjamin@python.org>
parents: 52326
diff changeset
1345 self.write(arg)
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1346 self.write('\n') # text-mode streams translate to \r\n if needed
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17133
diff changeset
1347
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1348
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1349 class _TextTestResult(TestResult):
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1350 """A test result class that can print formatted text results to a stream.
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1351
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1352 Used by TextTestRunner.
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1353 """
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1354 separator1 = '=' * 70
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1355 separator2 = '-' * 70
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1356
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1357 def __init__(self, stream, descriptions, verbosity):
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1358 super(_TextTestResult, self).__init__()
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1359 self.stream = stream
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1360 self.showAll = verbosity > 1
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1361 self.dots = verbosity == 1
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1362 self.descriptions = descriptions
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1363
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1364 def getDescription(self, test):
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1365 if self.descriptions:
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1366 return test.shortDescription() or str(test)
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1367 else:
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1368 return str(test)
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1369
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1370 def startTest(self, test):
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1371 super(_TextTestResult, self).startTest(test)
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1372 if self.showAll:
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1373 self.stream.write(self.getDescription(test))
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1374 self.stream.write(" ... ")
46890
5ccd165f30a4 #2787: Flush stdout after writing test name, helpful when running
Georg Brandl <georg@python.org>
parents: 44432
diff changeset
1375 self.stream.flush()
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1376
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1377 def addSuccess(self, test):
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1378 super(_TextTestResult, self).addSuccess(test)
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1379 if self.showAll:
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1380 self.stream.writeln("ok")
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1381 elif self.dots:
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1382 self.stream.write('.')
46890
5ccd165f30a4 #2787: Flush stdout after writing test name, helpful when running
Georg Brandl <georg@python.org>
parents: 44432
diff changeset
1383 self.stream.flush()
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1384
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1385 def addError(self, test, err):
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1386 super(_TextTestResult, self).addError(test, err)
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1387 if self.showAll:
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1388 self.stream.writeln("ERROR")
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1389 elif self.dots:
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1390 self.stream.write('E')
46890
5ccd165f30a4 #2787: Flush stdout after writing test name, helpful when running
Georg Brandl <georg@python.org>
parents: 44432
diff changeset
1391 self.stream.flush()
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1392
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1393 def addFailure(self, test, err):
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1394 super(_TextTestResult, self).addFailure(test, err)
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1395 if self.showAll:
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1396 self.stream.writeln("FAIL")
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1397 elif self.dots:
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1398 self.stream.write('F')
46890
5ccd165f30a4 #2787: Flush stdout after writing test name, helpful when running
Georg Brandl <georg@python.org>
parents: 44432
diff changeset
1399 self.stream.flush()
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1400
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1401 def addSkip(self, test, reason):
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1402 super(_TextTestResult, self).addSkip(test, reason)
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1403 if self.showAll:
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1404 self.stream.writeln("skipped {0!r}".format(reason))
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1405 elif self.dots:
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1406 self.stream.write("s")
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1407 self.stream.flush()
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1408
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1409 def addExpectedFailure(self, test, err):
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1410 super(_TextTestResult, self).addExpectedFailure(test, err)
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1411 if self.showAll:
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1412 self.stream.writeln("expected failure")
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1413 elif self.dots:
52333
ee0f751b8020 add shorthands for expected failures and unexpected success
Benjamin Peterson <benjamin@python.org>
parents: 52327
diff changeset
1414 self.stream.write("x")
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1415 self.stream.flush()
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1416
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1417 def addUnexpectedSuccess(self, test):
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1418 super(_TextTestResult, self).addUnexpectedSuccess(test)
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1419 if self.showAll:
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1420 self.stream.writeln("unexpected success")
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1421 elif self.dots:
52333
ee0f751b8020 add shorthands for expected failures and unexpected success
Benjamin Peterson <benjamin@python.org>
parents: 52327
diff changeset
1422 self.stream.write("u")
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1423 self.stream.flush()
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1424
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1425 def printErrors(self):
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1426 if self.dots or self.showAll:
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1427 self.stream.writeln()
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1428 self.printErrorList('ERROR', self.errors)
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1429 self.printErrorList('FAIL', self.failures)
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1430
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1431 def printErrorList(self, flavour, errors):
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1432 for test, err in errors:
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1433 self.stream.writeln(self.separator1)
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1434 self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1435 self.stream.writeln(self.separator2)
19759
8e87cdded96f Changed TestResult to store only the text representation of an error.
Steve Purcell <steve@pythonconsulting.com>
parents: 19060
diff changeset
1436 self.stream.writeln("%s" % err)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1437
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1438
50338
7c7624a33f8b Issue #2153: modernize coding style of unittest.py, remove obsolete compatibility stuff.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50333
diff changeset
1439 class TextTestRunner(object):
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1440 """A test runner class that displays results in textual form.
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17133
diff changeset
1441
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1442 It prints out the names of tests as they are run, errors as they
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1443 occur, and a summary of the results at the end of the test run.
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1444 """
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1445 def __init__(self, stream=sys.stderr, descriptions=1, verbosity=1):
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1446 self.stream = _WritelnDecorator(stream)
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1447 self.descriptions = descriptions
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1448 self.verbosity = verbosity
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1449
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1450 def _makeResult(self):
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1451 return _TextTestResult(self.stream, self.descriptions, self.verbosity)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1452
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1453 def run(self, test):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1454 "Run the given test case or test suite."
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1455 result = self._makeResult()
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1456 startTime = time.time()
53695
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1457 startTestRun = getattr(result, 'startTestRun', None)
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1458 if startTestRun is not None:
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1459 startTestRun()
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1460 try:
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1461 test(result)
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1462 finally:
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1463 stopTestRun = getattr(result, 'stopTestRun', None)
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1464 if stopTestRun is not None:
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53689
diff changeset
1465 stopTestRun()
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1466 stopTime = time.time()
30785
d996e2dcbd3a Incorporated patch 819077, from George Yoshida:
Steve Purcell <steve@pythonconsulting.com>
parents: 30767
diff changeset
1467 timeTaken = stopTime - startTime
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1468 result.printErrors()
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1469 self.stream.writeln(result.separator2)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1470 run = result.testsRun
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1471 self.stream.writeln("Ran %d test%s in %.3fs" %
23592
d67f1e3a9f1a Fix printing plural (s or "").
Neal Norwitz <nnorwitz@gmail.com>
parents: 23487
diff changeset
1472 (run, run != 1 and "s" or "", timeTaken))
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1473 self.stream.writeln()
52300
68ac2552f44e comply with the evilJavaNamingScheme for attribute names
Benjamin Peterson <benjamin@python.org>
parents: 52297
diff changeset
1474 results = map(len, (result.expectedFailures,
68ac2552f44e comply with the evilJavaNamingScheme for attribute names
Benjamin Peterson <benjamin@python.org>
parents: 52297
diff changeset
1475 result.unexpectedSuccesses,
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1476 result.skipped))
52300
68ac2552f44e comply with the evilJavaNamingScheme for attribute names
Benjamin Peterson <benjamin@python.org>
parents: 52297
diff changeset
1477 expectedFails, unexpectedSuccesses, skipped = results
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1478 infos = []
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1479 if not result.wasSuccessful():
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1480 self.stream.write("FAILED")
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1481 failed, errored = map(len, (result.failures, result.errors))
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1482 if failed:
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1483 infos.append("failures=%d" % failed)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1484 if errored:
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1485 infos.append("errors=%d" % errored)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1486 else:
52326
3df7f118e504 fix newline issue in test summary
Benjamin Peterson <benjamin@python.org>
parents: 52316
diff changeset
1487 self.stream.write("OK")
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1488 if skipped:
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1489 infos.append("skipped=%d" % skipped)
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1490 if expectedFails:
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1491 infos.append("expected failures=%d" % expectedFails)
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1492 if unexpectedSuccesses:
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1493 infos.append("unexpected successes=%d" % unexpectedSuccesses)
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1494 if infos:
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1495 self.stream.writeln(" (%s)" % (", ".join(infos),))
52326
3df7f118e504 fix newline issue in test summary
Benjamin Peterson <benjamin@python.org>
parents: 52316
diff changeset
1496 else:
3df7f118e504 fix newline issue in test summary
Benjamin Peterson <benjamin@python.org>
parents: 52316
diff changeset
1497 self.stream.write("\n")
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1498 return result
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17133
diff changeset
1499
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1500
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1501
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1502 ##############################################################################
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1503 # Facilities for running tests from the command line
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1504 ##############################################################################
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1505
50338
7c7624a33f8b Issue #2153: modernize coding style of unittest.py, remove obsolete compatibility stuff.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50333
diff changeset
1506 class TestProgram(object):
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1507 """A command-line program that runs a set of tests; this is primarily
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1508 for making test modules conveniently executable.
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1509 """
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1510 USAGE = """\
17352
ed3e66f30837 * Remove exc_info() kludge -- it actually messed up the Jython output
Steve Purcell <steve@pythonconsulting.com>
parents: 17245
diff changeset
1511 Usage: %(progName)s [options] [test] [...]
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1512
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1513 Options:
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1514 -h, --help Show this message
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1515 -v, --verbose Verbose output
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1516 -q, --quiet Minimal output
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1517
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1518 Examples:
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1519 %(progName)s - run default set of tests
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1520 %(progName)s MyTestSuite - run suite 'MyTestSuite'
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1521 %(progName)s MyTestCase.testSomething - run MyTestCase.testSomething
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1522 %(progName)s MyTestCase - run all 'test*' test methods
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1523 in MyTestCase
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1524 """
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1525 def __init__(self, module='__main__', defaultTest=None,
41054
b092b2c83ab7 Patch #787789: allow to pass custom TestRunner instances to unittest's
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1526 argv=None, testRunner=TextTestRunner,
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
1527 testLoader=defaultTestLoader, exit=True,
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
1528 verbosity=1):
50338
7c7624a33f8b Issue #2153: modernize coding style of unittest.py, remove obsolete compatibility stuff.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50333
diff changeset
1529 if isinstance(module, basestring):
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1530 self.module = __import__(module)
30494
3f28964f8ecc - Fixed loading of tests by name when name refers to unbound
Steve Purcell <steve@pythonconsulting.com>
parents: 30424
diff changeset
1531 for part in module.split('.')[1:]:
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1532 self.module = getattr(self.module, part)
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1533 else:
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1534 self.module = module
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1535 if argv is None:
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1536 argv = sys.argv
53664
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53238
diff changeset
1537
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53238
diff changeset
1538 self.exit = exit
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
1539 self.verbosity = verbosity
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1540 self.defaultTest = defaultTest
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1541 self.testRunner = testRunner
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1542 self.testLoader = testLoader
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1543 self.progName = os.path.basename(argv[0])
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1544 self.parseArgs(argv)
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1545 self.runTests()
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1546
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1547 def usageExit(self, msg=None):
52310
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1548 if msg:
f0a507414e8c some cleanup and modernization
Benjamin Peterson <benjamin@python.org>
parents: 52305
diff changeset
1549 print msg
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1550 print self.USAGE % self.__dict__
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1551 sys.exit(2)
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1552
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1553 def parseArgs(self, argv):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1554 import getopt
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1555 long_opts = ['help','verbose','quiet']
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1556 try:
52297
e4aa530e5760 implement test skipping and expected failures
Benjamin Peterson <benjamin@python.org>
parents: 50338
diff changeset
1557 options, args = getopt.getopt(argv[1:], 'hHvq', long_opts)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1558 for opt, value in options:
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1559 if opt in ('-h','-H','--help'):
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1560 self.usageExit()
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1561 if opt in ('-q','--quiet'):
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1562 self.verbosity = 0
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1563 if opt in ('-v','--verbose'):
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1564 self.verbosity = 2
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1565 if len(args) == 0 and self.defaultTest is None:
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1566 self.test = self.testLoader.loadTestsFromModule(self.module)
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1567 return
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1568 if len(args) > 0:
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1569 self.testNames = args
54019
beac0b41cfb1 Better fix for modules using unittest.main(). Fixes regression caused by commit for issue 5995. Michael Foord
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 54018
diff changeset
1570 if sys.modules['unittest'] is sys.modules['__main__']:
54018
dfe5865e5f98 Fix to restore command line behaviour for test modules using unittest.main(). Regression caused by issue 5995. Michael
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 54006
diff changeset
1571 self.module = None
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1572 else:
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1573 self.testNames = (self.defaultTest,)
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1574 self.createTests()
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1575 except getopt.error, msg:
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1576 self.usageExit(msg)
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1577
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1578 def createTests(self):
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1579 self.test = self.testLoader.loadTestsFromNames(self.testNames,
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17108
diff changeset
1580 self.module)
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1581
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1582 def runTests(self):
41054
b092b2c83ab7 Patch #787789: allow to pass custom TestRunner instances to unittest's
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1583 if isinstance(self.testRunner, (type, types.ClassType)):
b092b2c83ab7 Patch #787789: allow to pass custom TestRunner instances to unittest's
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1584 try:
b092b2c83ab7 Patch #787789: allow to pass custom TestRunner instances to unittest's
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1585 testRunner = self.testRunner(verbosity=self.verbosity)
b092b2c83ab7 Patch #787789: allow to pass custom TestRunner instances to unittest's
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1586 except TypeError:
b092b2c83ab7 Patch #787789: allow to pass custom TestRunner instances to unittest's
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1587 # didn't accept the verbosity argument
b092b2c83ab7 Patch #787789: allow to pass custom TestRunner instances to unittest's
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1588 testRunner = self.testRunner()
b092b2c83ab7 Patch #787789: allow to pass custom TestRunner instances to unittest's
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1589 else:
b092b2c83ab7 Patch #787789: allow to pass custom TestRunner instances to unittest's
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1590 # it is assumed to be a TestRunner instance
b092b2c83ab7 Patch #787789: allow to pass custom TestRunner instances to unittest's
Georg Brandl <georg@python.org>
parents: 41052
diff changeset
1591 testRunner = self.testRunner
53664
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53238
diff changeset
1592 self.result = testRunner.run(self.test)
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53238
diff changeset
1593 if self.exit:
9d840a58a0d5 Adds an exit parameter to unittest.main(). If False main no longer
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53238
diff changeset
1594 sys.exit(not self.result.wasSuccessful())
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1595
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1596 main = TestProgram
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1597
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1598
53689
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
1599 ##############################################################################
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
1600 # Executing this module from the command line
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
1601 ##############################################################################
7bce052f0e7b Add addCleanup and doCleanups to unittest.TestCase.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 53664
diff changeset
1602
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1603 if __name__ == "__main__":
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
1604 sys.modules['unittest'] = sys.modules['__main__']
17108
33f7769a218e The unittest module from PyUNIT, by Steve Purcell.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1605 main(module=None)