8000 DOC: TEST.rst: add example with `pytest.mark.parametrize` by mdhaber · Pull Request #24667 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

DOC: TEST.rst: add example with pytest.mark.parametrize #24667

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

Merged
merged 2 commits into from
Sep 21, 2023

Conversation

mdhaber
Copy link
Contributor
@mdhaber mdhaber commented Sep 7, 2023

The "Creating many similar tests" section of TEST.rst did not mention the use of pytest.mark.parametrize, which is a common and convenient way to run several similar tests. I also noticed the following about the example:

  • All instructions of test_single and test_double were identical aside from the dtype. This did not seem consistent with the intent of the example.
  • The example showed use of np.matrix, which is no longer recommended.
  • The example used a function imply, which was not defined.

This commit removes the "Creating many similar tests" section and expands the parametric tests section with an example that uses pytest.mark.parametrize. It also mentions NumPy's assert_equal, assert_allclose, and assert_less functions and makes other minor adjustments.

Closes gh-23283

The "Creating many similar tests" section of TEST.rst did not mention the use
of pytest.mark.parametrize, which is a very common and convenient way to
run several similar tests. I also noticed the following about the example:

- All instructions of `test_single` and `test_double` were identical aside
  from the dtype. This did not seem consistent with the intent of the
  example.
- The example showed use of `np.matrix`, which is no longer recommended.
- The example used a function `imply`, which was not defined.

This commit removes the "Creating many similar tests" section and expands the
parametric tests section with an example that uses pytest.mark.parametrize.
It also mentions NumPy's assert_equal, assert_allclose, and assert_less
functions and makes other minor adjustments.

[skip actions] [skip azp] [skip cirrus]
Copy link
Member
@seberg seberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, generally LGTM, its great to get rid of that weird class howto.

A few smaller comments, take or leave although it would be nice to see some of them addressed ;).

between a result array and a reference,
- :func:`numpy.testing.assert_allclose` for testing near elementwise equality
between a result array and a reference (i.e. with specified relative and
absolute tolerances), and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should advertise the _nulp versions more?

Copy link
Contributor Author
@mdhaber mdhaber Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. But actually, does NumPy 2.0 have plans to pare down the testing functions to those that are recommended?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no such plan. The usage of older testing functions is too high to do that I'm afraid. The functions that were deprecated/removed typically had a usage count of a handful, in some cases up to ~30 or so, in SciPy, scikit-learn and Pandas. I just did a quick count for assert_array_almost_equal and it's 2838 in SciPy alone - so we can't touch it really.

This topic can be better addressed in the docs though. This PR is a great step in the right direction.

Copy link
Contributor Author
@mdhaber mdhaber Sep 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Then how about listing assert_array_equal as "not recommended" following the plan suggested in #8457 (comment)?

doc/TESTS.rst Outdated
- :func:`numpy.testing.assert_array_less` for testing (strict) elementwise
ordering between a result array and a reference.

Note that these assertion functions only compare the numerical vales of the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Note that these assertion functions only compare the numerical vales of the
Note that these assertion functions only compare the numerical values of the

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now have strict=True on some (assert_equal?) to check a bit more (mainly the shape and probably the dtype).

Copy link
Contributor Author
@mdhaber mdhaber Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, assert_array_equal. But it's only that one, I think. How bout we add strict to the others and then I can mention it here? Update: see gh-24680.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That PR got merged, care to update the text too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'm happy to update this after adding strict to assert_equal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah missed that there was still another step, merging!

@ngoldbaum
Copy link
Member

This seems like a good improvement, just one comment but if you don't have time to update this is still a significant improvement over the status quo and should be merged.

[skip ci]

Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
@ngoldbaum ngoldbaum merged commit 08fbb43 into numpy:main Sep 21, 2023
@ngoldbaum
Copy link
Member

Thanks @mdhaber!

@mdhaber
Copy link
Contributor Author
mdhaber commented Sep 21, 2023

Squash merge (second commit is a one character touch-up without real commit message) would be great, and I'd be happy to come back and mention strict when I add it to the other functions, or ... oh, there it goes : ) thanks @ngoldbaum @seberg!

Comments on #8457 (comment) would be appreciated!

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

Successfully merging this pull request may close these issues.

DOC/TST: potential updates to NumPy Testing Guidelines?
4 participants
0