当前位置: 首页>>代码示例>>Python>>正文


Python xmlunittest.XmlTestCase类代码示例

本文整理汇总了Python中xmlunittest.XmlTestCase的典型用法代码示例。如果您正苦于以下问题:Python XmlTestCase类的具体用法?Python XmlTestCase怎么用?Python XmlTestCase使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了XmlTestCase类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_assertXmlEquivalentOutputs_namespaces

    def test_assertXmlEquivalentOutputs_namespaces(self):
        """Asserts assertXmlEquivalentOutputs raises when comparison failed.

        Assertion with different namespaces: the namespace URI is the same,
        but the prefix is different. In this case, the two XML are equivalents.

        """
        test_case = XmlTestCase(methodName='assertXmlEquivalentOutputs')

        # Same XML, but with different namespace prefixes
        data = b"""<?xml version="1.0" encoding="UTF-8" ?>
        <root xmlns:foo="mynamespace">
            <foo:tag>foo</foo:tag>
        </root>"""

        expected = b"""<?xml version="1.0" encoding="UTF-8" ?>
        <root xmlns:bar="mynamespace">
            <bar:tag>foo</bar:tag>
        </root>"""

        test_case.assertXmlEquivalentOutputs(data, expected)

        wrong_namespace = b"""<?xml version="1.0" encoding="UTF-8" ?>
        <root xmlns:foo="not_the_same_namespace">
            <foo:tag>foo</foo:tag>
        </root>
        """

        with self.assertRaises(test_case.failureException):
            test_case.assertXmlEquivalentOutputs(wrong_namespace, expected)
开发者ID:PonteIneptique,项目名称:python-xmlunittest,代码行数:30,代码来源:test.py

示例2: test_assertXpathValues

    def test_assertXpathValues(self):
        """Asserts assertXpathValues raises when validation failed.

        Method assertXpathValues raises when not each XPath expression's result
        is in the expected values.

        """
        test_case = XmlTestCase(methodName='assertXpathValues')
        data = b"""<?xml version="1.0" encoding="UTF-8" ?>
        <root>
            <sub id="1">a</sub>
            <sub id="2">a</sub>
            <sub id="3">b</sub>
            <sub id="4">c</sub>
        </root>"""
        root = test_case.assertXmlDocument(data)

        test_case.assertXpathValues(root, './sub/@id', ['1', '2', '3', '4'])
        test_case.assertXpathValues(root, './sub/text()', ['a', 'b', 'c'])

        with self.assertRaises(test_case.failureException):
            test_case.assertXpathValues(root, './sub/@id', ['1', '2'])

        with self.assertRaises(test_case.failureException):
            test_case.assertXpathValues(root, './sub/text()', ['a', 'b'])
开发者ID:AlekseiCherkes,项目名称:python-xmlunittest,代码行数:25,代码来源:test.py

示例3: test_assertXpathsExist

    def test_assertXpathsExist(self):
        """Asserts assertXpathsExist raises when validation failed.

        Method assertXpathsExist raises when any xpath does not select a least
        one result.

        """
        test_case = XmlTestCase(methodName='assertXpathsExist')
        data = b"""<?xml version="1.0" encoding="UTF-8" ?>
        <root att="exists">
            <sub subAtt="input"/>
            <sub/>
        </root>"""

        root = test_case.assertXmlDocument(data)
        xpaths = ['@att', './sub', './sub[@subAtt="input"]']
        test_case.assertXpathsExist(root, xpaths)

        with self.assertRaises(test_case.failureException):
            test_case.assertXpathsExist(root, ['@invalidAtt'])

        with self.assertRaises(test_case.failureException):
            test_case.assertXpathsExist(root, ['./invalidChild'])

        with self.assertRaises(test_case.failureException):
            test_case.assertXpathsExist(root, ['./sub[@subAtt="invalid"]'])
开发者ID:AlekseiCherkes,项目名称:python-xmlunittest,代码行数:26,代码来源:test.py

