Robot Framework Integrated Development Environment (RIDE)
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 unic
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, unic(err),
176  'Correct exception but wrong message')
177  else:
178  if hasattr(exc_class,'__name__'):
179  exc_name = exc_class.__name__
180  else:
181  exc_name = str(exc_class)
182  _report_failure('%s not raised' % exc_name)
183 
184 
185 
186 def assert_equal(first, second, msg=None, values=True, formatter=None):
187  if not first == second:
188  _report_inequality(first, second, '!=', msg, values, formatter)
189 
190 
191 
192 def assert_not_equal(first, second, msg=None, values=True, formatter=None):
193  if first == second:
194  _report_inequality(first, second, '==', msg, values, formatter)
195 
196 
197 
204 def assert_almost_equal(first, second, places=7, msg=None, values=True):
205  if round(second - first, places) != 0:
206  extra = 'within %r places' % places
207  _report_inequality(first, second, '!=', msg, values, extra=extra)
208 
209 
210 
217 def assert_not_almost_equal(first, second, places=7, msg=None, values=True):
218  if round(second-first, places) == 0:
219  extra = 'within %r places' % places
220  _report_inequality(first, second, '==', msg, values, extra=extra)
221 
222 
224  if msg is None:
225  raise AssertionError()
226  raise AssertionError(msg)
227 
228 
229 def _report_inequality(obj1, obj2, delim, msg=None, values=False,
230  formatter=None, extra=None):
231  if not msg:
232  msg = _format_message(obj1, obj2, delim, formatter)
233  elif values:
234  msg = '%s: %s' % (msg, _format_message(obj1, obj2, delim, formatter))
235  if values and extra:
236  msg += ' ' + extra
237  raise AssertionError(msg)
238 
239 
240 def _format_message(obj1, obj2, delim, formatter=None):
241  formatter = formatter or unic
242  str1 = formatter(obj1)
243  str2 = formatter(obj2)
244  if delim == '!=' and str1 == str2:
245  return '%s (%s) != %s (%s)' % (str1, type_name(obj1),
246  str2, type_name(obj2))
247  return '%s %s %s' % (str1, delim, str2)
def assert_false(expr, msg=None)
Fail the test if the expression is True.
Definition: asserts.py:107
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 assert_not_none(obj, msg=None, values=True)
Fail the test if given object is None.
Definition: asserts.py:119
def _format_message(obj1, obj2, delim, formatter=None)
Definition: asserts.py:240
def fail(msg=None)
Fail test immediately with the given message.
Definition: asserts.py:102
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_true(expr, msg=None)
Fail the test unless the expression is True.
Definition: asserts.py:113
def assert_not_equal(first, second, msg=None, values=True, formatter=None)
Fail if given objects are equal as determined by the '==' operator.
Definition: asserts.py:192
def assert_none(obj, msg=None, values=True)
Fail the test if given object is not None.
Definition: asserts.py:133
def assert_equal(first, second, msg=None, values=True, formatter=None)
Fail if given objects are unequal as determined by the '==' operator.
Definition: asserts.py:186
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:204
def _report_inequality(obj1, obj2, delim, msg=None, values=False, formatter=None, extra=None)
Definition: asserts.py:230
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:217