testing strategies: generating a XML file

207 views Asked by At

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?

4

There are 4 answers

2
Russell On BEST ANSWER

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.

2
bobbymcr On

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.

1
sateesh On

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.

0
Phillip Ngan On

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.