def generate_bdd_xml(features): """ Generates the bdd xml """ if not features: raise RadishError("No features given to generate BDD xml file") duration = timedelta() for feature in features: if feature.state in [Step.State.PASSED, Step.State.FAILED]: duration += feature.duration testrun_element = etree.Element( "testrun", starttime=utils.datetime_to_str(features[0].starttime), endtime=utils.datetime_to_str(features[-1].endtime), duration=str(duration.total_seconds()), agent="{}@{}".format(getuser(), gethostname()) ) for feature in features: if not feature.has_to_run(world.config.scenarios, world.config.feature_tags, world.config.scenario_tags): continue feature_element = _get_element_from_model("feature", feature) description_element = etree.Element("description") description_element.text = etree.CDATA("\n".join(feature.description)) scenarios_element = etree.Element("scenarios") for scenario in (s for s in feature.all_scenarios if not isinstance(s, (ScenarioOutline, ScenarioLoop))): if not scenario.has_to_run(world.config.scenarios, world.config.feature_tags, world.config.scenario_tags): continue scenario_element = _get_element_from_model("scenario", scenario) for step in scenario.all_steps: step_element = _get_element_from_model("step", step) if step.state is Step.State.FAILED: failure_element = etree.Element( "failure", type=step.failure.name, message=step.failure.reason ) failure_element.text = etree.CDATA(_strip_ansi(step.failure.traceback)) step_element.append(failure_element) scenario_element.append(step_element) scenarios_element.append(scenario_element) feature_element.append(description_element) feature_element.append(scenarios_element) testrun_element.append(feature_element) with open(world.config.bdd_xml, "w+") as f: f.write(etree.tostring(testrun_element, pretty_print=True, xml_declaration=True, encoding="utf-8"))
def _get_element_from_model(self, what, model): """ Create a etree.Element from a given model """ duration = str(model.duration.total_seconds() ) if model.starttime and model.endtime else "" return etree.Element(what, sentence=model.sentence, id=str(model.id), result=model.state, starttime=utils.datetime_to_str(model.starttime), endtime=utils.datetime_to_str(model.endtime), duration=duration, testfile=model.path)
def _get_element_from_model(self, what, model): """ Create a etree.Element from a given model """ duration = str(model.duration.total_seconds()) if model.starttime and model.endtime else "" return etree.Element( what, sentence=model.sentence, id=str(model.id), result=model.state, starttime=utils.datetime_to_str(model.starttime), endtime=utils.datetime_to_str(model.endtime), duration=duration, testfile=model.path )
def _get_element_from_model(self, what, model): """ Create a etree.Element from a given model """ # round duration to 10 decimal points, to avoid it being printed in # scientific notation duration = "%.10f" % model.duration.total_seconds( ) if model.starttime and model.endtime else "" return etree.Element(what, sentence=model.sentence, id=str(model.id), result=model.state, starttime=utils.datetime_to_str(model.starttime), endtime=utils.datetime_to_str(model.endtime), duration=duration, testfile=model.path)
def generate_bdd_xml(self, features, marker): """ Generates the bdd xml """ from lxml import etree if not features: raise RadishError("No features given to generate BDD xml file") duration = timedelta() for feature in features: if feature.state in [Step.State.PASSED, Step.State.FAILED]: duration += feature.duration testrun_element = etree.Element( "testrun", starttime=utils.datetime_to_str(features[0].starttime), endtime=utils.datetime_to_str(features[-1].endtime), duration=str(duration.total_seconds()), agent="{0}@{1}".format(getuser(), gethostname())) for feature in features: if not feature.has_to_run(world.config.scenarios): continue feature_element = self._get_element_from_model("feature", feature) description_element = etree.Element("description") description_element.text = etree.CDATA("\n".join( feature.description)) tags_element = etree.Element('tags') for tag in feature.tags: tag_element = etree.Element('tag') tag_element.text = tag.name tags_element.append(tag_element) scenarios_element = etree.Element("scenarios") for scenario in ( s for s in feature.all_scenarios if not isinstance(s, (ScenarioOutline, ScenarioLoop))): if not scenario.has_to_run(world.config.scenarios): continue scenario_element = self._get_element_from_model( "scenario", scenario) scenario_tags_element = etree.Element('tags') scenario_element.append(scenario_tags_element) for tag in scenario.tags: tag_element = etree.Element('tag') tag_element.text = tag.name if tag.arg: tag_element.text += '({0})'.format(tag.arg) scenario_tags_element.append(tag_element) for step in scenario.all_steps: step_element = self._get_element_from_model("step", step) if step.state is Step.State.FAILED: failure_element = etree.Element( "failure", type=step.failure.name, message=step.failure.reason) failure_element.text = etree.CDATA( self._strip_ansi(step.failure.traceback)) step_element.append(failure_element) scenario_element.append(step_element) scenarios_element.append(scenario_element) feature_element.append(description_element) feature_element.append(tags_element) feature_element.append(scenarios_element) testrun_element.append(feature_element) with open(world.config.bdd_xml, "w+") as f: content = etree.tostring(testrun_element, pretty_print=True, xml_declaration=True, encoding="utf-8") try: if not isinstance(content, str): content = content.decode("utf-8") except Exception: pass finally: f.write(content)