-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Supporting arrays for approx #1994
Comments
I have been using Numpy's own testing functions for this, for example assert_almost_equal and assert_approx_equal: http://docs.scipy.org/doc/numpy/reference/routines.testing.html |
Pinging @edisongustavo |
current pytest behaviour is wrong, should we a) support numpy, or b) fail and defer to numpy functions im for b |
At work we implemented a fixture (named: So it supports the following: import numpy as np
def test(almost_equal):
assert almost_equal([1.2345, 12.3456], places=3) == [1.2349, 12.3459]
assert almost_equal(np.array([1.2345, 12.3456]), places=3) == [1.2349, 12.3459]
assert almost_equal(np.array([1.2345, 12.3456]), places=3) == np.array([1.2349, 12.3459]) It also supports floats (dhuh). |
Sure, a PR improving Pinging @kalekundert as well |
Sorry for the slow response. This looks really useful; I'll try to put a PR together this weekend. In response to @RonnyPfannschmidt, I think natively supporting numpy would be better than deferring to the numpy functions. The reason is that numpy uses a slightly different algorithm to determine whether two numbers are close or not. The difference is described in the docs, and is small enough that you'd probably never notice it, but I think it still makes sense to use the same algorithm for every type of input. |
precisely because numpy does approximation the numpy way is why i want to defer to numpy approximations for numpy data - so that tests testing numpy are consistent with numpy logic from my point of view having testing and real code use different approximation algorithmics is simply the worst thing you can inflict to numeric code as it pretty much puts correctness at stake |
The current syntax of pytest.approx doesn't work good with arrays. np.allclose must be used to compare arrays.
In
the first assert evaluates to true, the second to false.
Something like
Would be the most intuitive if it worked, as it also works with lists.
The text was updated successfully, but these errors were encountered: