| 1 |
import os |
| 2 |
import shutil |
| 3 |
import sys |
| 4 |
import tempfile |
| 5 |
from importlib import import_module |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
# Expose the unittest-driven decorators |
| 10 |
from .ipunittest import ipdoctest, ipdocstring |
| 11 |
|
| 12 |
def skipif(skip_condition, msg=None): |
| 13 |
"""Make function raise SkipTest exception if skip_condition is true |
| 14 |
|
| 15 |
Parameters |
| 16 |
---------- |
| 17 |
|
| 18 |
skip_condition : bool or callable |
| 19 |
Flag to determine whether to skip test. If the condition is a |
| 20 |
callable, it is used at runtime to dynamically make the decision. This |
| 21 |
is useful for tests that may require costly imports, to delay the cost |
| 22 |
until the test suite is actually executed. |
| 23 |
msg : string |
| 24 |
Message to give on raising a SkipTest exception. |
| 25 |
|
| 26 |
Returns |
| 27 |
------- |
| 28 |
decorator : function |
| 29 |
Decorator, which, when applied to a function, causes SkipTest |
| 30 |
to be raised when the skip_condition was True, and the function |
| 31 |
to be called normally otherwise. |
| 32 |
""" |
| 33 |
if msg is None: |
| 34 |
msg = "Test skipped due to test condition." |
| 35 |
|
| 36 |
assert isinstance(skip_condition, bool) |
| 37 |
return pytest.mark.skipif(skip_condition, reason=msg) |
| 38 |
|
| 39 |
|
| 40 |
# A version with the condition set to true, common case just to attach a message |
| 41 |
# to a skip decorator |
| 42 |
def skip(msg=None): |
| 43 |
"""Decorator factory - mark a test function for skipping from test suite. |
| 44 |
|
| 45 |
Parameters |
| 46 |
---------- |
| 47 |
msg : string |
| 48 |
Optional message to be added. |
| 49 |
|
| 50 |
Returns |
| 51 |
------- |
| 52 |
decorator : function |
| 53 |
Decorator, which, when applied to a function, causes SkipTest |
| 54 |
to be raised, with the optional message added. |
| 55 |
""" |
| 56 |
if msg and not isinstance(msg, str): |
| 57 |
raise ValueError( |
| 58 |
"invalid object passed to `@skip` decorator, did you " |
| 59 |
"meant `@skip()` with brackets ?" |
| 60 |
) |
| 61 |
return skipif(True, msg) |
| 62 |
|
| 63 |
|
| 64 |
def onlyif(condition, msg): |
| 65 |
"""The reverse from skipif, see skipif for details.""" |
| 66 |
|
| 67 |
return skipif(not condition, msg) |
| 68 |
|
| 69 |
|
| 70 |
# ----------------------------------------------------------------------------- |
| 71 |
# Utility functions for decorators |
| 72 |
def module_not_available(module): |
| 73 |
"""Can module be imported? Returns true if module does NOT import. |
| 74 |
|
| 75 |
This is used to make a decorator to skip tests that require module to be |
| 76 |
available, but delay the 'import numpy' to test execution time. |
| 77 |
""" |
| 78 |
try: |
| 79 |
mod = import_module(module) |
| 80 |
mod_not_avail = False |
| 81 |
except Exception: |
| 82 |
# Catch all exceptions, not just ImportError, since modules can fail |
| 83 |
# to import for various reasons (e.g., compatibility issues with newer |
| 84 |
# Python versions causing TypeError, AttributeError, etc.) |
| 85 |
mod_not_avail = True |
| 86 |
|
| 87 |
return mod_not_avail |
| 88 |
|
| 89 |
|
| 90 |
# ----------------------------------------------------------------------------- |
| 91 |
# Decorators for public use |
| 92 |
|
| 93 |
# Decorators to skip certain tests on specific platforms. |
| 94 |
skip_win32 = skipif(sys.platform == "win32", "This test does not run under Windows") |
| 95 |
|
| 96 |
|
| 97 |
# Decorators to skip tests if not on specific platforms. |
| 98 |
skip_if_not_win32 = skipif(sys.platform != "win32", "This test only runs under Windows") |
| 99 |
skip_if_not_osx = skipif( |
| 100 |
not sys.platform.startswith("darwin"), "This test only runs under macOS" |
| 101 |
) |
| 102 |
|
| 103 |
_x11_skip_cond = ( |
| 104 |
sys.platform not in ("darwin", "win32") and os.environ.get("DISPLAY", "") == "" |
| 105 |
) |
| 106 |
_x11_skip_msg = "Skipped under *nix when X11/XOrg not available" |
| 107 |
|
| 108 |
skip_if_no_x11 = skipif(_x11_skip_cond, _x11_skip_msg) |
| 109 |
|
| 110 |
# Other skip decorators |
| 111 |
|
| 112 |
# generic skip without module |
| 113 |
skip_without = lambda mod: skipif( |
| 114 |
module_not_available(mod), "This test requires %s" % mod |
| 115 |
) |
| 116 |
|
| 117 |
skipif_not_numpy = skip_without("numpy") |
| 118 |
|
| 119 |
skipif_not_matplotlib = skip_without("matplotlib") |
| 120 |
|
| 121 |
# A null 'decorator', useful to make more readable code that needs to pick |
| 122 |
# between different decorators based on OS or other conditions |
| 123 |
null_deco = lambda f: f |
| 124 |
|
| 125 |
# Some tests only run where we can use unicode paths. Note that we can't just |
| 126 |
# check os.path.supports_unicode_filenames, which is always False on Linux. |
| 127 |
try: |
| 128 |
f = tempfile.NamedTemporaryFile(prefix="tmp€") |
| 129 |
except UnicodeEncodeError: |
| 130 |
unicode_paths = False |
| 131 |
# TODO: should this be finnally ? |
| 132 |
else: |
| 133 |
unicode_paths = True |
| 134 |
f.close() |
| 135 |
|
| 136 |
onlyif_unicode_paths = onlyif( |
| 137 |
unicode_paths, |
| 138 |
("This test is only applicable where we can use unicode in filenames."), |
| 139 |
) |
| 140 |
|
| 141 |
|
| 142 |
def onlyif_cmds_exist(*commands): |
| 143 |
""" |
| 144 |
Decorator to skip test when at least one of `commands` is not found. |
| 145 |
""" |
| 146 |
for cmd in commands: |
| 147 |
reason = f"This test runs only if command '{cmd}' is installed" |
| 148 |
if not shutil.which(cmd): |
| 149 |
return pytest.mark.skip(reason=reason) |
| 150 |
return null_deco |