Robot Framework
asserts.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 
96 
97 from .robottypes import type_name
98 from .unic import safe_str
99 
100 
101 
102 def fail(msg=None):
103  _report_failure(msg)
104 
105 
106 
107 def assert_false(expr, msg=None):
108  if expr:
109  _report_failure(msg)
110 
111 
112 
113 def assert_true(expr, msg=None):
114  if not expr:
115  _report_failure(msg)
116 
117 
118 
119 def assert_not_none(obj, msg=None, values=True):
120 
123  _msg = 'is None'
124  if obj is None:
125  if msg is None:
126  msg = _msg
127  elif values is True:
128  msg = '%s: %s' % (msg, _msg)
129  _report_failure(msg)
130 
131 
132 
133 def assert_none(obj, msg=None, values=True):
134 
137  _msg = '%r is not None' % obj
138  if obj is not None:
139  if msg is None:
140  msg = _msg
141  elif values is True:
142  msg = '%s: %s' % (msg, _msg)
143  _report_failure(msg)
144 
145 
146 
156 def assert_raises(exc_class, callable_obj, *args, **kwargs):
157  try:
158  callable_obj(*args, **kwargs)
159  except exc_class as err:
160  return err
161  else:
162  if hasattr(exc_class,'__name__'):
163  exc_name = exc_class.__name__
164  else:
165  exc_name = str(exc_class)
166  _report_failure('%s not raised' % exc_name)
167 
168 
169 
170 def assert_raises_with_msg(exc_class, expected_msg, callable_obj, *args,
171  **kwargs):
172  try:
173  callable_obj(*args, **kwargs)
174  except exc_class as err:
175  assert_equal(expected_msg, str(err), 'Correct exception but wrong message')
176  else:
177  if hasattr(exc_class,'__name__'):
178  exc_name = exc_class.__name__
179  else:
180  exc_name = str(exc_class)
181  _report_failure('%s not raised' % exc_name)
182 
183 
184 
185 def assert_equal(first, second, msg=None, values=True, formatter=safe_str):
186  if not first == second:
187  _report_inequality(first, second, '!=', msg, values, formatter)
188 
189 
190 
191 def assert_not_equal(first, second, msg=None, values=True, formatter=safe_str):
192  if first == second:
193  _report_inequality(first, second, '==', msg, values, formatter)
194 
195 
196 
203 def assert_almost_equal(first, second, places=7, msg=None, values=True):
204  if round(second - first, places) != 0:
205  extra = 'within %r places' % places
206  _report_inequality(first, second, '!=', msg, values, extra=extra)
207 
208 
209 
216 def assert_not_almost_equal(first, second, places=7, msg=None, values=True):
217  if round(second-first, places) == 0:
218  extra = 'within %r places' % places
219  _report_inequality(first, second, '==', msg, values, extra=extra)
220 
221 
223  if msg is None:
224  raise AssertionError()
225  raise AssertionError(msg)
226 
227 
228 def _report_inequality(obj1, obj2, delim, msg=None, values=False, formatter=safe_str,
229  extra=None):
230  if not msg:
231  msg = _format_message(obj1, obj2, delim, formatter)
232  elif values:
233  msg = '%s: %s' % (msg, _format_message(obj1, obj2, delim, formatter))
234  if values and extra:
235  msg += ' ' + extra
236  raise AssertionError(msg)
237 
238 
239 def _format_message(obj1, obj2, delim, formatter=safe_str):
240  str1 = formatter(obj1)
241  str2 = formatter(obj2)
242  if delim == '!=' and str1 == str2:
243  return '%s (%s) != %s (%s)' % (str1, type_name(obj1),
244  str2, type_name(obj2))
245  return '%s %s %s' % (str1, delim, str2)
def assert_not_equal(first, second, msg=None, values=True, formatter=safe_str)
Fail if given objects are equal as determined by the '==' operator.
Definition: asserts.py:191
def assert_true(expr, msg=None)
Fail the test unless the expression is True.
Definition: asserts.py:113
def assert_almost_equal(first, second, places=7, msg=None, values=True)
Fail if the two objects are unequal after rounded to given places.
Definition: asserts.py:203
def assert_none(obj, msg=None, values=True)
Fail the test if given object is not None.
Definition: asserts.py:133
def _report_inequality(obj1, obj2, delim, msg=None, values=False, formatter=safe_str, extra=None)
Definition: asserts.py:229
def assert_raises(exc_class, callable_obj, *args, **kwargs)
Fail unless an exception of class exc_class is thrown by callable_obj.
Definition: asserts.py:156
def assert_not_almost_equal(first, second, places=7, msg=None, values=True)
Fail if the two objects are unequal after rounded to given places.
Definition: asserts.py:216
def _report_failure(msg)
Definition: asserts.py:222
def assert_raises_with_msg(exc_class, expected_msg, callable_obj, *args, **kwargs)
Similar to fail_unless_raises but also checks the exception message.
Definition: asserts.py:171
def _format_message(obj1, obj2, delim, formatter=safe_str)
Definition: asserts.py:239
def assert_not_none(obj, msg=None, values=True)
Fail the test if given object is None.
Definition: asserts.py:119
def fail(msg=None)
Fail test immediately with the given message.
Definition: asserts.py:102
def assert_equal(first, second, msg=None, values=True, formatter=safe_str)
Fail if given objects are unequal as determined by the '==' operator.
Definition: asserts.py:185
def assert_false(expr, msg=None)
Fail the test if the expression is True.
Definition: asserts.py:107
def type_name(item, capitalize=False)
Return "non-technical" type name for objects and types.
Definition: robottypes.py:86