8

Does anyone know if there exists something like XMLUnit for C++? I'm looking for an easy way to check nodes, values, etc in a XML output. We are using Google test in Visual Studio but I suppose any library that makes the work easier will be enough.

I'm using Xerces as an XML parser but XMLUnit (http://xmlunit.sourceforge.net/) has some features wrapped over the XML parser that are very useful for unit testing. For example, asserts using XPath expressions, functions to compare two "similar" XMLs, etc.

walrii
  • 3,472
  • 2
  • 28
  • 47
  • When you say "checks"... What do you mean? I have my own STL Template class for dealing with XML that works pretty damn good, and it's very tight, very efficient. Allows me to read nodes, check children, get the text of the nodes, along with all the properties... And it checks that the XML is valid, during runtime, on XML Open, etc. Are you looking for an XML Lib that will compare two XML files to see if they truly equal each other, even if the tags ARE formatted a little differently? – LarryF Feb 14 '09 at 01:45
  • Yea.. That can be a tall order. But I am working on another project that is used for comparing two 'string' to see how similar they are. With my XML class, it COULD read two files, and compare two nodes, and give you an idea of how closely 'related' they are... (The text, no the tags, but..) – LarryF Feb 16 '09 at 21:02

6 Answers6

1

I have used a combination of Xerces and CPPUnit to accomplish this in the past. In my test cases I would create a DOM object with the Xerces API in the setUp() function. This DOM would represent my expected results. In the test case itself I would then read the XML file and the class under test would populate a DOM object representing the contents of the file. To check equality I would walk through the two DOM trees via the Xerces API (DOMTreeWalker) and use CPPUnit assertions as I compared the contents of the DOM nodes. It was a bit tedious but there were no frameworks available at the time that could mimic XmlUnit. I would imagine that Google Test would work just as well as CPPUnit for accomplishing this task.

The Xerces API has some support for XPath expressions:

http://xerces.apache.org/xerces-c/faq-parse-3.html#faq-2

For validation you would need to set up an error handler as mentioned here and incorporate it into your test case:

Validating document in Xerces C++

For XSLT transform checking you would need to use Xalan. It works with Xerces so I wouldn't anticipate any major difficulties:

http://xalan.apache.org/old/xalan-c/index.html

I was not able to locate any obvious products that packaged XMLUnit-like operations in C++. So the answer is I think you will have to roll your own. Good luck.

Community
  • 1
  • 1
Jeff White
  • 43
  • 7
0

I really like http://pugixml.org/

It:

  • is stable
  • is extremely fast
  • has great documentation and sample code
  • is licensed under the MIT license
  • is very STL friendly
  • is still quite an active project
  • has great support for xpath
Homer6
  • 15,034
  • 11
  • 61
  • 81
  • Correct me if I'm wrong, but it looks like just another xml parser. What does it have for unit testing? It has operators for comparing, but recommends that they only be used for associative containers. – walrii Aug 01 '12 at 02:51
  • Yes, you're right. Sorry, it doesn't have the specific features that you're after. – Homer6 Aug 01 '12 at 04:01
  • Darn. I was hoping I'd missed something :) – walrii Aug 02 '12 at 02:00
-1

I use Boost property_tree for xml, easy to use, pretty robust and works well with Boost unit test framework.

clanmjc
  • 299
  • 1
  • 9
  • I can see how to put xml into and out of a property tree, but I don't see any features for unit testing. Is there a way to compare property trees? Can it handle comparisons where child order does/does not matter? Please tell me how you used it for unit testing. – walrii Aug 01 '12 at 02:59
-1

You can use tinyxml package here: tinyxml

I'm working with it and it's quite friendly and bug free.

It's an xml handling. I guess it wasn't designed for unit testing, but you can use it to check/test your xml files. It as expected loads the xml into a DOM object and supplies a nice API to run on the nodes.

Gal

Gal Goldman
  • 8,641
  • 11
  • 45
  • 45
-1

Xerces at http://xerces.apache.org/xerces-c/i pretty full featured, has a C++ interface and produces good error messages, which several other XML parsers don't do so well. Having said that, it's pretty big & I've wound up using my own wrapper round the C parser Expat.

-1

I'm currently using libxml++ for a personal project of mine.

MighMoS
  • 1,228
  • 8
  • 18