Skip to content

Commit 7eb0792

Browse files
committed
Add pytest.version_tuple
This adds `pytest.version_tuple`, which makes it simpler for users to do something depending on the pytest version, such as declaring hooks which are introduced in later versions. This feature was added originally in pypa/setuptools-scm#475. Followup to #7605.
1 parent 6447ca5 commit 7eb0792

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed

changelog/8761.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
New :ref:`version-tuple` attribute, which makes it simpler for users to do something depending on the pytest version (such as declaring hooks which are introduced in later versions).

doc/en/reference/reference.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,37 @@ Marks a test function as *expected to fail*.
226226
a new release of a library fixes a known bug).
227227

228228

229+
pytest.__version__
230+
~~~~~~~~~~~~~~~~~~
231+
232+
The current pytest version, as a string::
233+
234+
>>> import pytest
235+
>>> pytest.__version__
236+
'7.0.0'
237+
238+
239+
.. _`version-tuple`:
240+
241+
pytest.version_tuple
242+
~~~~~~~~~~~~~~~~~~~~
243+
244+
.. versionadded:: 7.0
245+
246+
The current pytest version, as a tuple::
247+
248+
>>> import pytest
249+
>>> pytest.version_tuple
250+
(7, 0, 0)
251+
252+
For pre-releases, the last component will be a string with the prerelease version::
253+
254+
>>> import pytest
255+
>>> pytest.version_tuple
256+
(7, 0, '0rc1')
257+
258+
259+
229260
Custom marks
230261
~~~~~~~~~~~~
231262

src/_pytest/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
__all__ = ["__version__"]
1+
__all__ = ["__version__", "version_tuple"]
22

33
try:
4-
from ._version import version as __version__
5-
except ImportError:
4+
from ._version import version as __version__, version_tuple
5+
except ImportError: # pragma: no cover
66
# broken installation, we don't even try
77
# unknown only works because we do poor mans version compare
88
__version__ = "unknown"
9+
version_tuple = (0, 0, "unknown") # type:ignore[assignment]

src/pytest/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""pytest: unit and functional testing with Python."""
33
from . import collect
44
from _pytest import __version__
5+
from _pytest import version_tuple
56
from _pytest._code import ExceptionInfo
67
from _pytest.assertion import register_assert_rewrite
78
from _pytest.cacheprovider import Cache
@@ -130,6 +131,7 @@
130131
"Session",
131132
"set_trace",
132133
"skip",
134+
"version_tuple",
133135
"TempPathFactory",
134136
"Testdir",
135137
"TempdirFactory",

testing/test_helpconfig.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ def test_version_less_verbose(pytester: Pytester, pytestconfig, monkeypatch) ->
1919
result.stderr.fnmatch_lines([f"pytest {pytest.__version__}"])
2020

2121

22+
def test_versions():
23+
"""Regression check for the public version attributes in pytest."""
24+
assert isinstance(pytest.__version__, str)
25+
assert isinstance(pytest.version_tuple, tuple)
26+
27+
2228
def test_help(pytester: Pytester) -> None:
2329
result = pytester.runpytest("--help")
2430
assert result.ret == 0

0 commit comments

Comments
 (0)