unyt
unyt copied to clipboard
Tests may not check that units are correct
While working on #280, I noticed that both numpy.testing.assert_equal and numpy.testing.assert_array_equal, which are both used extensively in the unyt codebase, don't check units if one of the arguments is dimensionless. For example, this code runs without error:
from numpy.testing import assert_equal, assert_array_equal
import unyt as u
assert_equal(1*u.g, 1*u.dimensionless)
assert_equal(1*u.dimensionless, 1*u.g)
assert_equal(1*u.g, 1)
assert_array_equal([1, 2]*u.g, [1, 2]*u.dimensionless)
assert_array_equal([1, 2]*u.dimensionless, [1, 2]*u.g)
assert_array_equal([1, 2]*u.g, [1, 2])
IMO all of these examples would ideally raise an AssertionError. There are probably cases in the unyt codebase where we are unintentionally relying on this behavior in tests, masking bugs or broken tests.
In #280 I added a new test helper that should avoid this issue if we do a codebase-wide change to use it instead of the assertion code we don't control in numpy:
def assert_array_equal_units(a, b):
assert_array_equal(a, b)
assert_equal(a.units, b.units)