Krzysztof Słowiński
Krzysztof Słowiński

Reputation: 7227

unittest - how to assert if the two possibly NaN values are equal

In my test case, I assume that if two values are NaN then they are equal. What is the way to express it using unittest assertions? The two common functions presented below are not handling this case.

v1 = np.nan
v2 = np.nan
self.assertEquals(v1, v2)
self.assertTrue(v1 == v2)

A solution that is working for me right now is using a boolean expression inside assertTrue:

self.assertTrue(v1 == v2 or (np.isnan(v1) and np.isnan(v2))

Upvotes: 11

Views: 9950

Answers (6)

Dmitry
Dmitry

Reputation: 2993

For comparing python data structures (e.g. with assertListEqual/assertDictEqual) which might contain NaN values, the following expression works in place of NaN constant:

pytest.approx(np.nan, nan_ok=True)

Upvotes: 1

marcin
marcin

Reputation: 3571

You could use:

numpy.testing.assert_equal(v1, v2)

From docs:

This function handles NaN comparisons as if NaN was a “normal” number. That is, no assertion is raised if both objects have NaNs in the same positions. This is in contrast to the IEEE standard on NaNs, which says that NaN compared to anything must return False.

It throws AssertionError when the values are not equal and it should work fine with pytest, but it may not be a good fit for unittest tests.

Another option is:

numpy.isclose(v1, v2, equal_nan=True)

but obviously it's a replacement for math.isclose, not for ==.

Upvotes: 16

keepAlive
keepAlive

Reputation: 6655

A standard way of checking for nans is

assert (v1 != v1) and (v2 != v2)

i.e. something that is not equal to itself, e.g:

>>> n = float('nan')
>>> n != n
True

Upvotes: 1

haklir
haklir

Reputation: 86

You can use math.isnan

self.assertTrue(math.isnan(v1) and math.isnan(v2))

Upvotes: 3

ibarrond
ibarrond

Reputation: 7591

You could check if each of them is NaN separately. To do that, I suggest using the following class:

import math


class NumericAssertions:
    """
    This class is following the UnitTest naming conventions.
    It is meant to be used along with unittest.TestCase like so :
    class MyTest(unittest.TestCase, NumericAssertions):
        ...
    It needs python >= 2.6
    """

    def assertIsNaN(self, value, msg=None):
        """Fail if provided value is not NaN"""
        standardMsg = "%s is not NaN" % str(value)
        try:
            if not math.isnan(value):
                self.fail(self._formatMessage(msg, standardMsg))
        except:
            self.fail(self._formatMessage(msg, standardMsg))

    def assertIsNotNaN(self, value, msg=None):
        """Fail if provided value is NaN"""
        standardMsg = "Provided value is NaN"
        try:
            if math.isnan(value):
                self.fail(self._formatMessage(msg, standardMsg))
        except:
            pass

It would be as easy as:

v1 = np.nan
v2 = np.nan
self.assertIsNaN(v1)
self.assertIsNaN(v2)

Upvotes: 1

toti08
toti08

Reputation: 2454

It does not make sense to check two NaNs for equality. The best you could do is to check it in a different way, for example:

check = numpy.isnan(v1) and numpy.isnan(v2)
self.assertTrue(check)

Upvotes: 5

Related Questions