| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Functionality used for testing. This code itself is not covered in tests.""" |
| 5 |
|
| 6 |
import os |
| 7 |
import sys |
| 8 |
import pytest |
| 9 |
|
| 10 |
|
| 11 |
THIS_DIR = os.path.abspath(os.path.dirname(__file__)) |
| 12 |
ROOT_DIR = THIS_DIR |
| 13 |
for i in range(9): |
| 14 |
ROOT_DIR = os.path.dirname(ROOT_DIR) |
| 15 |
if os.path.isfile(os.path.join(ROOT_DIR, ".gitignore")): |
| 16 |
break |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
def test_unit(cov_report="term"): |
| 23 |
"""Run all unit tests. Returns exit code.""" |
| 24 |
orig_dir = os.getcwd() |
| 25 |
os.chdir(ROOT_DIR) |
| 26 |
try: |
| 27 |
_clear_imageio() |
| 28 |
_enable_faulthandler() |
| 29 |
return pytest.main( |
| 30 |
[ |
| 31 |
"-v", |
| 32 |
"--cov", |
| 33 |
"imageio", |
| 34 |
"--cov-config", |
| 35 |
".coveragerc", |
| 36 |
"--cov-report", |
| 37 |
cov_report, |
| 38 |
"tests", |
| 39 |
] |
| 40 |
) |
| 41 |
finally: |
| 42 |
os.chdir(orig_dir) |
| 43 |
import imageio |
| 44 |
|
| 45 |
print("Tests were performed on", str(imageio)) |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
def _enable_faulthandler(): |
| 52 |
"""Enable faulthandler (if we can), so that we get tracebacks |
| 53 |
on segfaults. |
| 54 |
""" |
| 55 |
try: |
| 56 |
import faulthandler |
| 57 |
|
| 58 |
faulthandler.enable() |
| 59 |
print("Faulthandler enabled") |
| 60 |
except Exception: |
| 61 |
print("Could not enable faulthandler") |
| 62 |
|
| 63 |
|
| 64 |
def _clear_imageio(): |
| 65 |
|
| 66 |
for key in list(sys.modules.keys()): |
| 67 |
if key.startswith("imageio"): |
| 68 |
del sys.modules[key] |