0

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?

1
  • I edited my answer to include a C++ library.
    – bobbymcr
    Oct 21, 2009 at 4:04

4 Answers 4

3

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
  • testing both, validating via XSD seems like a good idea too. (+1)
    – cbrulak
    Oct 21, 2009 at 3:26
  • 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
    Oct 21, 2009 at 3:33
2

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.

1

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.

1
  • +1 for the recommendation of testing lib, know of any that work in C++ and/or GoogleTest?
    – cbrulak
    Oct 21, 2009 at 3:42
1

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.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.