Ejemplo n.º 1
0
def test_build_xml_export_doc_properties_many():
    prop_dict = {
        'foo': 'alpha',
        'bar': 'beta',
    }
    resp_prop_dict = {
        'foo': 'gama',
        'bar': 'omega',
    }
    export_doc = export.build_xml_export_doc(
        properties=prop_dict,
        response_properties=resp_prop_dict,
    )
    exp_xml = textwrap.dedent('''\
    <testcases>
      <response-properties>
        <response-property name="foo" value="gama"/>
        <response-property name="bar" value="omega"/>
      </response-properties>
      <properties>
        <property name="foo" value="alpha"/>
        <property name="bar" value="beta"/>
      </properties>
    </testcases>
    ''')
    assert xmltostring(export_doc) == exp_xml
Ejemplo n.º 2
0
def test_build_xml_export_doc_testcases_single():
    tc_doc = etree.Element("testcase")
    export_doc = export.build_xml_export_doc(testcases=[tc_doc])
    exp_xml = textwrap.dedent('''\
    <testcases>
      <testcase/>
    </testcases>
    ''')
    assert xmltostring(export_doc) == exp_xml
Ejemplo n.º 3
0
def test_build_xml_export_doc_properties_null():
    export_doc = export.build_xml_export_doc(
        properties={},
        response_properties={},
    )
    exp_xml = textwrap.dedent('''\
    <testcases/>
    ''')
    assert xmltostring(export_doc) == exp_xml
Ejemplo n.º 4
0
def test_build_xml_export_doc_properties_single():
    prop_dict = {'lookup-method': 'custom'}
    export_doc = export.build_xml_export_doc(properties=prop_dict)
    exp_xml = textwrap.dedent('''\
    <testcases>
      <properties>
        <property name="lookup-method" value="custom"/>
      </properties>
    </testcases>
    ''')
    assert xmltostring(export_doc) == exp_xml
Ejemplo n.º 5
0
def test_build_xml_export_doc_testcases_many():
    testcases = [
        etree.Element("testcase"),
        etree.Element("testcase"),
        etree.Element("testcase"),
    ]
    export_doc = export.build_xml_export_doc(testcases=testcases)
    exp_xml = textwrap.dedent('''\
    <testcases>
      <testcase/>
      <testcase/>
      <testcase/>
    </testcases>
    ''')
    assert xmltostring(export_doc) == exp_xml
Ejemplo n.º 6
0
def test_build_xml_export_doc_project_id():
    export_doc = export.build_xml_export_doc(project_id="FOOBAR")
    exp_xml = textwrap.dedent('''\
    <testcases project-id="FOOBAR"/>
    ''')
    assert xmltostring(export_doc) == exp_xml
Ejemplo n.º 7
0
def test_build_xml_export_doc_empty():
    export_doc = export.build_xml_export_doc()
    exp_xml = textwrap.dedent('''\
    <testcases/>
    ''')
    assert xmltostring(export_doc) == exp_xml
Ejemplo n.º 8
0
    def write_doc(self, docname, doctree):
        # type: (unicode, nodes.Node) -> None
        """Where you actually write something to the filesystem."""

        # hack: check if the document is a test case
        is_testcase_doc = False
        for node in doctree.traverse(test_action_node):
            is_testcase_doc = True
            break
        # we will produce xml export output for test cases only
        if not is_testcase_doc:
            return

        # initialize dict with properties for xml export file
        properties = {}

        # set test case id based on selected lookup method
        if self.app.config.pylatest_export_lookup_method == "custom":
            testcase_id = "/" + docname
            properties['lookup-method'] = 'custom'
        elif self.app.config.pylatest_export_lookup_method == "id":
            # get test case id from a field list
            # if the id can't be found there, testcase id attribute is omitted
            testcase_id = get_testcase_id(doctree)
            properties['lookup-method'] = 'id'
        elif self.app.config.pylatest_export_lookup_method == "id,custom":
            # custom lookup method is used, unless explicit id is specified
            # in the rst file
            testcase_id = get_testcase_id(doctree)
            properties['lookup-method'] = 'id'
            if testcase_id is None:
                testcase_id = "/" + docname
                properties['lookup-method'] = 'custom'
        else:
            # TODO: report the error in a better way?
            msg = "pylatest_export_lookup_method value is invalid"
            raise Exception(msg)

        # set test case id based on selected lookup method
        if self.app.config.pylatest_export_dry_run:
            properties['dry-run'] = 'true'

        # generate html output from the doctree
        destination = StringOutput(encoding='utf-8')  # TODO: what is this?
        doctree.settings = self.settings
        self.current_docname = docname
        self.writer.write(doctree, destination)

        # generate content of target xml file based on html output
        tc_doc = build_xml_testcase_doc(
            html_source=self.writer.output,
            content_type=self.app.config.pylatest_export_content_type,
            testcase_id=testcase_id,
            )

        # validate and drop invalid metadata if needed
        if len(self.app.config.pylatest_valid_export_metadata) > 0:
            for name in list(tc_doc.metadata.keys()):
                if name not in self.app.config.pylatest_valid_export_metadata:
                    del tc_doc.metadata[name]

        # create xml export document with single test case
        export_doc = build_xml_export_doc(
            project_id=self.app.config.pylatest_project_id,
            testcases=[tc_doc.build_element_tree()],
            properties=properties,
            response_properties=                                     # noqa
                self.app.config.pylatest_export_response_properties, # noqa
            )
        content_b = etree.tostring(
            export_doc,
            xml_declaration=True,
            encoding='utf-8',
            pretty_print=self.app.config.pylatest_export_pretty_print)
        content = content_b.decode('utf-8')

        # write content into file
        outfilename = path.join(
            self.outdir, os_path(docname) + self.out_suffix)
        ensuredir(path.dirname(outfilename))
        try:
            with codecs.open(outfilename, 'w', 'utf-8') as f:  # type: ignore
                f.write(content)
        except (IOError, OSError) as err:
            logger.warning("error writing file %s: %s", outfilename, err)