Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 825
Corin Blaikie Avatar asked Sep 09 '08 12:09

Corin Blaikie


People also ask

What should you use to validate XML data?

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.

Can Notepad++ validate XML?

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).

Can we validate XML 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.


1 Answers

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.

like image 181
Eli Courtwright Avatar answered Sep 28 '22 08:09

Eli Courtwright