views:

59

answers:

4

I'm writing a couple of classes that generate xml file. (Details probably not important at the moment).

I wondering the best testing strategy is.

  • I don't want to re-write the xml generation code just to compare the output, when I could write the file to disk and compare it at certain milestones (the xml spec won't change often, like once or twice every couple of years)
  • I'm more interested in testing the behaviour of the architecture instead of the getters & setters

Options that come to mind:

  • rebuilding the xml file in the testing environment and comparing the string representations
  • manually checking the result (writing to file, etc)
  • rebuilding the xml file in memory in the testing environment and comparing the in-memory elements.

Virtual Bonus if you know any libraries for C++ and/or Google Test.

Ideas?

+3  A: 

Have you considered using XSD's and validating your XML to the XSD? You didn't mention if it was content or structure you were testing for (probably both).
If it validates, it will test the structure of the XML will conform to the required structure.

Russell
testing both, validating via XSD seems like a good idea too. (+1)
cbrulak
I must agree here with XSD validation. You were "supposed" to have the XSD anyway, so just validate the XML using the XSD. If you don't have already, it would certainly save you time.
Eduardo
+1  A: 

You might consider an XML diff tool. There is a free one available on MSDN: XML Diff and Patch Tool.

I see you are looking for C++ tools. In that case, libxmldiff might be more suitable.

bobbymcr
can that be added to the automated build process easily?
cbrulak
Yes, it contains a thin wrapper EXE "XmlDiff.exe"; some info here: http://my.safaribooksonline.com/0596003978/netxml-CHP-13-SECT-2
bobbymcr
+1  A: 

You might consider using XML Unit: http://xmlunit.sourceforge.net. It provides JUnit extension classes which can be used to assert equality of XML files.

sateesh
+1 for the recommendation of testing lib, know of any that work in C++ and/or GoogleTest?
cbrulak
+2  A: 

In the past I've approached this two ways:

Compare the xml file against the result stored as a string in the test file. This is easy to implement, and unless you are wanting to generate variations of the xml file for testing purposes, the string comparison method works fine.

In the case where you have a xml file writer and reader, you can compare the original with the round trip result.

I agree with you that you shouldn't replicate the logic to generate the file in the test function, just for the purpose of testing. Also, I would try to avoid the need to write to the file system -- this is unecessary dependence on the file system, and would probably result in slower running tests.

Phillip Ngan

related questions