Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing XML Generation [closed]

What unit testing strategies do people recommend for testing xml is being generated correctly.

The my current tests seem abit primitive, something along the lines of:

[Test] public void pseudo_test() {    XmlDocument myDOC = new XmlDocument();    mydoc = _task.MyMethodToMakeXMLDoc();     Assert.AreEqual(myDoc.OuterXML(),"big string of XML") } 
like image 892
Dan Avatar asked Feb 02 '09 16:02

Dan


People also ask

What is XML in testing?

XML documents are used for data interchange between different applications, web application create (X)HTML output or respond to AJAX requests using little XML snippets. There are many use cases where XML is generated and the outputs have to be tested as much as any other part of the application.


2 Answers

First, as pretty much everyone is saying, validate the XML if there's a schema defined for it. (If there's not, define one.)

But you can build tests that are a lot more granular than that by executing XPath queries against the document, e.g.:

string xml="Your xml string here" ; XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); path = "/doc/element1[@id='key1']/element2[. = 'value2']"; Assert.IsTrue(doc.SelectSingleNode(path) != null); 

This lets you test not only whether or not your document is semantically valid, but whether or not the method producing it is populating it with the values that you expect.

like image 104
Robert Rossney Avatar answered Oct 06 '22 04:10

Robert Rossney


XMLUnit may help you.

like image 40
Ionuț G. Stan Avatar answered Oct 06 '22 04:10

Ionuț G. Stan