21
21
from causal_testing .specification .scenario import Scenario
22
22
from causal_testing .specification .variable import Input , Meta , Output
23
23
from causal_testing .testing .causal_test_case import CausalTestCase
24
+ from causal_testing .testing .causal_test_result import CausalTestResult
24
25
from causal_testing .testing .causal_test_engine import CausalTestEngine
25
26
from causal_testing .testing .estimators import Estimator
26
27
from causal_testing .testing .base_test_case import BaseTestCase
@@ -124,28 +125,28 @@ def run_json_tests(self, effects: dict, estimators: dict, f_flag: bool = False,
124
125
effect = test .get ("effect" , "direct" ),
125
126
)
126
127
assert len (test ["expected_effect" ]) == 1 , "Can only have one expected effect."
127
- concrete_tests = [
128
- CausalTestCase (
129
- base_test_case = base_test_case ,
130
- expected_causal_effect = next (
131
- effects [effect ] for variable , effect in test ["expected_effect" ].items ()
132
- ),
133
- estimate_type = "coefficient" ,
134
- effect_modifier_configuration = {
135
- self .scenario .variables [v ] for v in test .get ("effect_modifiers" , [])
136
- },
137
- )
138
- ]
139
- failures = self ._execute_tests (concrete_tests , test , f_flag )
128
+ causal_test_case = CausalTestCase (
129
+ base_test_case = base_test_case ,
130
+ expected_causal_effect = next (
131
+ effects [effect ] for variable , effect in test ["expected_effect" ].items ()
132
+ ),
133
+ estimate_type = "coefficient" ,
134
+ effect_modifier_configuration = {
135
+ self .scenario .variables [v ] for v in test .get ("effect_modifiers" , [])
136
+ },
137
+ )
138
+ failed , result = self ._execute_test_case (causal_test_case = causal_test_case , test = test , f_flag = f_flag )
139
+ result = ('\n ' ).join (str (result ).split ("\n " ))
140
140
msg = (
141
141
f"Executing test: { test ['name' ]} \n "
142
- + f" { concrete_tests [0 ]} \n "
143
- + f" { failures } /{ len (concrete_tests )} failed for { test ['name' ]} "
142
+ + f" { causal_test_case } \n "
143
+ + f" { result } ==============\n "
144
+ + f" Result: { 'FAILED' if failed else 'Passed' } "
144
145
)
145
146
else :
146
147
abstract_test = self ._create_abstract_test_case (test , mutates , effects )
147
148
concrete_tests , dummy = abstract_test .generate_concrete_tests (5 , 0.05 )
148
- failures = self ._execute_tests (concrete_tests , test , f_flag )
149
+ failures , details = self ._execute_tests (concrete_tests , test , f_flag )
149
150
150
151
msg = (
151
152
f"Executing test: { test ['name' ]} \n "
@@ -173,7 +174,8 @@ def run_json_tests(self, effects: dict, estimators: dict, f_flag: bool = False,
173
174
treatment_value = test ["treatment_value" ],
174
175
estimate_type = test ["estimate_type" ],
175
176
)
176
- if self ._execute_test_case (causal_test_case = causal_test_case , test = test , f_flag = f_flag ):
177
+ failed , result = self ._execute_test_case (causal_test_case = causal_test_case , test = test , f_flag = f_flag )
178
+ if failed :
177
179
result = "failed"
178
180
else :
179
181
result = "passed"
@@ -189,13 +191,15 @@ def run_json_tests(self, effects: dict, estimators: dict, f_flag: bool = False,
189
191
190
192
def _execute_tests (self , concrete_tests , test , f_flag ):
191
193
failures = 0
194
+ details = []
192
195
if "formula" in test :
193
196
self ._append_to_file (f"Estimator formula used for test: { test ['formula' ]} " )
194
197
for concrete_test in concrete_tests :
195
- failed = self ._execute_test_case (concrete_test , test , f_flag )
198
+ failed , result = self ._execute_test_case (concrete_test , test , f_flag )
199
+ details .append (result )
196
200
if failed :
197
201
failures += 1
198
- return failures
202
+ return failures , details
199
203
200
204
def _json_parse (self ):
201
205
"""Parse a JSON input file into inputs, outputs, metas and a test plan"""
@@ -213,7 +217,7 @@ def _populate_metas(self):
213
217
for meta in self .scenario .variables_of_type (Meta ):
214
218
meta .populate (self .data )
215
219
216
- def _execute_test_case (self , causal_test_case : CausalTestCase , test : Iterable [Mapping ], f_flag : bool ) -> bool :
220
+ def _execute_test_case (self , causal_test_case : CausalTestCase , test : Iterable [Mapping ], f_flag : bool ) -> ( bool , CausalTestResult ) :
217
221
"""Executes a singular test case, prints the results and returns the test case result
218
222
:param causal_test_case: The concrete test case to be executed
219
223
:param test: Single JSON test definition stored in a mapping (dict)
@@ -249,7 +253,7 @@ def _execute_test_case(self, causal_test_case: CausalTestCase, test: Iterable[Ma
249
253
)
250
254
failed = True
251
255
logger .warning (" FAILED- expected %s, got %s" , causal_test_case .expected_causal_effect , result_string )
252
- return failed
256
+ return failed , causal_test_result
253
257
254
258
def _setup_test (
255
259
self , causal_test_case : CausalTestCase , test : Mapping , conditions : list [str ] = None
0 commit comments