Search code examples
pythonunit-testingassertnan

How to check if value is nan in unittest?


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…


Solution

  • I came up with

    assertTrue(math.isnan(nan_value))