unittest2pytest
unittest2pytest copied to clipboard
`pytest.approx` for `self.assertAlmostEqual`
The replacement for self.assertAlmostEqual(3, 3.0) is round(abs(3 - 3.0), 7) == 0.
How about replacing it with pytest.approx(3) == 3.0 instead? [pytest docs]
One big advantage of pytest.appox is that it correctly handles iterables like lists and tuples. Example:
xy = (1.0, 2.0)
assert round(abs(xy - (1, 2)), 7) == 0
fails with
TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'
whereas
xy = (1.0, 2.0)
assert pytest.approx(xy) == (1, 2)
passes.