Skip to content

Commit bb17bea

Browse files
committed
Add "only_version" decorator to testing.
1 parent 1f1e733 commit bb17bea

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

domdf_python_tools/testing.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"count",
8383
"min_version",
8484
"max_version",
85+
"only_version",
8586
"not_windows",
8687
"only_windows",
8788
"not_pypy",
@@ -318,6 +319,31 @@ def max_version(
318319
return pytest.mark.skipif(condition=sys.version_info[:3] > version_, reason=reason)
319320

320321

322+
def only_version(
323+
version: Union[str, float, Tuple[int]],
324+
reason: Optional[str] = None,
325+
) -> MarkDecorator:
326+
"""
327+
Factory function to return a ``@pytest.mark.skipif`` decorator that will
328+
skip a test if the current Python version not the required one.
329+
330+
:param version: The version number to compare to :py:data:`sys.version_info`.
331+
:param reason: The reason to display when skipping.
332+
:default reason: :file:`'Not needed on Python {<version>}.'`
333+
334+
:rtype:
335+
336+
.. versionadded:: 2.0.0
337+
""" # noqa D400
338+
339+
version_ = _make_version(version)
340+
341+
if reason is None:
342+
reason = f"Not needed on Python {version_}."
343+
344+
return pytest.mark.skipif(condition=sys.version_info[:2] != version_[:2], reason=reason)
345+
346+
321347
def platform_boolean_factory(
322348
condition: bool,
323349
platform: str,

tests/test_testing.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
not_windows,
2020
only_macos,
2121
only_pypy,
22+
only_version,
2223
only_windows
2324
)
2425
from domdf_python_tools.utils import strtobool
@@ -154,6 +155,22 @@ def test_generate_falsy():
154155
assert list(testing.generate_falsy_values(ratio=0.3)) == ['0', "no", "False", False]
155156

156157

158+
@pytest.mark.parametrize(
159+
"py_version",
160+
[
161+
pytest.param((3, 4), marks=only_version(3.4, "Success")),
162+
pytest.param((3, 5), marks=only_version(3.5, "Success")),
163+
pytest.param((3, 6), marks=only_version(3.6, "Success")),
164+
pytest.param((3, 8), marks=only_version(3.8, "Success")),
165+
pytest.param((3, 9), marks=only_version(3.9, "Success")),
166+
pytest.param((3, 10), marks=only_version(3.10, "Success")),
167+
]
168+
)
169+
def test_only_version(py_version):
170+
if sys.version_info[:2] != py_version:
171+
assert False # noqa: PT015
172+
173+
157174
@not_pypy("Success")
158175
def test_not_pypy():
159176
if platform.python_implementation() == "PyPy":

0 commit comments

Comments
 (0)