|
| 1 | +from array_api_tests.test_operators_and_elementwise_functions import (UnaryParamContext, BinaryParamContext) |
| 2 | +from array_api_tests.dtype_helpers import dtype_to_name |
| 3 | +from array_api_tests import _array_module as xp |
| 4 | + |
| 5 | +from pytest import mark, fixture |
| 6 | + |
| 7 | +def to_json_serializable(o): |
| 8 | + if o in dtype_to_name: |
| 9 | + return dtype_to_name[o] |
| 10 | + if isinstance(o, UnaryParamContext): |
| 11 | + return {'func_name': o.func_name} |
| 12 | + if isinstance(o, BinaryParamContext): |
| 13 | + return { |
| 14 | + 'func_name': o.func_name, |
| 15 | + 'left_sym': o.left_sym, |
| 16 | + 'right_sym': o.right_sym, |
| 17 | + 'right_is_scalar': o.right_is_scalar, |
| 18 | + 'res_name': o.res_name, |
| 19 | + } |
| 20 | + if isinstance(o, dict): |
| 21 | + return {to_json_serializable(k): to_json_serializable(v) for k, v in o.items()} |
| 22 | + if isinstance(o, tuple): |
| 23 | + return tuple(to_json_serializable(i) for i in o) |
| 24 | + if isinstance(o, list): |
| 25 | + return [to_json_serializable(i) for i in o] |
| 26 | + |
| 27 | + return o |
| 28 | + |
| 29 | +@mark.optionalhook |
| 30 | +def pytest_metadata(metadata): |
| 31 | + """ |
| 32 | + Additional global metadata for --json-report. |
| 33 | + """ |
| 34 | + metadata['array_api_tests_module'] = xp.mod_name |
| 35 | + |
| 36 | +@fixture(autouse=True) |
| 37 | +def add_api_name_to_metadata(request, json_metadata): |
| 38 | + """ |
| 39 | + Additional per-test metadata for --json-report |
| 40 | + """ |
| 41 | + test_module = request.module.__name__ |
| 42 | + if test_module.startswith('array_api_tests.meta'): |
| 43 | + return |
| 44 | + |
| 45 | + test_function = request.function.__name__ |
| 46 | + assert test_function.startswith('test_'), 'unexpected test function name' |
| 47 | + |
| 48 | + if test_module == 'array_api_tests.test_has_names': |
| 49 | + array_api_function_name = None |
| 50 | + else: |
| 51 | + array_api_function_name = test_function[len('test_'):] |
| 52 | + |
| 53 | + json_metadata['test_module'] = test_module |
| 54 | + json_metadata['test_function'] = test_function |
| 55 | + json_metadata['array_api_function_name'] = array_api_function_name |
| 56 | + |
| 57 | + if hasattr(request.node, 'callspec'): |
| 58 | + json_metadata['params'] = to_json_serializable(request.node.callspec.params) |
0 commit comments