How to check if value is nan in unittest?
Question:
I’ve got functions, which sometimes return NaNs with float('nan')
(I’m not using numpy).
How do I write a test for it, since
assertEqual(nan_value, float('nan'))
is just like float('nan') == float('nan')
always false. Is there maybe something like assertIsNan
? I could not find anything about it…
Answers:
I came up with
assertTrue(math.isnan(nan_value))
math.isnan(x)
will raise a TypeError
if x
is neither a float
nor a Real
.
It’s better to use something like this :
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
You can then use self.assertIsNaN()
and self.assertIsNotNaN()
.
Update with NumPy: I know the OP is not using numpy
. However, I had to use numpy
and didn’t find any post. So I’ll leave the answer here for anybody that may need help. It also works perfectly with unittest
library.
import numpy as np
np.testing.assert_equal(nan_value, np.nan)
Keying off of @user3503711’s answer, the following worked for me:
numpy.isnan(nan_value)
I am using numpy==1.22.2. See https://numpy.org/doc/1.22/reference/generated/numpy.isnan.html
I’ve got functions, which sometimes return NaNs with float('nan')
(I’m not using numpy).
How do I write a test for it, since
assertEqual(nan_value, float('nan'))
is just like float('nan') == float('nan')
always false. Is there maybe something like assertIsNan
? I could not find anything about it…
I came up with
assertTrue(math.isnan(nan_value))
math.isnan(x)
will raise a TypeError
if x
is neither a float
nor a Real
.
It’s better to use something like this :
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
You can then use self.assertIsNaN()
and self.assertIsNotNaN()
.
Update with NumPy: I know the OP is not using numpy
. However, I had to use numpy
and didn’t find any post. So I’ll leave the answer here for anybody that may need help. It also works perfectly with unittest
library.
import numpy as np
np.testing.assert_equal(nan_value, np.nan)
Keying off of @user3503711’s answer, the following worked for me:
numpy.isnan(nan_value)
I am using numpy==1.22.2. See https://numpy.org/doc/1.22/reference/generated/numpy.isnan.html