Here are two variables: earnings_forecast
, actual_earning
(numerical variables)
I want to assert if both these variables are equal with a diffe
You can use the new isclose function introduced in Python 3.5
PEP 485 adds the math.isclose() and cmath.isclose() functions which tell whether two values are approximately equal or “close” to each other. Whether or not two values are considered close is determined according to given absolute and relative tolerances. Relative tolerance is the maximum allowed difference between isclose arguments, relative to the larger absolute value
import math
a = 100.0
b = 102.0
c = 103.0
assert math.isclose(a,b, rel_tol=0.02)
assert math.isclose(a,c, rel_tol=0.02)
Simply define a new test:
def assertNearlyEqual(self,a,b,fraction=0.02,msg=None):
if abs(a-b) > abs(fraction*a):
if msg is None:
self.fail("The given numbers %s and %s are not near each other."%(a,b))
else:
fail(msg)
and call it with your two variables:
self.assertNearlyEqual(earnings_forecast,actual_earning)
Simple approach:
a, b = sorted(map(float, (a, b)))
assert a + abs(a)*1.02 >= b
You can use a custom Testcase
subclass for use in tests:
class FooTestCase(TestCase):
def assertAlmostEqual(a, b):
a, b = sorted(map(float, (a, b)))
self.assertTrue(a + abs(a)*1.02 >= b)
For those who still use Python 2.x, you could also use numpy.isclose()
from numpy import isclose as isclose
a = 100.0
b = 100.01
print isclose(a,b, atol=0.02) # True
From the documentation:
For finite values, isclose uses the following equation to test whether two floating point values are equivalent.
absolute(a - b) <= (atol + rtol * absolute(b))
abs(earnings_forecast - actual_earning) < 0.01 * abs(earnings_forecast + actual_earning)
is a nice way of doing it, which gives you a good symmetric 2% difference on either side. It also doesn't suffer from pitfalls that can arise of one of the values is zero.
There are other definitions, but like the one above, they have their own pros and cons.