1616import mimetypes
1717import os
1818import traceback
19+ from collections import defaultdict
1920from functools import wraps
2021from os import PathLike
2122from typing import Any , Callable , Optional , Union
3940from behave_reportportal .config import Config , LogLayout
4041from behave_reportportal .utils import Singleton
4142
43+ STATUS_MAPPINGS : dict [str , str ] = defaultdict (lambda : "FAILED" )
44+ STATUS_MAPPINGS ["passed" ] = "PASSED"
45+ STATUS_MAPPINGS ["failed" ] = "FAILED"
46+ STATUS_MAPPINGS ["error" ] = "ERROR"
47+ STATUS_MAPPINGS ["skipped" ] = "SKIPPED"
48+
49+
50+ def convert_to_rp_status (behave_status : str ) -> str :
51+ """
52+ Convert behave test result status to ReportPortal status.
53+
54+ :param behave_status: behave test result status
55+ :return: ReportPortal test result status
56+ """
57+ return STATUS_MAPPINGS [behave_status ]
58+
4259
4360def check_rp_enabled (func : Callable ) -> Callable :
4461 """Verify is RP is enabled in config."""
@@ -164,7 +181,7 @@ def finish_feature(self, context: Context, feature: Feature, status: Optional[st
164181 self ._rp .finish_test_item (
165182 item_id = self ._feature_id ,
166183 end_time = timestamp (),
167- status = status or self . convert_to_rp_status (feature .status .name ),
184+ status = status or convert_to_rp_status (feature .status .name ),
168185 ** kwargs ,
169186 )
170187
@@ -199,14 +216,15 @@ def finish_scenario(
199216 """Finish scenario in ReportPortal."""
200217 if scenario .tags and "skip" in scenario .tags :
201218 status = "SKIPPED"
202- if scenario .status .name == "failed" :
219+ rp_status = convert_to_rp_status (scenario .status .name )
220+ if rp_status == "FAILED" :
203221 self ._log_skipped_steps (context , scenario )
204222 self ._log_scenario_exception (scenario )
205223 self ._log_cleanups (context , "scenario" )
206224 self ._rp .finish_test_item (
207225 item_id = self ._scenario_id ,
208226 end_time = timestamp (),
209- status = status or self . convert_to_rp_status ( scenario . status . name ) ,
227+ status = status or rp_status ,
210228 ** kwargs ,
211229 )
212230 self ._log_item_id = self ._feature_id
@@ -321,12 +339,13 @@ def _build_step_content(step: Step) -> str:
321339 return txt
322340
323341 def _finish_step_step_based (self , step : Step , status : Optional [str ] = None , ** kwargs : Any ) -> None :
324- if step .status .name == "failed" :
342+ rp_status = convert_to_rp_status (step .status .name )
343+ if rp_status == "FAILED" :
325344 self ._log_step_exception (step , self ._step_id )
326345 self ._rp .finish_test_item (
327346 item_id = self ._step_id ,
328347 end_time = timestamp (),
329- status = status or self . convert_to_rp_status ( step . status . name ) ,
348+ status = status or rp_status ,
330349 ** kwargs ,
331350 )
332351 self ._log_item_id = self ._scenario_id
@@ -340,7 +359,7 @@ def _finish_step_scenario_based(self, step: Step, **kwargs: Any) -> None:
340359 level = "INFO" ,
341360 ** kwargs ,
342361 )
343- if step .status .name == "failed " :
362+ if convert_to_rp_status ( step .status .name ) == "FAILED " :
344363 self ._log_step_exception (step , self ._scenario_id )
345364
346365 def _log_step_exception (self , step : Step , item_id : Optional [str ]) -> None :
@@ -506,23 +525,3 @@ def _test_case_id(scenario: Scenario) -> Optional[Any]:
506525 return None
507526 return tc_id
508527 return None
509-
510- @staticmethod
511- def convert_to_rp_status (behave_status : str ) -> str :
512- """
513- Convert behave test result status to ReportPortal status.
514-
515- :param behave_status: behave test result status
516- :return: ReportPortal test result status
517- """
518- if behave_status == "passed" :
519- return "PASSED"
520- elif behave_status == "failed" :
521- return "FAILED"
522- elif behave_status == "skipped" :
523- return "SKIPPED"
524- elif behave_status == "error" :
525- return "FAILED"
526- else :
527- # todo define what to do
528- return "PASSED"
0 commit comments