示例4: _test_assertXMLValid

    def _test_assertXMLValid(self, schema):
        test_case = XmlTestCase(methodName='assertXmlValid')

        # Using a valid doc
        test_case.assertXmlValid(self._valid_xml, schema)

        # Using an invalid doc
        with self.assertRaises(self.failureException) as cm:
            test_case.assertXmlValid(self._invalid_xml, schema)

        # The error message mentions the unwanted element
        self.assertIn('father', str(cm.exception))
开发者ID:glenfant,项目名称:python-xmlunittest,代码行数:12,代码来源:test.py

示例5: test_assertXmlPartial

    def test_assertXmlPartial(self):
        """Asserts assertXmlPartial raises when data is invalid.

        Method assertXmlPartial must be able to take a partial XML formated
        string and returns a valid XML document, or raise an error.

        """
        test_case = XmlTestCase(methodName='assertXmlPartial')
        data = b"""<partial>1</partial>
        <partial>2</partial>"""

        root = test_case.assertXmlPartial(data)
        self.assertIsInstance(root, etree._Element)

        self.assertEqual(root.tag, test_case.default_partial_tag)
        self.assertEqual(len(root), 2)

        with self.assertRaises(test_case.failureException):
            test_case.assertXmlPartial(b'<invalidChar>&</invalidChar>')

        with self.assertRaises(test_case.failureException):
            test_case.assertXmlPartial(b'not even a partial XML document')

        with self.assertRaises(test_case.failureException):
            test_case.assertXmlPartial(b'<missingEndTag>')
开发者ID:AlekseiCherkes,项目名称:python-xmlunittest,代码行数:25,代码来源:test.py

示例6: test_assertXpathsUniqueValue

    def test_assertXpathsUniqueValue(self):
        """Asserts assertXpathsUniqueValue raises when validation failed.

        Method assertXpathsUniqueValue raises when one of XPath expression
        select does not returns unique results.

        """
        test_case = XmlTestCase(methodName='assertXpathsUniqueValue')
        data = b"""<?xml version="1.0" encoding="UTF-8" ?>
        <root>
            <sub subAtt="unique" id="1">unique 1</sub>
            <sub subAtt="notUnique" id="2">unique 2</sub>
            <sub subAtt="notUnique" id="3">unique 3</sub>
            <multiple>twice</multiple>
            <multiple>twice</multiple>
        </root>"""
        root = test_case.assertXmlDocument(data)

        test_case.assertXpathsUniqueValue(root, ['./sub/@id',
                                                 './sub/text()'])

        with self.assertRaises(test_case.failureException):
            test_case.assertXpathsUniqueValue(root, ['./sub/@subAtt'])

        with self.assertRaises(test_case.failureException):
            test_case.assertXpathsUniqueValue(root, ['./multiple/text()'])
开发者ID:AlekseiCherkes,项目名称:python-xmlunittest,代码行数:26,代码来源:test.py

示例7: test_assertXpathsOnlyOne

    def test_assertXpathsOnlyOne(self):
        """Asserts assertXpathsOnlyOne raises when validation failed.

        Method assertXpathsOnlyOne raises when one of XPath
        expressions does not select one and exactly one result.

        """
        test_case = XmlTestCase(methodName='assertXpathsOnlyOne')
        data = b"""<?xml version="1.0" encoding="UTF-8" ?>
        <root>
            <sub subAtt="unique" id="1" />
            <sub subAtt="notUnique" id="2"/>
            <sub subAtt="notUnique" id="3"/>
            <uniqueSub/>
        </root>"""

        root = test_case.assertXmlDocument(data)
        unique_for_each = ['./uniqueSub',
                           './sub[@subAtt="unique"]']
        test_case.assertXpathsOnlyOne(root, unique_for_each)

        with self.assertRaises(test_case.failureException):
            test_case.assertXpathsOnlyOne(root, ['./invalidChild'])

        with self.assertRaises(test_case.failureException):
            test_case.assertXpathsOnlyOne(root, ['./sub[@subAtt="notUnique"]'])
开发者ID:AlekseiCherkes,项目名称:python-xmlunittest,代码行数:26,代码来源:test.py

