Best Python code snippet using assertpy_python
assertpyx.py
Source:assertpyx.py
...403404 COMPAREABLE_TYPES = set([datetime.datetime, datetime.timedelta, datetime.date, datetime.time])405 NON_COMPAREABLE_TYPES = set([complex])406 @log407 def _validate_compareable(self, other):408 self_type = type(self.val)409 other_type = type(other)410411 if self_type in self.NON_COMPAREABLE_TYPES:412 raise TypeError('ordering is not defined for type <%s>' % self_type.__name__)413 if self_type in self.COMPAREABLE_TYPES:414 if other_type is not self_type:415 raise TypeError('given arg must be <%s>, but was <%s>' % (self_type.__name__, other_type.__name__))416 return417 if isinstance(self.val, numbers.Number):418 if not isinstance(other, numbers.Number):419 raise TypeError('given arg must be a number, but was <%s>' % other_type.__name__)420 return421 raise TypeError('ordering is not defined for type <%s>' % self_type.__name__)422 @log423 def is_zero(self):424 """Asserts that val is numeric and equal to zero."""425 if isinstance(self.val, numbers.Number) is False:426 raise TypeError('val is not numeric')427 return self.is_equal_to(0)428 @log429 def is_not_zero(self):430 """Asserts that val is numeric and not equal to zero."""431 if isinstance(self.val, numbers.Number) is False:432 raise TypeError('val is not numeric')433 return self.is_not_equal_to(0)434 @log435 def is_greater_than(self, other):436 """Asserts that val is numeric and is greater than other."""437 self._validate_compareable(other)438 if self.val <= other:439 if type(self.val) is datetime.datetime:440 self._err('Expected <%s> to be greater than <%s>, but was not.' % (self.val.strftime('%Y-%m-%d %H:%M:%S'), other.strftime('%Y-%m-%d %H:%M:%S')))441 else:442 self._err('Expected <%s> to be greater than <%s>, but was not.' % (self.val, other))443 return self444 @log445 def is_greater_than_or_equal_to(self, other):446 """Asserts that val is numeric and is greater than or equal to other."""447 self._validate_compareable(other)448 if self.val < other:449 if type(self.val) is datetime.datetime:450 self._err('Expected <%s> to be greater than or equal to <%s>, but was not.' % (self.val.strftime('%Y-%m-%d %H:%M:%S'), other.strftime('%Y-%m-%d %H:%M:%S')))451 else:452 self._err('Expected <%s> to be greater than or equal to <%s>, but was not.' % (self.val, other))453 return self454 @log455 def is_less_than(self, other):456 """Asserts that val is numeric and is less than other."""457 self._validate_compareable(other)458 if self.val >= other:459 if type(self.val) is datetime.datetime:460 self._err('Expected <%s> to be less than <%s>, but was not.' % (self.val.strftime('%Y-%m-%d %H:%M:%S'), other.strftime('%Y-%m-%d %H:%M:%S')))461 else:462 self._err('Expected <%s> to be less than <%s>, but was not.' % (self.val, other))463 return self464 @log465 def is_less_than_or_equal_to(self, other):466 """Asserts that val is numeric and is less than or equal to other."""467 self._validate_compareable(other)468 if self.val > other:469 if type(self.val) is datetime.datetime:470 self._err('Expected <%s> to be less than or equal to <%s>, but was not.' % (self.val.strftime('%Y-%m-%d %H:%M:%S'), other.strftime('%Y-%m-%d %H:%M:%S')))471 else:472 self._err('Expected <%s> to be less than or equal to <%s>, but was not.' % (self.val, other))473 return self474 @log475 def is_positive(self):476 """Asserts that val is numeric and greater than zero."""477 return self.is_greater_than(0)478 @log479 def is_negative(self):480 """Asserts that val is numeric and less than zero."""481 return self.is_less_than(0)
...
assertpy.py
Source:assertpy.py
...269 return self270### numeric assertions ###271 COMPAREABLE_TYPES = set([datetime.datetime, datetime.timedelta, datetime.date, datetime.time])272 NON_COMPAREABLE_TYPES = set([complex])273 def _validate_compareable(self, other):274 self_type = type(self.val)275 other_type = type(other)276 if self_type in self.NON_COMPAREABLE_TYPES:277 raise TypeError('ordering is not defined for type <%s>' % self_type.__name__)278 if self_type in self.COMPAREABLE_TYPES:279 if other_type is not self_type:280 raise TypeError('given arg must be <%s>, but was <%s>' % (self_type.__name__, other_type.__name__))281 return282 if isinstance(self.val, numbers.Number):283 if not isinstance(other, numbers.Number):284 raise TypeError('given arg must be a number, but was <%s>' % other_type.__name__)285 return286 raise TypeError('ordering is not defined for type <%s>' % self_type.__name__)287 def is_zero(self):288 """Asserts that val is numeric and equal to zero."""289 if isinstance(self.val, numbers.Number) is False:290 raise TypeError('val is not numeric')291 return self.is_equal_to(0)292 def is_not_zero(self):293 """Asserts that val is numeric and not equal to zero."""294 if isinstance(self.val, numbers.Number) is False:295 raise TypeError('val is not numeric')296 return self.is_not_equal_to(0)297 def is_greater_than(self, other):298 """Asserts that val is numeric and is greater than other."""299 self._validate_compareable(other)300 if self.val <= other:301 if type(self.val) is datetime.datetime:302 self._err('Expected <%s> to be greater than <%s>, but was not.' % (self.val.strftime('%Y-%m-%d %H:%M:%S'), other.strftime('%Y-%m-%d %H:%M:%S')))303 else:304 self._err('Expected <%s> to be greater than <%s>, but was not.' % (self.val, other))305 return self306 def is_greater_than_or_equal_to(self, other):307 """Asserts that val is numeric and is greater than or equal to other."""308 self._validate_compareable(other)309 if self.val < other:310 if type(self.val) is datetime.datetime:311 self._err('Expected <%s> to be greater than or equal to <%s>, but was not.' % (self.val.strftime('%Y-%m-%d %H:%M:%S'), other.strftime('%Y-%m-%d %H:%M:%S')))312 else:313 self._err('Expected <%s> to be greater than or equal to <%s>, but was not.' % (self.val, other))314 return self315 def is_less_than(self, other):316 """Asserts that val is numeric and is less than other."""317 self._validate_compareable(other)318 if self.val >= other:319 if type(self.val) is datetime.datetime:320 self._err('Expected <%s> to be less than <%s>, but was not.' % (self.val.strftime('%Y-%m-%d %H:%M:%S'), other.strftime('%Y-%m-%d %H:%M:%S')))321 else:322 self._err('Expected <%s> to be less than <%s>, but was not.' % (self.val, other))323 return self324 def is_less_than_or_equal_to(self, other):325 """Asserts that val is numeric and is less than or equal to other."""326 self._validate_compareable(other)327 if self.val > other:328 if type(self.val) is datetime.datetime:329 self._err('Expected <%s> to be less than or equal to <%s>, but was not.' % (self.val.strftime('%Y-%m-%d %H:%M:%S'), other.strftime('%Y-%m-%d %H:%M:%S')))330 else:331 self._err('Expected <%s> to be less than or equal to <%s>, but was not.' % (self.val, other))332 return self333 def is_positive(self):334 """Asserts that val is numeric and greater than zero."""335 return self.is_greater_than(0)336 def is_negative(self):337 """Asserts that val is numeric and less than zero."""338 return self.is_less_than(0)339 def is_between(self, low, high):340 """Asserts that val is numeric and is between low and high."""...
numeric.py
Source:numeric.py
...34class NumericMixin(object):35 """Numeric assertions mixin."""36 _NUMERIC_COMPAREABLE = set([datetime.datetime, datetime.timedelta, datetime.date, datetime.time])37 _NUMERIC_NON_COMPAREABLE = set([complex])38 def _validate_compareable(self, other):39 self_type = type(self.val)40 other_type = type(other)41 if self_type in self._NUMERIC_NON_COMPAREABLE:42 raise TypeError('ordering is not defined for type <%s>' % self_type.__name__)43 if self_type in self._NUMERIC_COMPAREABLE:44 if other_type is not self_type:45 raise TypeError('given arg must be <%s>, but was <%s>' % (self_type.__name__, other_type.__name__))46 return47 if isinstance(self.val, numbers.Number):48 if not isinstance(other, numbers.Number):49 raise TypeError('given arg must be a number, but was <%s>' % other_type.__name__)50 return51 raise TypeError('ordering is not defined for type <%s>' % self_type.__name__)52 def _validate_number(self):53 """Raise TypeError if val is not numeric."""54 if isinstance(self.val, numbers.Number) is False:55 raise TypeError('val is not numeric')56 def _validate_real(self):57 """Raise TypeError if val is not real number."""58 if isinstance(self.val, numbers.Real) is False:59 raise TypeError('val is not real number')60 def is_zero(self):61 """Asserts that val is numeric and is zero.62 Examples:63 Usage::64 assert_that(0).is_zero()65 Returns:66 AssertionBuilder: returns this instance to chain to the next assertion67 Raises:68 AssertionError: if val is **not** zero69 """70 self._validate_number()71 return self.is_equal_to(0)72 def is_not_zero(self):73 """Asserts that val is numeric and is *not* zero.74 Examples:75 Usage::76 assert_that(1).is_not_zero()77 assert_that(123.4).is_not_zero()78 Returns:79 AssertionBuilder: returns this instance to chain to the next assertion80 Raises:81 AssertionError: if val **is** zero82 """83 self._validate_number()84 return self.is_not_equal_to(0)85 def is_nan(self):86 """Asserts that val is real number and is ``NaN`` (not a number).87 Examples:88 Usage::89 assert_that(float('nan')).is_nan()90 assert_that(float('inf') * 0).is_nan()91 Returns:92 AssertionBuilder: returns this instance to chain to the next assertion93 Raises:94 AssertionError: if val is **not** NaN95 """96 self._validate_number()97 self._validate_real()98 if not math.isnan(self.val):99 return self.error('Expected <%s> to be <NaN>, but was not.' % self.val)100 return self101 def is_not_nan(self):102 """Asserts that val is real number and is *not* ``NaN`` (not a number).103 Examples:104 Usage::105 assert_that(0).is_not_nan()106 assert_that(123.4).is_not_nan()107 assert_that(float('inf')).is_not_nan()108 Returns:109 AssertionBuilder: returns this instance to chain to the next assertion110 Raises:111 AssertionError: if val **is** NaN112 """113 self._validate_number()114 self._validate_real()115 if math.isnan(self.val):116 return self.error('Expected not <NaN>, but was.')117 return self118 def is_inf(self):119 """Asserts that val is real number and is ``Inf`` (infinity).120 Examples:121 Usage::122 assert_that(float('inf')).is_inf()123 assert_that(float('inf') * 1).is_inf()124 Returns:125 AssertionBuilder: returns this instance to chain to the next assertion126 Raises:127 AssertionError: if val is **not** Inf128 """129 self._validate_number()130 self._validate_real()131 if not math.isinf(self.val):132 return self.error('Expected <%s> to be <Inf>, but was not.' % self.val)133 return self134 def is_not_inf(self):135 """Asserts that val is real number and is *not* ``Inf`` (infinity).136 Examples:137 Usage::138 assert_that(0).is_not_inf()139 assert_that(123.4).is_not_inf()140 assert_that(float('nan')).is_not_inf()141 Returns:142 AssertionBuilder: returns this instance to chain to the next assertion143 Raises:144 AssertionError: if val **is** Inf145 """146 self._validate_number()147 self._validate_real()148 if math.isinf(self.val):149 return self.error('Expected not <Inf>, but was.')150 return self151 def is_greater_than(self, other):152 """Asserts that val is numeric and is greater than other.153 Args:154 other: the other date, expected to be less than val155 Examples:156 Usage::157 assert_that(1).is_greater_than(0)158 assert_that(123.4).is_greater_than(111.1)159 For dates, behavior is identical to :meth:`~assertpy.date.DateMixin.is_after`::160 import datetime161 today = datetime.datetime.now()162 yesterday = today - datetime.timedelta(days=1)163 assert_that(today).is_greater_than(yesterday)164 Returns:165 AssertionBuilder: returns this instance to chain to the next assertion166 Raises:167 AssertionError: if val is **not** greater than other168 """169 self._validate_compareable(other)170 if self.val <= other:171 if type(self.val) is datetime.datetime:172 return self.error('Expected <%s> to be greater than <%s>, but was not.' % (173 self.val.strftime('%Y-%m-%d %H:%M:%S'), other.strftime('%Y-%m-%d %H:%M:%S')))174 else:175 return self.error('Expected <%s> to be greater than <%s>, but was not.' % (self.val, other))176 return self177 def is_greater_than_or_equal_to(self, other):178 """Asserts that val is numeric and is greater than or equal to other.179 Args:180 other: the other date, expected to be less than or equal to val181 Examples:182 Usage::183 assert_that(1).is_greater_than_or_equal_to(0)184 assert_that(1).is_greater_than_or_equal_to(1)185 assert_that(123.4).is_greater_than_or_equal_to(111.1)186 For dates, behavior is identical to :meth:`~assertpy.date.DateMixin.is_after` *except* when equal::187 import datetime188 today = datetime.datetime.now()189 yesterday = today - datetime.timedelta(days=1)190 assert_that(today).is_greater_than_or_equal_to(yesterday)191 assert_that(today).is_greater_than_or_equal_to(today)192 Returns:193 AssertionBuilder: returns this instance to chain to the next assertion194 Raises:195 AssertionError: if val is **not** greater than or equal to other196 """197 self._validate_compareable(other)198 if self.val < other:199 if type(self.val) is datetime.datetime:200 return self.error('Expected <%s> to be greater than or equal to <%s>, but was not.' % (201 self.val.strftime('%Y-%m-%d %H:%M:%S'), other.strftime('%Y-%m-%d %H:%M:%S')))202 else:203 return self.error('Expected <%s> to be greater than or equal to <%s>, but was not.' % (self.val, other))204 return self205 def is_less_than(self, other):206 """Asserts that val is numeric and is less than other.207 Args:208 other: the other date, expected to be greater than val209 Examples:210 Usage::211 assert_that(0).is_less_than(1)212 assert_that(123.4).is_less_than(555.5)213 For dates, behavior is identical to :meth:`~assertpy.date.DateMixin.is_before`::214 import datetime215 today = datetime.datetime.now()216 yesterday = today - datetime.timedelta(days=1)217 assert_that(yesterday).is_less_than(today)218 Returns:219 AssertionBuilder: returns this instance to chain to the next assertion220 Raises:221 AssertionError: if val is **not** less than other222 """223 self._validate_compareable(other)224 if self.val >= other:225 if type(self.val) is datetime.datetime:226 return self.error('Expected <%s> to be less than <%s>, but was not.' % (self.val.strftime('%Y-%m-%d %H:%M:%S'), other.strftime('%Y-%m-%d %H:%M:%S')))227 else:228 return self.error('Expected <%s> to be less than <%s>, but was not.' % (self.val, other))229 return self230 def is_less_than_or_equal_to(self, other):231 """Asserts that val is numeric and is less than or equal to other.232 Args:233 other: the other date, expected to be greater than or equal to val234 Examples:235 Usage::236 assert_that(1).is_less_than_or_equal_to(0)237 assert_that(1).is_less_than_or_equal_to(1)238 assert_that(123.4).is_less_than_or_equal_to(100.0)239 For dates, behavior is identical to :meth:`~assertpy.date.DateMixin.is_before` *except* when equal::240 import datetime241 today = datetime.datetime.now()242 yesterday = today - datetime.timedelta(days=1)243 assert_that(yesterday).is_less_than_or_equal_to(today)244 assert_that(today).is_less_than_or_equal_to(today)245 Returns:246 AssertionBuilder: returns this instance to chain to the next assertion247 Raises:248 AssertionError: if val is **not** less than or equal to other249 """250 self._validate_compareable(other)251 if self.val > other:252 if type(self.val) is datetime.datetime:253 return self.error('Expected <%s> to be less than or equal to <%s>, but was not.' % (254 self.val.strftime('%Y-%m-%d %H:%M:%S'), other.strftime('%Y-%m-%d %H:%M:%S')))255 else:256 return self.error('Expected <%s> to be less than or equal to <%s>, but was not.' % (self.val, other))257 return self258 def is_positive(self):259 """Asserts that val is numeric and is greater than zero.260 Examples:261 Usage::262 assert_that(1).is_positive()263 assert_that(123.4).is_positive()264 Returns:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!