Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest.approx for self.assertAlmostEqual #63

Open
janosh opened this issue Aug 6, 2022 · 1 comment
Open

pytest.approx for self.assertAlmostEqual #63

janosh opened this issue Aug 6, 2022 · 1 comment

Comments

@janosh
Copy link
Contributor

janosh commented Aug 6, 2022

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
Copy link
Contributor Author

janosh commented Aug 6, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant