@@ -40,14 +40,14 @@ def files(paths):
40
40
Each test file in the provided paths, as an array of test cases.
41
41
"""
42
42
for path in paths :
43
- yield json .loads (path .read_text ())
43
+ yield path , json .loads (path .read_text ())
44
44
45
45
46
46
def cases (paths ):
47
47
"""
48
48
Each test case within each file in the provided paths.
49
49
"""
50
- for test_file in files (paths ):
50
+ for _ , test_file in files (paths ):
51
51
yield from test_file
52
52
53
53
@@ -82,50 +82,62 @@ class SanityTests(unittest.TestCase):
82
82
assert cls .remote_files , "Didn't find the remote files!"
83
83
print (f"Found { len (cls .remote_files )} remote files" )
84
84
85
+ def assertUnique (self , iterable ):
86
+ """
87
+ Assert that the elements of an iterable are unique.
88
+ """
89
+
90
+ seen , duplicated = set (), set ()
91
+ for each in iterable :
92
+ if each in seen :
93
+ duplicated .add (each )
94
+ seen .add (each )
95
+ self .assertFalse (duplicated , "Elements are not unique." )
96
+
85
97
def test_all_test_files_are_valid_json (self ):
86
98
"""
87
99
All test files contain valid JSON.
88
100
"""
89
101
for path in self .test_files :
90
- try :
91
- json .loads (path .read_text ())
92
- except ValueError as error :
93
- self .fail (f"{ path } contains invalid JSON ({ error } )" )
102
+ with self .subTest (path = path ):
103
+ try :
104
+ json .loads (path .read_text ())
105
+ except ValueError as error :
106
+ self .fail (f"{ path } contains invalid JSON ({ error } )" )
94
107
95
108
def test_all_remote_files_are_valid_json (self ):
96
109
"""
97
110
All remote files contain valid JSON.
98
111
"""
99
112
for path in self .remote_files :
100
- try :
101
- json .loads (path .read_text ())
102
- except ValueError as error :
103
- self .fail (f"{ path } contains invalid JSON ({ error } )" )
113
+ with self .subTest (path = path ):
114
+ try :
115
+ json .loads (path .read_text ())
116
+ except ValueError as error :
117
+ self .fail (f"{ path } contains invalid JSON ({ error } )" )
104
118
105
119
def test_all_descriptions_have_reasonable_length (self ):
106
120
"""
107
121
All tests have reasonably long descriptions.
108
122
"""
109
123
for count , test in enumerate (tests (self .test_files )):
110
- description = test ["description" ]
111
- self .assertLess (
112
- len (description ),
113
- 70 ,
114
- f" { description !r } is too long! (keep it to less than 70 chars)"
115
- )
124
+ with self . subTest ( description = test ["description" ]):
125
+ self .assertLess (
126
+ len (test [ " description" ] ),
127
+ 70 ,
128
+ "Description is too long (keep it to less than 70 chars). "
129
+ )
116
130
print (f"Found { count } tests." )
117
131
118
132
def test_all_descriptions_are_unique (self ):
119
133
"""
120
134
All test cases have unique test descriptions in their tests.
121
135
"""
122
136
for count , case in enumerate (cases (self .test_files )):
123
- descriptions = set (test ["description" ] for test in case ["tests" ])
124
- self .assertEqual (
125
- len (descriptions ),
126
- len (case ["tests" ]),
127
- f"{ case !r} contains a duplicate description" ,
128
- )
137
+ with self .subTest (description = case ["description" ]):
138
+ self .assertUnique (
139
+ test ["description" ] for test in case ["tests" ]
140
+ )
129
141
print (f"Found { count } test cases." )
130
142
131
143
@unittest .skipIf (jsonschema is None , "Validation library not present!" )
@@ -141,12 +153,14 @@ class SanityTests(unittest.TestCase):
141
153
if Validator is not None :
142
154
test_files = collect (version )
143
155
for case in cases (test_files ):
144
- try :
145
- Validator .check_schema (case ["schema" ])
146
- except jsonschema .SchemaError as error :
147
- self .fail (
148
- f"{ case } contains an invalid schema ({ error } )" ,
149
- )
156
+ with self .subTest (case = case ):
157
+ try :
158
+ Validator .check_schema (case ["schema" ])
159
+ except jsonschema .SchemaError :
160
+ self .fail (
161
+ "Found an invalid schema."
162
+ "See the traceback for details on why."
163
+ )
150
164
else :
151
165
warnings .warn (f"No schema validator for { version .name } " )
152
166
@@ -157,11 +171,12 @@ class SanityTests(unittest.TestCase):
157
171
"""
158
172
Validator = jsonschema .validators .validator_for (TESTSUITE_SCHEMA )
159
173
validator = Validator (TESTSUITE_SCHEMA )
160
- for tests in files (self .test_files ):
161
- try :
162
- validator .validate (tests )
163
- except jsonschema .ValidationError as error :
164
- self .fail (str (error ))
174
+ for path , cases in files (self .test_files ):
175
+ with self .subTest (path = path ):
176
+ try :
177
+ validator .validate (cases )
178
+ except jsonschema .ValidationError as error :
179
+ self .fail (str (error ))
165
180
166
181
167
182
def main (arguments ):
0 commit comments