本文整理匯總了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)
示例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'])
示例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"]'])
示例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))
示例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>')
示例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()'])
示例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"]'])
示例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)
示例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')
示例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')
示例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)
示例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)
示例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)
示例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)
示例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')