toqito icon indicating copy to clipboard operation
toqito copied to clipboard

Enhancement: Prefer `np.testing.array_equal` over `assertEqual` in testing

Open vprusso opened this issue 3 years ago • 1 comments

As pointed out by @theRoughCode in https://github.com/vprusso/toqito/issues/44, the np.testing.array_equal pattern should be preferred over the assertEqual pattern in the unit testing code. The reason for this preference is that the former provides more helpful testing data from failed instances in comparison with the latter. This task will involve finding instances of assertEqual and altering those to np.testing.array_equal where appropriate.

vprusso avatar May 19 '21 13:05 vprusso

Another thing to consider when fixing this is that this line might not be testing what was intended:

        # It may be possible for the lower bound to not be equal to the correct
        # quantum value (as it may get stuck in a local minimum), but it should
        # never be possible for the lower bound to attain a value higher than
        # the true quantum value.
        self.assertLessEqual(np.isclose(res, np.cos(np.pi / 8) ** 2, rtol=1e-02), True)

what would end up here is:

 self.assertLessEqual(True, True)

Instead, what we want might be something like this:

        expected = np.cos(np.pi / 8) ** 2
        self.assertLessEqual(res, expected)
        np.testing.assert_allclose(res, expected, rtol=1e-02)

However, I tried this but the "less than or equal to" check failed because res was slightly greater than expected:

E       AssertionError: 0.8535545972243008 not less than or equal to 0.8535533905932737

@vprusso Should we be truncating the values to a certain decimal place and then performing this check or is this not supposed to happen?

theRoughCode avatar May 19 '21 15:05 theRoughCode

We have decided to switch to using pytest instead of np.testing. Closing this as #488 has more details on what to do.

purva-thakre avatar Feb 24 '24 15:02 purva-thakre