views:

126

answers:

4

I have a function that returns a tuple that, among others, contains a float value. Usually I use assertAlmostEquals to compare those, but this does not work with tuples. Also, the tuple contains other data-types as well. Currently I am asserting every element of the tuple individually, but that gets too much for a list of such tuples. Is there any good way to write assertions for such cases? Consider this function:

def f(a):
    return [(1.0/x, x * 2) for x in a]

Now I want to write a test for it:

def testF(self):
    self.assertEqual(f(range(1,3)), [(1.0, 2), (0.5, 4)])

This will fail because the result of 1.0/2 is not exactly 0.5. Can anyone recommend a good way of writing such an assertion in a readable way?

Edit: Actually 1.0/2 is exactly 0.5, but you get my meaning.

+1  A: 

What I have done in the past is to write a custom-function that establishes validity for a complicated data type, and then used assert( IsFooValid( foo ) ). The validity function can simply return true/false, but it's usually better for it to raise AssertionError with an appropriate message.

Craig Trader
A: 

I'll probably define a recursive function.

from collections import Iterable;

def recursiveAssertAlmostEqual(testCase, first, second, *args, **kwargs):
   if isinstance(first, Iterable) and isinstance(second, Iterable):
      for a, b in zip(first, second):
         recursiveAssertAlmostEqual(testCase, a, b, *args, **kwargs)
   else:
      testCase.assertAlmostEqual(first, second, *args, **kwargs)

(Note that it will assert (1, 2) and [1, 2] are equal.)

KennyTM
A: 

This version of KennyTM's code checks for equality with assertEqual first, and only if that fails, tries assertAlmostEqual.

import unittest
import collections
import datetime

def f(a):
    return [(datetime.date(2000,1,1)+datetime.timedelta(days=x),1.0/x, x * 2) for x in a]

class Test(unittest.TestCase):
    def recursiveAssertAlmostEqual(self, first, second, *args, **kwargs):
       if (isinstance(first, collections.Iterable)
           and isinstance(second, collections.Iterable)):
           for a, b in zip(first, second):
               self.recursiveAssertAlmostEqual(a, b, *args, **kwargs)
       else:    
           try:
               self.assertEqual(first,second,*args,**kwargs)
           except self.failureException:           
               self.assertAlmostEqual(first, second, *args, **kwargs)

    def testF(self):
        self.recursiveAssertAlmostEqual(
            f(range(1,4)),[(datetime.date(2000,1,2),1.0, 2),
                           (datetime.date(2000,1,3),0.5, 4),
                           (datetime.date(2000,1,4),0.3333333, 6)])

if __name__ == '__main__':
    unittest.main(argv = unittest.sys.argv + ['--verbose'])
unutbu
+1  A: 

Well how about pimping up your function with couple of zips:

def testF(self):
    for tuple1, tuple2 in zip(f(range(1,3)), [(1.0, 2), (0.5, 4)]):
        for val1, val2 in zip(tuple1, tuple2):
            if type(val2) is float:
                self.assertAlmostEquals(val1, val2, 5)
            else:
                self.assertEquals(val1, val2)

My premise here is that it is better to use multiple asserts in a loop as to get the exact values where it breaks, vs. using single assert with all().

ps. If you have other numeric types you want to use assertAlmostEquals for, you can change the if above to e.g. if type(val2) in [float, decimal.Decimal]:

Nas Banov