By XML documents I mean predefined XML documents which has to be in certain format. Take a look at the following XML file.
<Exam title="Exam 1">
<Questions>
<Question text ="What is the capital of Texas?" point="10">
<Choices>
<Choice text="Houston" isCorrect="false" />
<Choice text="Dallas" isCorrect="false"/>
<Choice text="Austin" isCorrect="true"/>
</Choices>
</Question>
<Question text="What is the fastest animal on earth?" point ="10">
<Choices>
<Choice text="Turtle" isCorrect="false" />
<Choice text="Deer" isCorrect ="false" />
<Choice text="Cheetah" isCorrect="true" />
</Choices>
</Question>
<Question text="China exists in which continent?" point="10">
<Choices>
<Choice text="South America" isCorrect="false" />
<Choice text="Asia" isCorrect="true" />
<Choice text="Africa" isCorrect="false" />
</Choices>
</Question>
</Questions>
</Exam>
This is a very simple XML file which contains the Exam. Let's say that we are writing an application that generates this XML file. How would be test that the application is working as expected. In my opinion one way is to write a solid XML Schema and run the file against that schema. You can also write individual tests for each test case but I think that is overkill. The XSD tool is great for generating XML schema from an XML file but it too is not very efficient and leave out the details.
Here is one of the methods if you want to write unit tests for XML file using C# code.
public void TestExamFileHaveTheExamElement()
{
XDocument doc = XDocument.Load(_fileName);
Assert.IsTrue(doc.Element("Exam") != null);
}
You can guess that if you follow this path then you will be writing lot of tests.
What do you think? How would you test your XML file?