From ed2fd1b1510c67e5097bc9cbf3a2823dd0f927bf Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 14 Jun 2021 08:28:45 -0300 Subject: [PATCH] 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 https://github.com/pypa/setuptools_scm/pull/475. Followup to https://github.com/pytest-dev/pytest/pull/7605. --- doc/en/reference/reference.rst | 28 ++++++++++++++++++++++++++++ src/_pytest/__init__.py | 5 +++-- src/pytest/__init__.py | 2 ++ testing/test_helpconfig.py | 6 ++++++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/doc/en/reference/reference.rst b/doc/en/reference/reference.rst index 153dad903b7..1a6992e18dd 100644 --- a/doc/en/reference/reference.rst +++ b/doc/en/reference/reference.rst @@ -226,6 +226,34 @@ Marks a test function as *expected to fail*. a new release of a library fixes a known bug). +pytest.__version__ +~~~~~~~~~~~~~~~~~~ + +The current pytest version, as a string:: + + >>> import pytest + >>> pytest.__version__ + '7.0.0' + +pytest.version_tuple +~~~~~~~~~~~~~~~~~~~~ + +.. versionadded:: 7.0 + +The current pytest version, as a tuple:: + + >>> import pytest + >>> pytest.version_tuple + (7, 0, 0) + +For pre-releases, the last component will be a string with the prerelease version:: + + >>> import pytest + >>> pytest.version_tuple + (7, 0, '0rc1') + + + Custom marks ~~~~~~~~~~~~ diff --git a/src/_pytest/__init__.py b/src/_pytest/__init__.py index 46c7827ed5e..bdb83d6ffde 100644 --- a/src/_pytest/__init__.py +++ b/src/_pytest/__init__.py @@ -1,8 +1,9 @@ -__all__ = ["__version__"] +__all__ = ["__version__", "version_tuple"] try: - from ._version import version as __version__ + from ._version import version as __version__, version_tuple except ImportError: # broken installation, we don't even try # unknown only works because we do poor mans version compare __version__ = "unknown" + version_tuple = (0, 0, "unknown") # type:ignore[assignment] diff --git a/src/pytest/__init__.py b/src/pytest/__init__.py index d8169b8080f..3694f0fc471 100644 --- a/src/pytest/__init__.py +++ b/src/pytest/__init__.py @@ -2,6 +2,7 @@ """pytest: unit and functional testing with Python.""" from . import collect from _pytest import __version__ +from _pytest import version_tuple from _pytest._code import ExceptionInfo from _pytest.assertion import register_assert_rewrite from _pytest.cacheprovider import Cache @@ -130,6 +131,7 @@ "Session", "set_trace", "skip", + "version_tuple", "TempPathFactory", "Testdir", "TempdirFactory", diff --git a/testing/test_helpconfig.py b/testing/test_helpconfig.py index 571a4783e67..e2a27b4e706 100644 --- a/testing/test_helpconfig.py +++ b/testing/test_helpconfig.py @@ -19,6 +19,12 @@ def test_version_less_verbose(pytester: Pytester, pytestconfig, monkeypatch) -> result.stderr.fnmatch_lines([f"pytest {pytest.__version__}"]) +def test_versions(): + """Regression check for the public version attributes in pytest.""" + assert isinstance(pytest.__version__, str) + assert isinstance(pytest.version_tuple, tuple) + + def test_help(pytester: Pytester) -> None: result = pytester.runpytest("--help") assert result.ret == 0