示例8: test_assertXmlValidDTD_no_dtd

    def test_assertXmlValidDTD_no_dtd(self):
        """Asserts assertXmlValidDTD raises ValueError without any DTD."""
        test_case = XmlTestCase(methodName='assertXmlValidDTD')

        data = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="child1"/>
        </root>
        """
        root = test_case.assertXmlDocument(data)

        # No DTD: ValueError
        with self.assertRaises(ValueError):
            test_case.assertXmlValidDTD(root)
开发者ID:AlekseiCherkes,项目名称:python-xmlunittest,代码行数:14,代码来源:test.py

示例9: test_assertXmlPartial_name

    def test_assertXmlPartial_name(self):
        """Asserts assertXmlPartial raises when data is invalid.

        Method assertXmlPartial accept a `root_tag` parameter to tell
        method the root element's tag name.

        """
        test_case = XmlTestCase(methodName='assertXmlPartial')
        data = b"""<partial>1</partial>
        <partial>2</partial>"""

        root = test_case.assertXmlPartial(data, root_tag='customTag')
        self.assertIsInstance(root, etree._Element)

        self.assertEqual(root.tag, 'customTag')
        self.assertEqual(len(root), 2)

        with self.assertRaises(test_case.failureException):
            test_case.assertXmlPartial(b'<invalidChar>&</invalidChar>',
                                       root_tag='customTag')

        with self.assertRaises(test_case.failureException):
            test_case.assertXmlPartial(b'not even a partial XML document',
                                       root_tag='customTag')

        with self.assertRaises(test_case.failureException):
            test_case.assertXmlPartial(b'<missingEndTag>',
                                       root_tag='customTag')
开发者ID:AlekseiCherkes,项目名称:python-xmlunittest,代码行数:28,代码来源:test.py

示例10: test_assertXmlDocument

    def test_assertXmlDocument(self):
        """Asserts assertXmlDocument raises when data is invalid.

        At this time, assertXmlDocument only test XML data is a valid XML
        document, but it can be a fragment of XML only. This does not test
        the XML declaration nor any doctype declaration.

        """
        test_case = XmlTestCase(methodName='assertXmlDocument')
        data = b"""<?xml version="1.0" encoding="UTF-8" ?>
        <root/>"""

        root = test_case.assertXmlDocument(data)
        self.assertIsInstance(root, etree._Element)

        with self.assertRaises(test_case.failureException):
            test_case.assertXmlDocument('not an XML document')
开发者ID:AlekseiCherkes,项目名称:python-xmlunittest,代码行数:17,代码来源:test.py

示例11: test_assertXmlValidDTD

    def test_assertXmlValidDTD(self):
        """Asserts assertXmlValidDTD raises when DTD does not valid XML."""
        test_case = XmlTestCase(methodName='assertXmlValidDTD')

        dtd = """<!ELEMENT root (child)>
        <!ELEMENT child EMPTY>
        <!ATTLIST child id ID #REQUIRED>
        """

        data = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="child1"/>
        </root>
        """
        root = test_case.assertXmlDocument(data)

        # Document is valid according to DTD
        test_case.assertXmlValidDTD(root, dtd)

        data_invalid = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="child1"/>
            <child id="child1"/>
        </root>
        """
        root = test_case.assertXmlDocument(data_invalid)

        # Document is invalid according to DTD (multiple child element)
        with self.assertRaises(test_case.failureException):
            test_case.assertXmlValidDTD(root, dtd)
开发者ID:AlekseiCherkes,项目名称:python-xmlunittest,代码行数:30,代码来源:test.py

示例12: test_assertXmlValidRelaxNG

    def test_assertXmlValidRelaxNG(self):
        """Asserts assertXmlValidRelaxNG raises when schema does not valid XML.
        """
        test_case = XmlTestCase(methodName='assertXmlValidRelaxNG')

        relaxng = b"""<?xml version="1.0" encoding="utf-8"?>
        <rng:element name="root" xmlns:rng="http://relaxng.org/ns/structure/1.0">
            <rng:element name="child">
                <rng:attribute name="id">
                    <rng:text />
                </rng:attribute>
            </rng:element>
        </rng:element>
        """

        data = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="valid"/>
        </root>
        """
        root = test_case.assertXmlDocument(data)

        test_case.assertXmlValidRelaxNG(root, relaxng)

        data_invalid = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="valid"/>
            <child id="tooManyChild"/>
        </root>
        """
        root = test_case.assertXmlDocument(data_invalid)

        with self.assertRaises(test_case.failureException):
            test_case.assertXmlValidRelaxNG(root, relaxng)
开发者ID:AlekseiCherkes,项目名称:python-xmlunittest,代码行数:34,代码来源:test.py

示例13: test_assertXmlValidDTD_DTD

    def test_assertXmlValidDTD_DTD(self):
        """Asserts assertXmlValidDTD accepts an LXML DTD object."""
        test_case = XmlTestCase(methodName='assertXmlValidDTD')

        dtd = """<!ELEMENT root (child)>
        <!ELEMENT child EMPTY>
        <!ATTLIST child id ID #REQUIRED>
        """
        schema = etree.DTD(io.StringIO(dtd))

        data = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="child1"/>
        </root>
        """
        root = test_case.assertXmlDocument(data)

        # Document is valid according to DTD
        test_case.assertXmlValidDTD(root, schema)

        data_invalid = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="child1"/>
            <child id="child1"/>
        </root>
        """
        root = test_case.assertXmlDocument(data_invalid)

        # Document is invalid according to DTD (multiple child element)
        with self.assertRaises(test_case.failureException):
            test_case.assertXmlValidDTD(root, schema)
开发者ID:AlekseiCherkes,项目名称:python-xmlunittest,代码行数:31,代码来源:test.py

示例14: test_assertXmlValidXSchema_filename

    def test_assertXmlValidXSchema_filename(self):
        """Asserts assertXmlValidXSchema raises when schema does not valid XML.
        """
        test_case = XmlTestCase(methodName='assertXmlValidXSchema')

        xschema = b"""<?xml version="1.0" encoding="utf-8"?>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            <xsd:element name="root">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="child" minOccurs="1" maxOccurs="1">
                            <xsd:complexType>
                                <xsd:simpleContent>
                                    <xsd:extension base="xsd:string">
                                        <xsd:attribute name="id" type="xsd:string" use="required" />
                                    </xsd:extension>
                                </xsd:simpleContent>
                            </xsd:complexType>
                        </xsd:element>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:schema>
        """

        filename = 'test_assertXmlValidXSchema_filename.xml'
        with open(filename, 'w') as xchema_file:
            xchema_file.write(xschema.encode('utf8'))

        data = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="valid"/>
        </root>
        """
        root = test_case.assertXmlDocument(data)

        try:
            test_case.assertXmlValidXSchema(root, filename=filename)
        except:
            os.unlink(filename)
            raise

        data_invalid = b"""<?xml version="1.0" encoding="utf-8"?>
        <root>
            <child id="valid"/>
            <child id="tooManyChild"/>
        </root>
        """
        root = test_case.assertXmlDocument(data_invalid)

        try:
            with self.assertRaises(test_case.failureException):
                test_case.assertXmlValidXSchema(root, filename=filename)
        finally:
            os.unlink(filename)
开发者ID:AlekseiCherkes,项目名称:python-xmlunittest,代码行数:55,代码来源:test.py

示例15: test_assertXmlNode_tag_text

    def test_assertXmlNode_tag_text(self):
        """Asserts assertXmlNode raises when node is invalid.

        Method assertXmlNode raises if node has not the expected tag name
        or the expected text value.

        """
        test_case = XmlTestCase(methodName='assertXmlNode')
        data = b"""<?xml version="1.0" encoding="UTF-8" ?>
        <root>text_value</root>"""

        root = test_case.assertXmlDocument(data)

        test_case.assertXmlNode(root, tag='root', text='text_value')

        with self.assertRaises(test_case.failureException):
            test_case.assertXmlNode(root, tag='root', text='invalid')

        with self.assertRaises(test_case.failureException):
            test_case.assertXmlNode(root, tag='noRoot', text='text_value')

        with self.assertRaises(test_case.failureException):
            test_case.assertXmlNode(root, tag='noRoot', text='invalid')
开发者ID:AlekseiCherkes,项目名称:python-xmlunittest,代码行数:23,代码来源:test.py


注:本文中的xmlunittest.XmlTestCase类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。