1- import os
21import contextlib
2+ import os
33
44
55def defaultequalityfn (x , y ):
@@ -24,37 +24,47 @@ def __init__(
2424 self .equalityFunc = equalityFunc or defaultequalityfn
2525
2626 def run (self , solution ):
27- if self .is_secret :
28- with open (os .devnull , "w" ) as devnull :
29- with (
30- contextlib .redirect_stdout (devnull ),
31- contextlib .redirect_stderr (devnull ),
32- ):
33- result = solution (* self .args )
34- else :
35- result = solution (* self .args )
36- # This is to help students debug their code (their prints are not split by tests, so this helps)
37- print (f"-- Test: { self .name } : { result } --" )
38- if (
39- # If you override the equality function, type checking is on you!
40- self .equalityFunc != defaultequalityfn
41- or type (result ) is type (self .expected )
42- ) and self .equalityFunc (result , self .expected ):
43- return {
44- "name" : "Ukryty test" if self .is_secret else self .name ,
45- "points" : self .points ,
46- "max_points" : self .points ,
47- "expected" : "N/A" if self .is_secret else str (self .expected ),
48- "result" : "N/A" if self .is_secret else str (result ),
49- "is_secret" : self .is_secret ,
50- }
51- else :
27+ try :
28+ if self .is_secret :
29+ with open (os .devnull , "w" ) as devnull :
30+ with (
31+ contextlib .redirect_stdout (devnull ),
32+ contextlib .redirect_stderr (devnull ),
33+ ):
34+ result = solution (* self .args )
35+ else :
36+ result = solution (* self .args )
37+ # This is to help students debug their code (their prints are not split by tests, so this helps)
38+ print (f"-- Test: { self .name } : { result } --" )
39+ if (
40+ # If you override the equality function, type checking is on you!
41+ self .equalityFunc != defaultequalityfn
42+ or type (result ) is type (self .expected )
43+ ) and self .equalityFunc (result , self .expected ):
44+ return {
45+ "name" : "Ukryty test" if self .is_secret else self .name ,
46+ "points" : self .points ,
47+ "max_points" : self .points ,
48+ "expected" : "N/A" if self .is_secret else str (self .expected ),
49+ "result" : "N/A" if self .is_secret else str (result ),
50+ "is_secret" : self .is_secret ,
51+ }
52+ else :
53+ return {
54+ "name" : "Ukryty test" if self .is_secret else self .name ,
55+ "points" : 0 ,
56+ "max_points" : self .points ,
57+ "expected" : "N/A" if self .is_secret else str (self .expected ),
58+ "result" : "N/A" if self .is_secret else str (result ),
59+ "is_secret" : self .is_secret ,
60+ }
61+ except Exception as e :
5262 return {
5363 "name" : "Ukryty test" if self .is_secret else self .name ,
5464 "points" : 0 ,
5565 "max_points" : self .points ,
5666 "expected" : "N/A" if self .is_secret else str (self .expected ),
57- "result" : "N/A" if self . is_secret else str (result ) ,
67+ "result" : f"Exception: { str (e ) } " ,
5868 "is_secret" : self .is_secret ,
5969 }
6070
0 commit comments