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") }
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.
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.
XMLUnit may help you.
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