Skip to content

Commit befbd80

Browse files
committed
Add a check that the custom JSON report metadata is always JSON serializable
Otherwise it just shows up as a warning in the test results, which might easily be missed.
1 parent 0c94174 commit befbd80

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

reporting.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from types import BuiltinFunctionType, FunctionType
66
import dataclasses
7+
import json
78

89
from hypothesis.strategies import SearchStrategy
910

@@ -42,6 +43,13 @@ def add_api_name_to_metadata(request, json_metadata):
4243
"""
4344
Additional per-test metadata for --json-report
4445
"""
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+
4553
test_module = request.module.__name__
4654
if test_module.startswith('array_api_tests.meta'):
4755
return
@@ -54,20 +62,20 @@ def add_api_name_to_metadata(request, json_metadata):
5462
else:
5563
array_api_function_name = test_function[len('test_'):]
5664

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)
6068

6169
if hasattr(request.node, 'callspec'):
6270
params = request.node.callspec.params
63-
json_metadata['params'] = to_json_serializable(params)
71+
add_metadata('params', params)
6472

6573
def finalizer():
6674
# TODO: This metadata is all in the form of error strings. It might be
6775
# nice to extract the hypothesis failing inputs directly somehow.
6876
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)
7078
if hasattr(request.node, 'hypothesis_statistics'):
71-
json_metadata['hypothesis_statistics'] = request.node.hypothesis_statistics
79+
add_metadata('hypothesis_statistics', request.node.hypothesis_statistics)
7280

7381
request.addfinalizer(finalizer)

0 commit comments

Comments
 (0)