def _log_cleanups(self, context, scope): layer = next( iter( [ level for level in context._stack if level.get("@layer") == scope ] ), None, ) if not layer: return item_type = "AFTER_SUITE" if scope == "feature" else "AFTER_TEST" item_id = self._feature_id if scope == "feature" else self._scenario_id for cleanup in layer.get("@cleanups", []): msg = "Execution of '{}' cleanup function".format(cleanup.__name__) if self._cfg.step_based: self._step_id = self._step_id = self._rp.start_test_item( name=msg, start_time=timestamp(), item_type=item_type, parent_item_id=item_id, ) self._rp.finish_test_item(self._step_id, timestamp(), "PASSED") continue self._rp.log( timestamp(), msg, level="INFO", item_id=item_id, )
def _log_fixtures(self, item, item_type, parent_item_id): """ Log used fixtures for item. It will log records for scenario based approach and step for step based. """ if not item.tags: return for tag in item.tags: if not tag.startswith("fixture."): continue msg = "Using of '{}' fixture".format(tag[len("fixture.") :]) if self._cfg.step_based: self._step_id = self._rp.start_test_item( name=msg, start_time=timestamp(), item_type=item_type, parent_item_id=parent_item_id, ) self._rp.finish_test_item(self._step_id, timestamp(), "PASSED") continue self._rp.log( timestamp(), msg, level="INFO", item_id=parent_item_id, )
def start_launch(self, launch, mode=None, rerun=False, rerun_of=None, ts=None, skip_analytics=False): """Call start_launch method of the common client. :param launch: Instance of the Launch class :param mode: Launch mode :param rerun: Rerun mode. Allowable values 'True' of 'False' :param rerun_of: Rerun mode. Specifies launch to be re-runned. Should be used with the 'rerun' option. :param ts: Start time :param skip_analytics: Skip reporting of agent name and version to GA? :return: launch UUID """ sl_pt = { 'attributes': self._get_launch_attributes(launch.attributes), 'description': launch.doc, 'name': launch.name, 'mode': mode, 'rerun': rerun, 'rerunOf': rerun_of, 'start_time': ts or to_epoch(launch.start_time) or timestamp() } logger.debug( 'ReportPortal - Start launch: request_body={0}'.format(sl_pt)) if not skip_analytics: send_event(self.agent_name, self.agent_version) return self.rp.start_launch(**sl_pt)
def _finish_step_step_based(self, step, status=None, **kwargs): if step.status.name == "failed": self._log_step_exception(step, self._step_id) self._rp.finish_test_item( item_id=self._step_id, end_time=timestamp(), status=status or self.convert_to_rp_status(step.status.name), **kwargs) self._log_item_id = self._scenario_id
def finish_feature(self, context, feature, status=None, **kwargs): """Finish feature in Report Portal.""" if feature.tags and "skip" in feature.tags: status = "SKIPPED" self._log_cleanups(context, "feature") self._rp.finish_test_item( item_id=self._feature_id, end_time=timestamp(), status=status or self.convert_to_rp_status(feature.status.name), **kwargs)
def start_launch(self, context, **kwargs): """Start launch in Report Portal.""" self._launch_id = self._rp.launch_id or self._rp.start_launch( name=self._cfg.launch_name, start_time=timestamp(), attributes=self._get_launch_attributes(), description=self._cfg.launch_description, rerun=self._cfg.rerun, rerunOf=self._cfg.rerun_of, **kwargs) if not self._skip_analytics: send_event(self.agent_name, self.agent_version)
def _log_scenario_exception(self, scenario): message = ["Scenario '{}' finished with error.".format(scenario.name)] if scenario.exception: message.append(", ".join(scenario.exception.args)) if scenario.error_message: message.append(scenario.error_message) self._rp.log( item_id=self._scenario_id, time=timestamp(), level="ERROR", message="\n".join(message), )
def _finish_step_scenario_based(self, step, **kwargs): self._rp.log(item_id=self._scenario_id, time=timestamp(), message="[{keyword}]: {name}. {text}{table}".format( keyword=step.keyword, name=step.name, text=step.text or "", table=self._build_table_content(step.table), ), level="INFO", **kwargs) if step.status.name == "failed": self._log_step_exception(step, self._scenario_id)
def finish_launch(self, launch, ts=None): """Finish started launch. :param launch: Launch name :param ts: End time """ fl_rq = { 'end_time': ts or to_epoch(launch.end_time) or timestamp(), 'status': STATUS_MAPPING[launch.status] } logger.debug( 'ReportPortal - Finish launch: request_body={0}'.format(fl_rq)) self.rp.finish_launch(**fl_rq)
def finish_scenario(self, context, scenario, status=None, **kwargs): """Finish scenario in Report Portal.""" if scenario.tags and "skip" in scenario.tags: status = "SKIPPED" if scenario.status.name == "failed": self._log_scenario_exception(scenario) self._log_cleanups(context, "scenario"), self._rp.finish_test_item( item_id=self._scenario_id, end_time=timestamp(), status=status or self.convert_to_rp_status(scenario.status.name), **kwargs) self._log_item_id = self._feature_id
def log(self, message, ts=None): """Send log message to Report Portal. :param message: model.LogMessage object :param ts: Timestamp """ sl_rq = { 'attachment': message.attachment, 'item_id': message.item_id, 'level': LOG_LEVEL_MAPPING[message.level], 'message': message.message, 'time': ts or timestamp() } self.rp.log(**sl_rq)
def start_feature(self, context, feature, **kwargs): """Start feature in Report Portal.""" if feature.tags and "skip" in feature.tags: feature.skip("Marked with @skip") self._feature_id = self._rp.start_test_item( name=feature.name, start_time=timestamp(), item_type="SUITE", description=self._item_description(feature), code_ref=self._code_ref(feature), attributes=self._attributes(feature), **kwargs) self._log_fixtures(feature, "BEFORE_SUITE", self._feature_id) self._log_item_id = self._feature_id
def start_step(self, context, step, **kwargs): """Start test in Report Portal.""" if self._cfg.step_based: description = step.text or "" self._step_id = self._rp.start_test_item( name="[{keyword}]: {name}".format(keyword=step.keyword, name=step.name), start_time=timestamp(), item_type="STEP", parent_item_id=self._scenario_id, code_ref=self._code_ref(step), description=description + self._build_table_content(step.table), **kwargs) self._log_item_id = self._step_id
def finish_keyword(self, keyword, issue=None, ts=None): """Finish started keyword item. :param keyword: Instance of started keyword item :param issue: Corresponding issue if it exists :param ts: End time """ fta_rq = { 'end_time': ts or to_epoch(keyword.end_time) or timestamp(), 'issue': issue, 'item_id': keyword.rp_item_id, 'status': STATUS_MAPPING[keyword.status] } logger.debug( 'ReportPortal - Finish keyword: request_body={0}'.format(fta_rq)) self.rp.finish_test_item(**fta_rq)
def finish_suite(self, suite, issue=None, ts=None): """Finish started suite. :param suite: Instance of the started suite item :param issue: Corresponding issue if it exists :param ts: End time """ fta_rq = { 'end_time': ts or to_epoch(suite.end_time) or timestamp(), 'issue': issue, 'item_id': suite.rp_item_id, 'status': STATUS_MAPPING[suite.status] } logger.debug( 'ReportPortal - Finish suite: request_body={0}'.format(fta_rq)) self.rp.finish_test_item(**fta_rq)
def _log_step_exception(self, step, item_id): message = [ "Step [{keyword}]: {name} was finished with exception.".format( keyword=step.keyword, name=step.name) ] if step.exception: message.append(", ".join(step.exception.args)) if step.error_message: message.append(step.error_message) self._rp.log( item_id=item_id, time=timestamp(), level="ERROR", message="\n".join(message), )
def start_keyword(self, keyword, ts=None): """Call start_test method of the common client. :param keyword: model.Keyword object :param ts: Start time """ start_rq = { 'description': keyword.doc, 'has_stats': False, 'item_type': keyword.get_type(), 'name': keyword.get_name(), 'parent_item_id': keyword.rp_parent_item_id, 'start_time': ts or to_epoch(keyword.start_time) or timestamp() } logger.debug( 'ReportPortal - Start keyword: request_body={0}'.format(start_rq)) return self.rp.start_test_item(**start_rq)
def finish_test(self, test, issue=None, ts=None): """Finish started test case. :param test: Instance of started test item :param issue: Corresponding issue if it exists :param ts: End time """ fta_rq = { 'attributes': test.attributes, 'end_time': ts or to_epoch(test.end_time) or timestamp(), 'issue': issue, 'item_id': test.rp_item_id, 'status': STATUS_MAPPING[test.status] } logger.debug( 'ReportPortal - Finish test: request_body={0}'.format(fta_rq)) self.rp.finish_test_item(**fta_rq)
def start_scenario(self, context, scenario, **kwargs): """Start scenario in Report Portal.""" if scenario.tags and "skip" in scenario.tags: scenario.skip("Marked with @skip") self._scenario_id = self._rp.start_test_item( name=scenario.name, start_time=timestamp(), item_type="STEP", parent_item_id=self._feature_id, code_ref=self._code_ref(scenario), attributes=self._attributes(scenario), parameters=self._get_parameters(scenario), description=self._item_description(scenario), test_case_id=self._test_case_id(scenario), **kwargs) self._log_fixtures(scenario, "BEFORE_TEST", self._scenario_id) self._log_item_id = self._scenario_id
def _log(self, message, level, file_to_attach=None, item_id=None): attachment = None if file_to_attach: with open(file_to_attach, "rb") as f: attachment = { "name": os.path.basename(file_to_attach), "data": f.read(), "mime": mimetypes.guess_type(file_to_attach)[0] or "application/octet-stream", } self._rp.log( time=timestamp(), message=message, level=level, attachment=attachment, item_id=item_id, )
def start_suite(self, suite, ts=None): """Call start_test method of the common client. :param suite: model.Suite object :param ts: Start time :return: Suite UUID """ start_rq = { 'attributes': None, 'description': suite.doc, 'item_type': suite.type, 'name': suite.name, 'parent_item_id': suite.rp_parent_item_id, 'start_time': ts or to_epoch(suite.start_time) or timestamp() } logger.debug( 'ReportPortal - Start suite: request_body={0}'.format(start_rq)) return self.rp.start_test_item(**start_rq)
def start_launch(self, launch, mode=None, ts=None, skip_analytics=False): """Call start_launch method of the common client. :param launch: Instance of the Launch class :param mode: Launch mode :param ts: Start time :param skip_analytics: Skip reporting of agent name and version to GA? :return: launch UUID """ sl_pt = { 'attributes': self._get_launch_attributes(launch.attributes), 'description': launch.doc, 'name': launch.name, 'mode': mode, 'start_time': ts or to_epoch(launch.start_time) or timestamp() } logger.debug( 'ReportPortal - Start launch: request_body={0}'.format(sl_pt)) if not skip_analytics: send_event(self.agent_name, self.agent_version) return self.rp.start_launch(**sl_pt)
def start_test(self, test, ts=None): """Call start_test method of the common client. :param test: model.Test object :param ts: Start time """ # Item type should be sent as "STEP" until we upgrade to RPv6. # Details at: # https://github.com/reportportal/agent-Python-RobotFramework/issues/56 start_rq = { 'attributes': test.attributes, 'code_ref': test.code_ref, 'description': test.doc, 'item_type': 'STEP', 'name': test.name, 'parent_item_id': test.rp_parent_item_id, 'start_time': ts or to_epoch(test.start_time) or timestamp(), 'test_case_id': test.test_case_id } logger.debug( 'ReportPortal - Start test: request_body={0}'.format(start_rq)) return self.rp.start_test_item(**start_rq)
def finish_launch(self, context, **kwargs): """Finish launch in Report Portal.""" self._rp.finish_launch(end_time=timestamp(), **kwargs) self._rp.terminate()