hbutils.testing.requires

pre_condition

hbutils.testing.requires.pre_condition(cond, skip_reason: Optional[str] = None)[source]
Overview:

Set pre-condition for the cases of unittest. Can be used to both functions and classes.

Parameters:
  • cond – Condition expression.

  • skip_reason – Reason when skipping.

Returns:

Skip decorator, based on unittest.skipUnless().

Note

See vpython, vpip, OS and Impl for actual examples.

vpython

hbutils.testing.requires.vpython = <hbutils.testing.requires.expr.VersionCmpExpression object>
Overview:

Python version expression.

Examples::
>>> import platform
>>> import unittest
>>> from hbutils.testing import pre_condition, vpython
>>> 
>>> class TestMyCase(unittest.TestCase):  # on python 3.6
...     def test_anytime(self):
...         assert 2 + 1 == 3
...
...     @pre_condition((vpython >= '3.6') & (vpython < '3.7'))  # will run
...     def test_on_python36(self):
...         assert platform.python_version_tuple()[:2] == ('3', '6')
...
...     @pre_condition((vpython >= '3.7') & (vpython < '3.8'))  # 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.vpip = <hbutils.testing.requires.expr.PipVersionCmpExpression object>
Overview:

Pip version expression

Examples::
>>> import unittest
>>> from hbutils.testing import pre_condition, vpip
>>> 
>>> class TestMyCase(unittest.TestCase):
...     def test_1_anytime(self):
...         assert 2 + 1 == 3
...
...     @pre_condition(vpip >= '21')  # pip>=21
...     def test_2_on_pip21plus(self):
...         assert True
...
...     @pre_condition(vpip('pip') >= '21')  # the same as above
...     def test_3_on_pip21plus2(self):
...         assert True
...
...     @pre_condition((vpip('setuptools') >= '45') | (vpip('build') >= '0.8'))  # setuptools>=45 or build>=0.8
...     def test_4_on_setuptools_or_build(self):
...         assert True
...
...     @pre_condition(~vpip & (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)

OS

class hbutils.testing.requires.OS[source]
Overview:

Expressions for operating system.

Examples::
>>> import unittest
>>> from hbutils.testing import pre_condition, OS
>>>
>>> class TestMyCase(unittest.TestCase):  # on Linux
...     def test_1_anytime(self):
...         assert 2 + 1 == 3
...
...     @pre_condition(OS.linux)  # only run on Linux
...     def test_2_linux(self):
...         assert True
...
...     @pre_condition(OS.windows)  # only run on Windows
...     def test_2_windows(self):
...         assert True
...
...     @pre_condition(OS.macos)  # only run on macOS
...     def test_4_macos(self):
...         assert True
...
>>> unittest.main()
..ss
----------------------------------------------------------------------
Ran 4 tests in 0.001s
OK (skipped=2)
darwin = <hbutils.testing.requires.expr.OSExpression object>

Expression for darwin system (also named OS.macos).

linux = <hbutils.testing.requires.expr.OSExpression object>

Expression for linux system.

macos = <hbutils.testing.requires.expr.OSExpression object>

Alias for OS.darwin.

windows = <hbutils.testing.requires.expr.OSExpression object>

Expression for windows system.

Impl

class hbutils.testing.requires.Impl[source]
Overview:

Expression for python implementation. See platform.python_implementation() .

Examples::
>>> import unittest
>>> from hbutils.testing import pre_condition, Impl
>>>
>>> class TestMyCase(unittest.TestCase):  # on CPython
...     def test_1_anytime(self):
...         assert 2 + 1 == 3
...
...     @pre_condition(Impl.cpython)  # only run on CPython
...     def test_2_cpython(self):
...         assert True
...
...     @pre_condition(Impl.pypy)  # only run on PyPy
...     def test_3_pypy(self):
...         assert True
...
...     @pre_condition(Impl.iron_python)  # only run on IronPython
...     def test_4_ironpython(self):
...         assert True
...
...     @pre_condition(Impl.jython)  # only run on Jython
...     def test_5_jython(self):
...         assert True
...
>>> unittest.main()
..sss
----------------------------------------------------------------------
Ran 5 tests in 0.000s
OK (skipped=3)
cpython = <hbutils.testing.requires.expr.PythonImplementExpression object>

Expression for CPython (most-frequently-used python).

iron_python = <hbutils.testing.requires.expr.PythonImplementExpression object>

Expression for IronPython.

jython = <hbutils.testing.requires.expr.PythonImplementExpression object>

Expression for Jython (java-based python).

pypy = <hbutils.testing.requires.expr.PythonImplementExpression object>

Expression for PyPy.