Exemplo n.º 1
0
 def _fill_verdict(verdict: str, result: dict, testcase: etree.Element,
                   records: dict) -> None:
     # XUnit Pass maps to Passed in Polarion
     if verdict in Verdicts.PASS:
         records["passed"] += 1
     # XUnit Failure maps to Failed in Polarion
     elif verdict in Verdicts.FAIL:
         records["failures"] += 1
         verdict_data = {"type": "failure"}
         if result.get("comment"):
             verdict_data["message"] = utils.get_unicode_str(
                 result["comment"])
         etree.SubElement(testcase, "failure",
                          utils.sorted_dict(verdict_data))
     # XUnit Error maps to Blocked in Polarion
     elif verdict in Verdicts.SKIP:
         records["skipped"] += 1
         verdict_data = {"type": "error"}
         if result.get("comment"):
             verdict_data["message"] = utils.get_unicode_str(
                 result["comment"])
         etree.SubElement(testcase, "error",
                          utils.sorted_dict(verdict_data))
     # XUnit Skipped maps to Waiting in Polarion
     elif verdict in Verdicts.WAIT:
         records["waiting"] += 1
         verdict_data = {"type": "skipped"}
         if result.get("comment"):
             verdict_data["message"] = utils.get_unicode_str(
                 result["comment"])
         etree.SubElement(testcase, "skipped",
                          utils.sorted_dict(verdict_data))
Exemplo n.º 2
0
    def _requirement_element(self, parent_element: etree.Element,
                             req_data: dict) -> None:
        """Add requirement XML element."""
        req_data = self.requirement_transform.transform(req_data)
        if not req_data:
            return

        title = req_data.get("title")

        req_id = req_data.get("id")
        if not self._check_lookup_prop(req_id):
            LOGGER.warning(
                "Skipping requirement `%s`, data missing for selected lookup method",
                title)
            return

        attrs, custom_fields = self._classify_data(req_data)

        # For testing purposes, the order of fields in resulting XML
        # needs to be always the same.
        attrs = utils.sorted_dict(attrs)
        custom_fields = utils.sorted_dict(custom_fields)

        requirement = etree.SubElement(parent_element, "requirement", attrs)

        title_el = etree.SubElement(requirement, "title")
        title_el.text = utils.get_unicode_str(title)

        description = req_data.get("description")
        if description:
            description_el = etree.SubElement(requirement, "description")
            description_el.text = utils.get_unicode_str(description)

        self._fill_custom_fields(requirement, custom_fields)
Exemplo n.º 3
0
    def _testcase_element(self, parent_element: etree.Element,
                          testcase_data: dict, records: list) -> None:
        """Add testcase XML element."""
        nodeid = testcase_data.get("nodeid", "")
        if not self._is_whitelisted(nodeid):
            LOGGER.debug("Skipping blacklisted node: %s", nodeid)
            return

        testcase_data = self.testcases_transform.transform(testcase_data)
        if not testcase_data:
            return

        if testcase_data.get("ignored"):
            LOGGER.debug("Skipping ignored node: %s", nodeid)
            return

        testcase_title = testcase_data.get("title")
        self._set_lookup_prop(testcase_data)
        if not self._check_lookup_prop(testcase_data):
            LOGGER.warning(
                "Skipping testcase `%s`, data missing for selected lookup method",
                testcase_data.get("id") or testcase_title,
            )
            return

        # make sure that ID is set even for "name" lookup method
        testcase_data["id"] = self._get_testcase_id(testcase_data)
        records.append(testcase_data["id"])

        attrs, custom_fields = self._classify_data(testcase_data)

        # For testing purposes, the order of fields in resulting XML
        # needs to be always the same.
        attrs = utils.sorted_dict(attrs)
        custom_fields = utils.sorted_dict(custom_fields)

        testcase = etree.SubElement(parent_element, "testcase", attrs)

        title_el = etree.SubElement(testcase, "title")
        title_el.text = utils.get_unicode_str(testcase_title)

        description = testcase_data.get("description")
        if description:
            description_el = etree.SubElement(testcase, "description")
            description_el.text = utils.get_unicode_str(description)

        self._add_test_steps(testcase, testcase_data)
        self._fill_custom_fields(testcase, custom_fields)
        self._add_linked_items(testcase, testcase_data)
Exemplo n.º 4
0
 def _top_element(self) -> etree.Element:
     """Return top XML element."""
     attrs = {"project-id": self.config["polarion-project-id"]}
     document_relative_path = self.config.get(
         "requirements-document-relative-path")
     if document_relative_path:
         attrs["document-relative-path"] = document_relative_path
     top = etree.Element("requirements", utils.sorted_dict(attrs))
     return top
Exemplo n.º 5
0
    def __init__(self,
                 config: dict,
                 transform_func: Optional[Callable] = None) -> None:
        self.config = config
        self._transform_func = transform_func or transform_projects.get_requirements_transform(
            config)

        default_fields = self.config.get("requirements_default_fields") or {}
        default_fields = {
            k: utils.get_unicode_str(v)
            for k, v in default_fields.items() if v
        }
        self.default_fields = utils.sorted_dict(default_fields)
Exemplo n.º 6
0
    def _fill_custom_fields(parent: etree.Element,
                            custom_fields: dict) -> None:
        if not custom_fields:
            return

        custom_fields_el = etree.SubElement(parent, "custom-fields")
        for field, content in custom_fields.items():
            etree.SubElement(
                custom_fields_el,
                "custom-field",
                utils.sorted_dict({
                    "id": field,
                    "content": content
                }),
            )
Exemplo n.º 7
0
def import_junit(junit_file, **kwargs):
    """Read the content of the junit-results file produced by pytest and return imported data."""
    xml_root = _get_xml_root(junit_file)

    testcases = xml_root.xpath(".//testcase")
    if testcases is None:
        testcases = ()

    results = []
    for test_data in testcases:
        verdict, comment, properties = _parse_testcase_record(test_data)
        properties, parameters = _extract_parameters_from_properties(properties)

        testcase_id = properties.get("polarion-testcase-id")
        title = test_data.get("name")
        classname = test_data.get("classname")
        time = test_data.get("time", 0)
        filepath = test_data.get("file")

        data = {
            "id": testcase_id,
            "title": title,
            "classname": classname,
            "verdict": verdict,
            "comment": comment,
            "time": time,
            "file": filepath,
        }
        for key, value in properties.items():
            data[key] = value
        if parameters:
            data["params"] = utils.sorted_dict(parameters)

        results.append(utils.sorted_dict(data))

    return xunit_exporter.ImportedData(results=results, testrun=None)
Exemplo n.º 8
0
    def _testcase_element(
        parent_element: etree.Element,
        result: dict,
        records: dict,
        testcase_id: Optional[str],
        testcase_title: Optional[str],
    ) -> etree.Element:
        """Create XML element for given testcase result and update testcases records."""
        name = testcase_title or testcase_id
        if not name:
            raise Dump2PolarionException(
                "Neither `testcase_id` not `testcase_title` has valid value.")
        testcase_time = float(
            result.get("time") or result.get("duration") or 0)
        records["time"] += testcase_time

        testcase_data = {"name": name, "time": str(testcase_time)}
        if result.get("classname"):
            testcase_data["classname"] = result["classname"]
        testcase = etree.SubElement(parent_element, "testcase",
                                    utils.sorted_dict(testcase_data))
        return testcase