Skip to content

Commit 5dcbc64

Browse files
simonjayhawkinsjreback
authored andcommitted
TST: return pytest MarkDecorator from td.skip_if_no (#26735)
1 parent 13ee0e8 commit 5dcbc64

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

pandas/tests/io/test_html.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,8 @@ def test_same_ordering(datapath):
7777

7878

7979
@pytest.mark.parametrize("flavor", [
80-
pytest.param('bs4', marks=pytest.mark.skipif(
81-
not td.safe_import('lxml'), reason='No bs4')),
82-
pytest.param('lxml', marks=pytest.mark.skipif(
83-
not td.safe_import('lxml'), reason='No lxml'))], scope="class")
80+
pytest.param('bs4', marks=td.skip_if_no('lxml')),
81+
pytest.param('lxml', marks=td.skip_if_no('lxml'))], scope="class")
8482
class TestReadHtml:
8583

8684
@pytest.fixture(autouse=True)

pandas/util/_test_decorators.py

+29-20
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ def test_foo():
2424
For more information, refer to the ``pytest`` documentation on ``skipif``.
2525
"""
2626
import locale
27+
from typing import Optional
2728

29+
from _pytest.mark.structures import MarkDecorator
2830
import pytest
2931

3032
from pandas.compat import is_platform_32bit, is_platform_windows
@@ -97,38 +99,45 @@ def _skip_if_no_scipy():
9799
safe_import('scipy.signal'))
98100

99101

100-
def skip_if_no(package, min_version=None):
102+
def skip_if_no(
103+
package: str,
104+
min_version: Optional[str] = None
105+
) -> MarkDecorator:
101106
"""
102-
Generic function to help skip test functions when required packages are not
107+
Generic function to help skip tests when required packages are not
103108
present on the testing system.
104109
105-
Intended for use as a decorator, this function will wrap the decorated
106-
function with a pytest ``skip_if`` mark. During a pytest test suite
107-
execution, that mark will attempt to import the specified ``package`` and
108-
optionally ensure it meets the ``min_version``. If the import and version
109-
check are unsuccessful, then the decorated function will be skipped.
110+
This function returns a pytest mark with a skip condition that will be
111+
evaluated during test collection. An attempt will be made to import the
112+
specified ``package`` and optionally ensure it meets the ``min_version``
113+
114+
The mark can be used as either a decorator for a test function or to be
115+
applied to parameters in pytest.mark.parametrize calls or parametrized
116+
fixtures.
117+
118+
If the import and version check are unsuccessful, then the test function
119+
(or test case when used in conjunction with parametrization) will be
120+
skipped.
110121
111122
Parameters
112123
----------
113124
package: str
114-
The name of the package required by the decorated function
125+
The name of the required package.
115126
min_version: str or None, default None
116-
Optional minimum version of the package required by the decorated
117-
function
127+
Optional minimum version of the package.
118128
119129
Returns
120130
-------
121-
decorated_func: function
122-
The decorated function wrapped within a pytest ``skip_if`` mark
131+
_pytest.mark.structures.MarkDecorator
132+
a pytest.mark.skipif to use as either a test decorator or a
133+
parametrization mark.
123134
"""
124-
def decorated_func(func):
125-
msg = "Could not import '{}'".format(package)
126-
if min_version:
127-
msg += " satisfying a min_version of {}".format(min_version)
128-
return pytest.mark.skipif(
129-
not safe_import(package, min_version=min_version), reason=msg
130-
)(func)
131-
return decorated_func
135+
msg = "Could not import '{}'".format(package)
136+
if min_version:
137+
msg += " satisfying a min_version of {}".format(min_version)
138+
return pytest.mark.skipif(
139+
not safe_import(package, min_version=min_version), reason=msg
140+
)
132141

133142

134143
skip_if_no_mpl = pytest.mark.skipif(_skip_if_no_mpl(),

0 commit comments

Comments
 (0)