DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Introducing unittest-xml: testing XML in python

Introducing unittest-xml: testing XML in python

David Winterbottom user avatar by
David Winterbottom
·
Feb. 17, 12 · Interview
Like (0)
Save
Tweet
Share
4.47K Views

Join the DZone community and get the full member experience.

Join For Free

For some reason, I keep finding myself writing unit tests that need to make assertions about an XML document. To keep things DRY, I've packaged up my custom assertion methods as a PyPi module: unittest-xml. There's a small chance it may be useful to others.

Sample Usage

Enable the additional assert methods using a mixin:

import unittest
from xmltest import XMLAssertions


class SampleTestCase(unittest.TestCase, XMLAssertions):
    ...


Now suppose that the expected XML from some System Under Test (SUT) is:

<?xml version="1.0" encoding="UTF-8" ?>
<Response>
    <CardTxn>
        <authcode>060642</authcode>
        <card_scheme>Switch</card_scheme>
        <issuer country="UK">HSBC</issuer>
    </CardTxn>
    <reference>3000000088888888</reference>
    <merchantreference>1000001</merchantreference>
    <mode>LIVE</mode>
    <reason>ACCEPTED</reason>
    <status>1</status>
    <time>1071567305</time>
</Response>


then you can make assertions about the document using 3 additional assertions methods.

self.assertXPathNodeCount(RESPONSE, 1, 'CardTxn/authcode')
self.assertXPathNodeText(RESPONSE, 'LIVE', 'mode')
self.assertXPathNodeAttributes(RESPONSE, {'country': 'UK'}, 'CardTxn/issuer')


The first argument to each method is the XML string, the second is the expected value, while the third is the XPath query.

Installation

The standard way:

pip install unittest-xml


Discussion

Note, the implementation uses ElementTree and so only a subset of the XPath specification is implemented. However the above three assertion methods are sufficient for most scenarios.

The code is on Github, as usual.



Source: http://codeinthehole.com/writing/introducing-unittest-xml-testing-xml-in-python/

XML Python (language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Choose the Right Streaming Database
  • Create Spider Chart With ReactJS
  • DevOps vs Agile: Which Approach Will Win the Battle for Efficiency?
  • Solving the Kubernetes Security Puzzle

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: