@@ -54,6 +54,8 @@ class _TestScenarioTOML(BaseModel):
5454 name : str
5555 job_status_check : bool = True
5656 tests : list [_TestRunTOML ] = Field (alias = "Tests" , min_length = 1 )
57+ pre_test : Optional [str ] = None
58+ post_test : Optional [str ] = None
5759
5860 @model_validator (mode = "after" )
5961 def check_no_self_dependency (self ):
@@ -99,9 +101,10 @@ class TestScenarioParser:
99101
100102 __test__ = False
101103
102- def __init__ (self , file_path : Path , test_mapping : Dict [str , Test ]) -> None :
104+ def __init__ (self , file_path : Path , test_mapping : Dict [str , Test ], hook_mapping : Dict [ str , TestScenario ] ) -> None :
103105 self .file_path = file_path
104106 self .test_mapping = test_mapping
107+ self .hook_mapping = hook_mapping
105108
106109 def parse (self ) -> TestScenario :
107110 """
@@ -136,8 +139,31 @@ def _parse_data(self, data: Dict[str, Any]) -> TestScenario:
136139 total_weight = sum (tr .weight for tr in ts_model .tests )
137140 normalized_weight = 0 if total_weight == 0 else 100 / total_weight
138141
142+ pre_test , post_test = None , None
143+ if ts_model .pre_test :
144+ pre_test = self .hook_mapping .get (ts_model .pre_test )
145+ if pre_test is None :
146+ msg = (
147+ f"Pre-test hook '{ ts_model .pre_test } ' not found in hook mapping. "
148+ "A corresponding hook should exist under 'conf/hook'. "
149+ "Ensure that a proper hook directory is set under the working directory."
150+ )
151+ logging .error (msg )
152+ raise TestScenarioParsingError (msg )
153+
154+ if ts_model .post_test :
155+ post_test = self .hook_mapping .get (ts_model .post_test )
156+ if post_test is None :
157+ msg = (
158+ f"Post-test hook '{ ts_model .post_test } ' not found in hook mapping. "
159+ "A corresponding hook should exist under 'conf/hook'. "
160+ "Ensure that a proper hook directory is set under the working directory."
161+ )
162+ logging .error (msg )
163+ raise TestScenarioParsingError (msg )
164+
139165 test_runs_by_id : dict [str , TestRun ] = {
140- tr .id : self ._create_test_run (tr , normalized_weight ) for tr in ts_model .tests
166+ tr .id : self ._create_test_run (tr , normalized_weight , pre_test , post_test ) for tr in ts_model .tests
141167 }
142168
143169 tests_data : dict [str , _TestRunTOML ] = {tr .id : tr for tr in ts_model .tests }
@@ -153,13 +179,21 @@ def _parse_data(self, data: Dict[str, Any]) -> TestScenario:
153179 job_status_check = ts_model .job_status_check ,
154180 )
155181
156- def _create_test_run (self , test_info : _TestRunTOML , normalized_weight : float ) -> TestRun :
182+ def _create_test_run (
183+ self ,
184+ test_info : _TestRunTOML ,
185+ normalized_weight : float ,
186+ pre_test : Optional [TestScenario ] = None ,
187+ post_test : Optional [TestScenario ] = None ,
188+ ) -> TestRun :
157189 """
158190 Create a section-specific Test object by copying from the test mapping.
159191
160192 Args:
161193 test_info (Dict[str, Any]): Information of the test.
162194 normalized_weight (float): Normalized weight for the test.
195+ pre_test (Optional[TestScenario]): TestScenario object representing the pre-test sequence.
196+ post_test (Optional[TestScenario]): TestScenario object representing the post-test sequence.
163197
164198 Returns:
165199 Test: Copied and updated Test object for the section.
@@ -192,5 +226,7 @@ def _create_test_run(self, test_info: _TestRunTOML, normalized_weight: float) ->
192226 sol = test_info .sol ,
193227 weight = test_info .weight * normalized_weight ,
194228 ideal_perf = test_info .ideal_perf ,
229+ pre_test = pre_test ,
230+ post_test = post_test ,
195231 )
196232 return tr
0 commit comments