Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top