pytest-dev / unittest2pytest

helps rewriting Python `unittest` test-cases into `pytest` test-cases
GNU General Public License v3.0
128 stars 27 forks source link

`pytest.approx` for `self.assertAlmostEqual` #63

Open janosh opened 2 years ago

janosh commented 2 years ago

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]

janosh commented 2 years ago

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.