Improve docs of pytest.importorskip (#5718)
Improve docs of pytest.importorskip
This commit is contained in:
commit
8ffa3aa65d
|
@ -27,6 +27,8 @@ pytest.skip
|
||||||
|
|
||||||
.. autofunction:: _pytest.outcomes.skip(msg, [allow_module_level=False])
|
.. autofunction:: _pytest.outcomes.skip(msg, [allow_module_level=False])
|
||||||
|
|
||||||
|
.. _`pytest.importorskip ref`:
|
||||||
|
|
||||||
pytest.importorskip
|
pytest.importorskip
|
||||||
~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
|
|
@ -179,16 +179,15 @@ information.
|
||||||
Skipping on a missing import dependency
|
Skipping on a missing import dependency
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
You can use the following helper at module level
|
You can skip tests on a missing import by using :ref:`pytest.importorskip ref`
|
||||||
or within a test or test setup function:
|
at module level or within a test or test setup function.
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
docutils = pytest.importorskip("docutils")
|
docutils = pytest.importorskip("docutils")
|
||||||
|
|
||||||
If ``docutils`` cannot be imported here, this will lead to a
|
If ``docutils`` cannot be imported here, this will lead to a skip outcome of
|
||||||
skip outcome of the test. You can also skip based on the
|
the test. You can also skip based on the version number of a library:
|
||||||
version number of a library:
|
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
|
|
|
@ -143,14 +143,21 @@ xfail.Exception = XFailed # type: ignore
|
||||||
|
|
||||||
|
|
||||||
def importorskip(modname, minversion=None, reason=None):
|
def importorskip(modname, minversion=None, reason=None):
|
||||||
"""Imports and returns the requested module ``modname``, or skip the current test
|
"""Imports and returns the requested module ``modname``, or skip the
|
||||||
if the module cannot be imported.
|
current test if the module cannot be imported.
|
||||||
|
|
||||||
:param str modname: the name of the module to import
|
:param str modname: the name of the module to import
|
||||||
:param str minversion: if given, the imported module ``__version__`` attribute must be
|
:param str minversion: if given, the imported module ``__version__``
|
||||||
at least this minimal version, otherwise the test is still skipped.
|
attribute must be at least this minimal version, otherwise the test is
|
||||||
:param str reason: if given, this reason is shown as the message when the module
|
still skipped.
|
||||||
cannot be imported.
|
:param str reason: if given, this reason is shown as the message when the
|
||||||
|
module cannot be imported.
|
||||||
|
:returns: The imported module. This should be assigned to its canonical
|
||||||
|
name.
|
||||||
|
|
||||||
|
Example::
|
||||||
|
|
||||||
|
docutils = pytest.importorskip("docutils")
|
||||||
"""
|
"""
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue