hbutils.testing.requires.expr
Environment and version expressions for conditional testing.
This module provides utilities for expressing runtime requirements in a
comparison-friendly form. The exported objects are intended for use with
unittest.skipUnless or other conditional execution mechanisms. It includes
version expressions for the Python interpreter and pip or third-party packages,
along with boolean indicators for the current operating system and Python
implementation.
The module contains the following main components:
vpython- Version expression bound to the current Python interpretervpip- Version expression bound to pip and package versionsOS- Operating system detection attributesImpl- Python implementation detection attributes
Example:
>>> import unittest
>>> from hbutils.testing import vpython, vpip, OS, Impl
>>>
>>> class TestMyCase(unittest.TestCase):
... @unittest.skipUnless('3.8' <= vpython, 'Python 3.8+ only')
... def test_python_version(self):
... assert True
...
... @unittest.skipUnless(vpip >= '21', 'pip 21+ only')
... def test_pip_version(self):
... assert True
...
... @unittest.skipUnless(OS.linux and Impl.cpython, 'CPython on Linux only')
... def test_runtime(self):
... assert True
Note
All version expressions are dynamic and read the current environment when compared or evaluated.
__all__
- hbutils.testing.requires.expr.__all__ = ['vpython', 'vpip', 'OS', 'Impl']
Built-in mutable sequence.
If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.
vpython
- hbutils.testing.requires.expr.vpython: VersionInfo = <VersionInfo 3.10.17>
Python version expression.
This object represents the current Python interpreter version and supports comparison operations against version strings, tuples, or integers.
Examples:
>>> import platform >>> import unittest >>> from hbutils.testing import vpython >>> >>> class TestMyCase(unittest.TestCase): # on Python 3.6 ... def test_anytime(self): ... assert 2 + 1 == 3 ... ... @unittest.skipUnless('3.6' <= vpython < '3.7', 'py36 only') # will run ... def test_on_python36(self): ... assert platform.python_version_tuple()[:2] == ('3', '6') ... ... @unittest.skipUnless('3.7' <= vpython < '3.8', 'py37 only') # will skip ... def test_on_python37(self): ... assert platform.python_version_tuple()[:2] == ('3', '7') ... >>> unittest.main() ..s ---------------------------------------------------------------------- Ran 3 tests in 0.000s OK (skipped=1)
vpip
- hbutils.testing.requires.expr.vpip: PipVersionInfo = <PipVersionInfo 26.0.1>
Pip and package version expression.
This object represents the current
pipversion and supports comparisons. Calling the object with a package name returns a version expression for that package.Examples:
>>> import unittest >>> from hbutils.testing import vpip >>> >>> class TestMyCase(unittest.TestCase): ... def test_1_anytime(self): ... assert 2 + 1 == 3 ... ... @unittest.skipUnless(vpip >= '21', 'pip21+ only') # pip>=21 ... def test_2_on_pip21plus(self): ... assert True ... ... @unittest.skipUnless(vpip('pip') >= '21', 'pip21+ only') # the same as above ... def test_3_on_pip21plus2(self): ... assert True ... ... @unittest.skipUnless(vpip('setuptools') >= '45' or vpip('build') >= '0.8', '') # setuptools>=45 or build>=0.8 ... def test_4_on_setuptools_or_build(self): ... assert True ... ... @unittest.skipUnless(not vpip and vpip('build') >= '0.8', '') # pip not installed, and build>=0.8 ... def test_5_on_nopip_and_build(self): ... assert True ... >>> unittest.main() ....s ---------------------------------------------------------------------- Ran 5 tests in 0.000s OK (skipped=1)
PipVersionInfo
- class hbutils.testing.requires.expr.PipVersionInfo(v: VersionInfo | Version | Callable[[], VersionInfo | Version | str | Tuple[int, ...] | int | None] | str | Tuple[int, ...] | int | None)[source]
Version expression for pip and installed packages.
This class extends
VersionInfoto provide version expressions for arbitrary installed packages. The base instance is bound to the currentpipversion, while calling the instance returns a version expression for another package name.- __call__(name: str) VersionInfo[source]
Get version information for a specific package.
- Parameters:
name (str) – The name of the package to check.
- Returns:
A
VersionInfoobject for the specified package.- Return type:
Example:
>>> from hbutils.testing import vpip >>> vpip('setuptools') # Get setuptools version info <VersionInfo ...>
OS
- class hbutils.testing.requires.expr.OS[source]
Operating system detection expressions.
The attributes of this class are boolean values evaluated at import time, indicating the current operating system.
- Variables:
windows (bool) – Whether the current OS is Windows.
linux (bool) – Whether the current OS is Linux.
darwin (bool) – Whether the current OS is macOS (Darwin).
macos (bool) – Alias for
darwin.
Examples:
>>> import unittest >>> from hbutils.testing import OS >>> >>> class TestMyCase(unittest.TestCase): # on Linux ... def test_1_anytime(self): ... assert 2 + 1 == 3 ... ... @unittest.skipUnless(OS.linux, 'linux only') # only run on Linux ... def test_2_linux(self): ... assert True ... ... @unittest.skipUnless(OS.windows, 'windows only') # only run on Windows ... def test_2_windows(self): ... assert True ... ... @unittest.skipUnless(OS.macos, 'macos only') # only run on macOS ... def test_4_macos(self): ... assert True ... >>> unittest.main() ..ss ---------------------------------------------------------------------- Ran 4 tests in 0.001s OK (skipped=2)
- darwin: bool = False
Whether the current operating system is Darwin (macOS).
- linux: bool = True
Whether the current operating system is Linux.
- windows: bool = False
Whether the current operating system is Windows.
Impl
- class hbutils.testing.requires.expr.Impl[source]
Python implementation detection expressions.
The attributes of this class are boolean values evaluated at import time, indicating the current Python implementation. See platform.python_implementation() for details.
- Variables:
cpython (bool) – Whether the current implementation is CPython.
iron_python (bool) – Whether the current implementation is IronPython.
jython (bool) – Whether the current implementation is Jython.
pypy (bool) – Whether the current implementation is PyPy.
Examples:
>>> import unittest >>> from hbutils.testing import Impl >>> >>> class TestMyCase(unittest.TestCase): # on CPython ... def test_1_anytime(self): ... assert 2 + 1 == 3 ... ... @unittest.skipUnless(Impl.cpython, 'cpython only') # only run on CPython ... def test_2_cpython(self): ... assert True ... ... @unittest.skipUnless(Impl.pypy, 'pypy only') # only run on PyPy ... def test_3_pypy(self): ... assert True ... ... @unittest.skipUnless(Impl.iron_python, 'ironpython only') # only run on IronPython ... def test_4_ironpython(self): ... assert True ... ... @unittest.skipUnless(Impl.jython, 'jython only') # only run on Jython ... def test_5_jython(self): ... assert True ... >>> unittest.main() ..sss ---------------------------------------------------------------------- Ran 5 tests in 0.000s OK (skipped=3)
- cpython: bool = True
Whether the current Python implementation is CPython.
- iron_python: bool = False
Whether the current Python implementation is IronPython.
- jython: bool = False
Whether the current Python implementation is Jython.
- pypy: bool = False
Whether the current Python implementation is PyPy.