@@ -106,22 +106,32 @@ module called ``test_yyy.py``. If you only need to test one aspect of
106
106
More often, we need to group a number of tests together, so we create
107
107
a test class::
108
108
109
- from numpy.testing import assert_, assert_raises
109
+ import pytest
110
110
111
111
# import xxx symbols
112
112
from numpy.xxx.yyy import zzz
113
113
114
114
class TestZzz:
115
115
def test_simple(self):
116
- assert_( zzz() == 'Hello from zzz')
116
+ assert zzz() == 'Hello from zzz'
117
117
118
118
def test_invalid_parameter(self):
119
- assert_raises(...)
119
+ with pytest.raises(ValueError, match='.*some matching regex.*'):
120
+ ...
120
121
121
- Within these test methods, ``assert_() `` and related functions are used to test
122
+ Within these test methods, ``assert `` and related functions are used to test
122
123
whether a certain assumption is valid. If the assertion fails, the test fails.
123
- Note that the Python builtin ``assert `` should not be used, because it is
124
- stripped during compilation with ``-O ``.
124
+ ``pytest `` internally rewrites the ``assert `` statement to give informative
125
+ output when it fails, so should be preferred over the legacy variant
126
+ ``numpy.testing.assert_ ``. Whereas plain ``assert `` statements are ignored
127
+ when running Python in optimized mode with ``-O ``, this is not an issue when
128
+ running tests with pytest.
129
+
130
+ Similarly, the pytest functions :func: `pytest.raises ` and :func: `pytest.warns `
131
+ should be preferred over their legacy counterparts
132
+ :func: `numpy.testing.assert_raises ` and :func: `numpy.testing.assert_warns `,
133
+ since the pytest variants are more broadly used and allow more explicit
134
+ targeting of warnings and errors when used with the ``match `` regex.
125
135
126
136
Note that ``test_ `` functions or methods should not have a docstring, because
127
137
that makes it hard to identify the test from the output of running the test
0 commit comments