Skip to content

Commit ed2fd1b

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 plugins which are introduced in later versions) more easily than parsing the version themselves. This feature was added originally in pypa/setuptools-scm#475. Followup to #7605.
1 parent 6447ca5 commit ed2fd1b

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

doc/en/reference/reference.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,34 @@ 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+
pytest.version_tuple
239+
~~~~~~~~~~~~~~~~~~~~
240+
241+
.. versionadded:: 7.0
242+
243+
The current pytest version, as a tuple::
244+
245+
>>> import pytest
246+
>>> pytest.version_tuple
247+
(7, 0, 0)
248+
249+
For pre-releases, the last component will be a string with the prerelease version::
250+
251+
>>> import pytest
252+
>>> pytest.version_tuple
253+
(7, 0, '0rc1')
254+
255+
256+
229257
Custom marks
230258
~~~~~~~~~~~~
231259

src/_pytest/__init__.py

Lines changed: 3 additions & 2 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__
4+
from ._version import version as __version__, version_tuple
55
except ImportError:
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)