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?
Perform validation by using an XSD schema Build and run the application to validate the XML document by using the XSD schema. The application should report that the XML document is valid.
Notepad++ in combination with the XML Tools Plugin and the XidML schema is a smart way to validate XidML files (basically any XML file where you have a schema).
XML documents are validated by the Create method of the XmlReader class. To validate an XML document, construct an XmlReaderSettings object that contains an XML schema definition language (XSD) schema with which to validate the XML document.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With