diff options
author | Pamphile ROY <roy.pamphile@gmail.com> | 2021-04-14 19:35:07 +0200 |
---|---|---|
committer | Pamphile ROY <roy.pamphile@gmail.com> | 2021-04-14 19:35:07 +0200 |
commit | 04c97a63e1420402b54b89e05082a0ff4cb1c001 (patch) | |
tree | ca9f5f225eb096609d9d592236d6e7d3667242d6 /doc | |
parent | 0c528b933ce1e7d71f310fbb997ed4a0f52b89e2 (diff) | |
download | numpy-04c97a63e1420402b54b89e05082a0ff4cb1c001.tar.gz |
DOC: remove legacy global seed, assert_almost_equal and assert_
Diffstat (limited to 'doc')
-rw-r--r-- | doc/TESTS.rst.txt | 31 | ||||
-rw-r--r-- | doc/source/reference/routines.testing.rst | 11 |
2 files changed, 23 insertions, 19 deletions
diff --git a/doc/TESTS.rst.txt b/doc/TESTS.rst.txt index 21cc08673..c7e89b5b9 100644 --- a/doc/TESTS.rst.txt +++ b/doc/TESTS.rst.txt @@ -59,7 +59,7 @@ that are run; but if it is greater than 1, then the tests will also provide warnings on missing tests. So if you want to run every test and get messages about which modules don't have tests:: - >>> numpy.test(label='full', verbose=2) # or numpy.test('full', 2) + >>> numpy.test(label='full', verbose=2) # or numpy.test('full', 2) Finally, if you are only interested in testing a subset of NumPy, for example, the ``core`` module, use the following:: @@ -101,27 +101,25 @@ module called ``test_yyy.py``. If you only need to test one aspect of ``zzz``, you can simply add a test function:: def test_zzz(): - assert_(zzz() == 'Hello from zzz') + assert zzz() == 'Hello from zzz' More often, we need to group a number of tests together, so we create a test class:: - from numpy.testing import assert_, assert_raises - # import xxx symbols from numpy.xxx.yyy import zzz + import pytest class TestZzz: def test_simple(self): - assert_(zzz() == 'Hello from zzz') + assert zzz() == 'Hello from zzz' def test_invalid_parameter(self): - assert_raises(...) + with pytest.raises(xxxError, match='expected error message'): + ... -Within these test methods, ``assert_()`` and related functions are used to test +Within these test methods, ``assert`` and related functions are used to test whether a certain assumption is valid. If the assertion fails, the test fails. -Note that the Python builtin ``assert`` should not be used, because it is -stripped during compilation with ``-O``. Note that ``test_`` functions or methods should not have a docstring, because that makes it hard to identify the test from the output of running the test @@ -282,12 +280,12 @@ from one in `numpy/linalg/tests/test_linalg.py class LinalgTestCase: def test_single(self): - a = array([[1.,2.], [3.,4.]], dtype=single) + a = array([[1., 2.], [3., 4.]], dtype=single) b = array([2., 1.], dtype=single) self.do(a, b) def test_double(self): - a = array([[1.,2.], [3.,4.]], dtype=double) + a = array([[1., 2.], [3., 4.]], dtype=double) b = array([2., 1.], dtype=double) self.do(a, b) @@ -296,14 +294,14 @@ from one in `numpy/linalg/tests/test_linalg.py class TestSolve(LinalgTestCase): def do(self, a, b): x = linalg.solve(a, b) - assert_almost_equal(b, dot(a, x)) - assert_(imply(isinstance(b, matrix), isinstance(x, matrix))) + assert_allclose(b, dot(a, x)) + assert imply(isinstance(b, matrix), isinstance(x, matrix)) class TestInv(LinalgTestCase): def do(self, a, b): a_inv = linalg.inv(a) - assert_almost_equal(dot(a, a_inv), identity(asarray(a).shape[0])) - assert_(imply(isinstance(a, matrix), isinstance(a_inv, matrix))) + assert_allclose(dot(a, a_inv), identity(asarray(a).shape[0])) + assert imply(isinstance(a, matrix), isinstance(a_inv, matrix)) In this case, we wanted to test solving a linear algebra problem using matrices of several data types, using ``linalg.solve`` and @@ -353,7 +351,8 @@ new bugs or regressions, a test that passes most of the time but fails occasionally with no code changes is not helpful. Make the random data deterministic by setting the random number seed before generating it. Use either Python's ``random.seed(some_number)`` or NumPy's -``numpy.random.seed(some_number)``, depending on the source of random numbers. +``rng = np.random.default_rng(some_number)``, depending on the source of +random numbers. Alternatively, you can use `Hypothesis`_ to generate arbitrary data. Hypothesis manages both Python's and Numpy's random seeds for you, and diff --git a/doc/source/reference/routines.testing.rst b/doc/source/reference/routines.testing.rst index 98ce3f377..7bd499fdf 100644 --- a/doc/source/reference/routines.testing.rst +++ b/doc/source/reference/routines.testing.rst @@ -18,9 +18,6 @@ Asserts .. autosummary:: :toctree: generated/ - assert_almost_equal - assert_approx_equal - assert_array_almost_equal assert_allclose assert_array_almost_equal_nulp assert_array_max_ulp @@ -32,6 +29,14 @@ Asserts assert_warns assert_string_equal +.. autosummary:: + :toctree: generated/ + :hidden: + + assert_almost_equal + assert_approx_equal + assert_array_almost_equal + Decorators ---------- .. autosummary:: |