Search code examples
xmlunit-testinglanguage-agnosticdtd

The best way to validate XML in a unit test?


I have a class with a ToString method that produces XML. I want to unit test it to ensure it is producing valid xml. I have a DTD to validate the XML against.

Should I include the DTD as a string within the unit test to avoid a dependency on it, or is there a smarter way to do this?


Solution

  • If your program validates the XML against the DTD during normal execution, then you should just get the DTD from wherever your program will get it.

    If not and the DTD is extremely short (only a few lines), then storing it as a string in your code is probably okay.

    Otherwise, I'd put it in an external file and have your unit test read it from that file.