Skip to content

Commit 8ffa3aa

Browse files
authored
Improve docs of pytest.importorskip (#5718)
Improve docs of pytest.importorskip
2 parents 5ecac3c + b095e0d commit 8ffa3aa

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

doc/en/reference.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pytest.skip
2727

2828
.. autofunction:: _pytest.outcomes.skip(msg, [allow_module_level=False])
2929

30+
.. _`pytest.importorskip ref`:
31+
3032
pytest.importorskip
3133
~~~~~~~~~~~~~~~~~~~
3234

doc/en/skipping.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,15 @@ information.
179179
Skipping on a missing import dependency
180180
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
181181

182-
You can use the following helper at module level
183-
or within a test or test setup function:
182+
You can skip tests on a missing import by using :ref:`pytest.importorskip ref`
183+
at module level or within a test or test setup function.
184184

185185
.. code-block:: python
186186
187187
docutils = pytest.importorskip("docutils")
188188
189-
If ``docutils`` cannot be imported here, this will lead to a
190-
skip outcome of the test. You can also skip based on the
191-
version number of a library:
189+
If ``docutils`` cannot be imported here, this will lead to a skip outcome of
190+
the test. You can also skip based on the version number of a library:
192191

193192
.. code-block:: python
194193

src/_pytest/outcomes.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,21 @@ def xfail(reason=""):
143143

144144

145145
def importorskip(modname, minversion=None, reason=None):
146-
"""Imports and returns the requested module ``modname``, or skip the current test
147-
if the module cannot be imported.
146+
"""Imports and returns the requested module ``modname``, or skip the
147+
current test if the module cannot be imported.
148148
149149
:param str modname: the name of the module to import
150-
:param str minversion: if given, the imported module ``__version__`` attribute must be
151-
at least this minimal version, otherwise the test is still skipped.
152-
:param str reason: if given, this reason is shown as the message when the module
153-
cannot be imported.
150+
:param str minversion: if given, the imported module ``__version__``
151+
attribute must be at least this minimal version, otherwise the test is
152+
still skipped.
153+
:param str reason: if given, this reason is shown as the message when the
154+
module cannot be imported.
155+
:returns: The imported module. This should be assigned to its canonical
156+
name.
157+
158+
Example::
159+
160+
docutils = pytest.importorskip("docutils")
154161
"""
155162
import warnings
156163

0 commit comments

Comments
 (0)