Unit Testing Frameworks – Validating XML Documents

schemaunit testingxml

From http://www.w3.org/XML/Schema:

[XML Schemas] provide a means for defining the structure, content and semantics of XML documents.

I'm using an XML Schema (XSD) to validate several large XML documents. While I'm finding plenty of support within XSD for checking the structure of my documents, there are no procedural if/else features that allow me to say, for instance,

If Country is USA, then Zipcode cannot be empty.

I'm comfortable using unit testing frameworks, and could quite happily use a framework to test content integrity.

Am I asking for trouble doing it this way, rather than an alternative approach? Has anybody tried this with good / bad results?

Edit: I didn't include this information to keep it technology agnostic, but I would be using C# / Linq / xUnit for deserialization / testing.

Best Answer

I use C# to deserialize XML documents into classes and unit test from there. I do this in addition to verifying the XML document against the XSD file, and it works great. I highly recommend this method of validating XML.

...I would be using C#...

Look into XMLUnit for .NET

Related Topic