Skip to main content

Library using lxml and unittest for unit testing XML.

Project description

https://travis-ci.org/Exirel/python-xmlunittest.svg?branch=master https://coveralls.io/repos/Exirel/python-xmlunittest/badge.svg?branch=master&service=github https://gemnasium.com/Exirel/python-xmlunittest.svg https://badge.fury.io/py/xmlunittest.svg

Anyone uses XML, for RSS, for configuration files, for… well, we all use XML for our own reasons (folk says one can not simply uses XML, but still…).

So, your code generates XML, and everything is fine. As you follow best practices (if you don’t, I think you should), you have written some good unit-tests, where you compare code’s result with an expected result. I mean you compare string with string. One day, something bad might happen.

XML is not a simple string, it is a structured document. One can not simply compare two XML string and expect everything to be fine: attributes’s order can change unexpectedly, elements can be optional, and no one can explain simply how spaces and tabs works in XML formatting.

Here comes XML unittest TestCase: if you want to use the built-in unittest package (or if it is a requirement), and you are not afraid of using xpath expression with lxml, this library is made for you.

You will be able to test your XML document, and use the power of xpath and various schema languages to write tests that matter.

How to

  • Extends xmlunittest.XmlTestCase

  • Write your tests, using the function or method that generate XML document

  • Use xmlunittest.XmlTestCase‘s assertion methods to validate

  • Keep your tests readable

Example:

from xmlunittest import XmlTestCase


class CustomTestCase(XmlTestCase):

    def test_my_custom_test(self):
        # In a real case, data come from a call to your function/method.
        data = """<?xml version="1.0" encoding="UTF-8" ?>
        <root xmlns:ns="uri">
            <leaf id="1" active="on" />
            <leaf id="2" active="on" />
            <leaf id="3" active="off" />
        </root>"""

        # Everything starts with `assertXmlDocument`
        root = self.assertXmlDocument(data)

        # Check namespace
        self.assertXmlNamespace(root, 'ns', 'uri')
        # Check
        self.assertXpathsUniqueValue(root, ('./leaf/@id', ))
        self.assertXpathValues(root, './leaf/@active', ('on', 'off'))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xmlunittest-0.5.0.tar.gz (11.8 kB view details)

Uploaded Source

File details

Details for the file xmlunittest-0.5.0.tar.gz.

File metadata

  • Download URL: xmlunittest-0.5.0.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for xmlunittest-0.5.0.tar.gz
Algorithm Hash digest
SHA256 9c7e11603fc03adcef8ee6af2a4e6162846305adedc4c4524ee42ac551b0d8ff
MD5 c6d9ed64b544149c06680ca047bd996d
BLAKE2b-256 06451f6ed447b4613f8fc30a0a22b35232224235f627ecceb1092ee3117f63dc

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page