[Python] How to assert if the two possibly NaN values are equal

Discussão em 'Python' iniciado por Stack, Julho 12, 2025.

  1. Stack

    Stack Membro Participativo

    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))

    Continue reading...

Compartilhe esta Página