Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pytest.version_tuple #8761

Merged
merged 1 commit into from Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/8761.feature.rst
@@ -0,0 +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).
31 changes: 31 additions & 0 deletions doc/en/reference/reference.rst
Expand Up @@ -226,6 +226,37 @@ 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'


.. _`version-tuple`:

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
~~~~~~~~~~~~

Expand Down
7 changes: 4 additions & 3 deletions src/_pytest/__init__.py
@@ -1,8 +1,9 @@
__all__ = ["__version__"]
__all__ = ["__version__", "version_tuple"]

try:
from ._version import version as __version__
except ImportError:
from ._version import version as __version__, version_tuple
except ImportError: # pragma: no cover
# 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]
2 changes: 2 additions & 0 deletions src/pytest/__init__.py
Expand Up @@ -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
Expand Down Expand Up @@ -130,6 +131,7 @@
"Session",
"set_trace",
"skip",
"version_tuple",
"TempPathFactory",
"Testdir",
"TempdirFactory",
Expand Down
6 changes: 6 additions & 0 deletions testing/test_helpconfig.py
Expand Up @@ -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
Expand Down