[TIP] newb - nose and floating point values

Michał Kwiatkowski constant.beta at gmail.com
Sun Feb 24 16:36:45 PST 2008

On Mon, Feb 25, 2008 at 1:11 AM, Carl Trachte <ctrachte at gmail.com> wrote:
> Sorry if this is an FAQ; I googled and came up empty:
> Is this the appropriate way to test for floating point return values from a
> function (with nose)?

Unittest module has assertAlmostEqual method
(http://docs.python.org/lib/testcase-objects.html#l2h-5025), which
does exactly what you need:

import unittest

def addtwonumbers(num1, num2):
    return num1 + num2

class TestAddTwoNumber(unittest.TestCase):
    def test_addtwonumbers(self):
        self.assertEqual(10, addtwonumbers(4, 6))
        self.assertEqual(40, addtwonumbers(8, 32))
        self.assertAlmostEqual(2.2, addtwonumbers(4.3, -2.1))


More information about the testing-in-python mailing list