Skip to content

Commit c7282cf

Browse files
authored
Merge pull request numpy#18787 from larsoner/doc
DOC: Document newer pytest conventions
2 parents 284923e + 065de62 commit c7282cf

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

doc/TESTS.rst.txt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,32 @@ module called ``test_yyy.py``. If you only need to test one aspect of
106106
More often, we need to group a number of tests together, so we create
107107
a test class::
108108

109-
from numpy.testing import assert_, assert_raises
109+
import pytest
110110

111111
# import xxx symbols
112112
from numpy.xxx.yyy import zzz
113113

114114
class TestZzz:
115115
def test_simple(self):
116-
assert_(zzz() == 'Hello from zzz')
116+
assert zzz() == 'Hello from zzz'
117117

118118
def test_invalid_parameter(self):
119-
assert_raises(...)
119+
with pytest.raises(ValueError, match='.*some matching regex.*'):
120+
...
120121

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
122123
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.
125135

126136
Note that ``test_`` functions or methods should not have a docstring, because
127137
that makes it hard to identify the test from the output of running the test

doc/source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ def setup(app):
292292
'skimage': ('https://scikit-image.org/docs/stable', None),
293293
'pandas': ('https://pandas.pydata.org/pandas-docs/stable', None),
294294
'scipy-lecture-notes': ('https://scipy-lectures.org', None),
295+
'pytest': ('https://docs.pytest.org/en/stable', None),
295296
}
296297

297298

0 commit comments

Comments
 (0)