4
4
5
5
from types import BuiltinFunctionType , FunctionType
6
6
import dataclasses
7
+ import json
7
8
8
9
from hypothesis .strategies import SearchStrategy
9
10
@@ -42,6 +43,13 @@ def add_api_name_to_metadata(request, json_metadata):
42
43
"""
43
44
Additional per-test metadata for --json-report
44
45
"""
46
+ def add_metadata (name , obj ):
47
+ obj = to_json_serializable (obj )
48
+ # Ensure everything is JSON serializable. If this errors, it means the
49
+ # given type needs to be added to to_json_serializable above.
50
+ json .dumps (obj )
51
+ json_metadata [name ] = obj
52
+
45
53
test_module = request .module .__name__
46
54
if test_module .startswith ('array_api_tests.meta' ):
47
55
return
@@ -54,20 +62,20 @@ def add_api_name_to_metadata(request, json_metadata):
54
62
else :
55
63
array_api_function_name = test_function [len ('test_' ):]
56
64
57
- json_metadata [ 'test_module' ] = test_module
58
- json_metadata [ 'test_function' ] = test_function
59
- json_metadata [ 'array_api_function_name' ] = array_api_function_name
65
+ add_metadata ( 'test_module' , test_module )
66
+ add_metadata ( 'test_function' , test_function )
67
+ add_metadata ( 'array_api_function_name' , array_api_function_name )
60
68
61
69
if hasattr (request .node , 'callspec' ):
62
70
params = request .node .callspec .params
63
- json_metadata [ 'params' ] = to_json_serializable ( params )
71
+ add_metadata ( 'params' , params )
64
72
65
73
def finalizer ():
66
74
# TODO: This metadata is all in the form of error strings. It might be
67
75
# nice to extract the hypothesis failing inputs directly somehow.
68
76
if hasattr (request .node , 'hypothesis_report_information' ):
69
- json_metadata [ 'hypothesis_report_information' ] = request .node .hypothesis_report_information
77
+ add_metadata ( 'hypothesis_report_information' , request .node .hypothesis_report_information )
70
78
if hasattr (request .node , 'hypothesis_statistics' ):
71
- json_metadata [ 'hypothesis_statistics' ] = request .node .hypothesis_statistics
79
+ add_metadata ( 'hypothesis_statistics' , request .node .hypothesis_statistics )
72
80
73
81
request .addfinalizer (finalizer )
0 commit comments