1313
1414def fail (msg = None ):
1515 """Raise an AssertionError with the given message."""
16- raise AssertionError (msg )
16+ raise AssertionError (msg or "assertion failure" )
1717
1818
1919def assert_true (expr , msg = None ):
@@ -74,15 +74,16 @@ def assert_not_equal(first, second, msg=None):
7474
7575def assert_almost_equal (first , second , places = 7 , msg = None ):
7676 """Fail if the two objects are unequal when rounded."""
77- if round (second - first , places ) != 0 :
77+ if round (second - first , places ):
7878 fail (msg or "{!r} != {!r} within {} places" .format (first , second ,
7979 places ))
8080
8181
8282def assert_regex (text , regex , msg = None ):
8383 """Fail if actual does not match the regular expression expected."""
84- if not re .match (regex , text ):
85- fail (msg or "{!r} does not match {!r}" .format (text , regex ))
84+ compiled = re .compile (regex )
85+ if not compiled .search (text ):
86+ fail (msg or "{!r} does not match {!r}" .format (text , compiled .pattern ))
8687
8788
8889def assert_is (first , second , msg = None ):
@@ -121,31 +122,32 @@ def assert_is_instance(obj, cls, msg=None):
121122 msg = (msg if msg is not None else
122123 repr (obj ) + " is of " + repr (obj .__class__ ) +
123124 " not of " + repr (cls ))
124- raise AssertionError (msg )
125+ fail (msg )
125126
126127
127- def assert_has_attr (obj , attribute ):
128+ def assert_has_attr (obj , attribute , msg = None ):
128129 if not hasattr (obj , attribute ):
129- raise AssertionError (repr (obj ) + " is missing attribute '" +
130- attribute + "'" )
130+ fail (msg or repr (obj ) + " is missing attribute '" + attribute + "'" )
131131
132132
133133_EPSILON_SECONDS = 5
134134
135135
136- def assert_datetime_about_now (actual ):
136+ def assert_datetime_about_now (actual , msg = None ):
137137 now = datetime .now ()
138138 lower_bound = now - timedelta (seconds = _EPSILON_SECONDS )
139139 upper_bound = now + timedelta (seconds = _EPSILON_SECONDS )
140- msg = repr (actual ) + " is not close to current " + repr (now )
140+ if not msg :
141+ msg = repr (actual ) + " is not close to current " + repr (now )
141142 assert_between (lower_bound , upper_bound , actual , msg )
142143
143144
144- def assert_datetime_about_now_utc (actual ):
145+ def assert_datetime_about_now_utc (actual , msg = None ):
145146 now = datetime .utcnow ()
146147 lower_bound = now - timedelta (seconds = _EPSILON_SECONDS )
147148 upper_bound = now + timedelta (seconds = _EPSILON_SECONDS )
148- msg = repr (actual ) + " is not close to current UTC " + repr (now )
149+ if not msg :
150+ msg = repr (actual ) + " is not close to current UTC " + repr (now )
149151 assert_between (lower_bound , upper_bound , actual , msg )
150152
151153
@@ -173,16 +175,16 @@ def add_test(self, cb):
173175 """Add a test callback.
174176
175177 This callback is called after determining that the right exception
176- class was thrown .
178+ class was raised .
177179
178180 """
179181 self ._tests .append (cb )
180182
181183
182184def assert_raises (exception , msg = None ):
183- """Fail unless a specific exception is thrown inside the context.
185+ """Fail unless a specific exception is raised inside the context.
184186
185- If a different type of exception is thrown , it will not be caught.
187+ If a different type of exception is raised , it will not be caught.
186188
187189 """
188190 return AssertRaisesContext (exception , msg )
@@ -205,7 +207,7 @@ def test(exc):
205207
206208
207209def assert_raises_errno (exception , errno , msg = None ):
208- """Fail unless the context throws an exception of class exc_cls and
210+ """Fail unless the context raises an exception of class exc_cls and
209211 its errno attribute equals the supplied one.
210212
211213 """
@@ -217,7 +219,7 @@ def check_errno(exc):
217219 return context
218220
219221
220- def assert_succeeds (exception ):
222+ def assert_succeeds (exception , msg = None ):
221223 """Fail if an exception of the provided type is raised within the context.
222224
223225 This assertion should be used for cases, where successfully running a
@@ -249,6 +251,6 @@ def __enter__(self):
249251
250252 def __exit__ (self , exc_type , exc_val , exc_tb ):
251253 if exc_type and issubclass (exc_type , exception ):
252- fail (exception .__name__ + " was unexpectedly raised" )
254+ fail (msg or exception .__name__ + " was unexpectedly raised" )
253255
254256 return _AssertSucceeds ()
0 commit comments