Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

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">


    <Question text ="What is the capital of Texas?" point="10">
        <Choice text="Houston" isCorrect="false" />
        <Choice text="Dallas" isCorrect="false"/>
        <Choice text="Austin" isCorrect="true"/>       

    <Question text="What is the fastest animal on earth?" point ="10">
        <Choice text="Turtle" isCorrect="false" />
        <Choice text="Deer" isCorrect ="false" />
        <Choice text="Cheetah" isCorrect="true" />

    <Question text="China exists in which continent?" point="10">
        <Choice text="South America" isCorrect="false" />
        <Choice text="Asia" isCorrect="true" />
        <Choice text="Africa" isCorrect="false" />       


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?

Posted on Monday, December 17, 2007 7:52 AM | Back to top

Comments on this post: How do you test XML documents?

# re: How do you test XML documents?
Requesting Gravatar...
I would test an XML file in just two ways.

First, I would test that the minimum requirements for the XML are satisfied. Does every question have one and only one correct answer? Are all questions not null and not an empty string? Are all answers (correct or incorrect) not null and not an empty string? These tests largely depend on how the XML file is used.

Second, I would have what I call value tests, where I test that specific values are there. In the example you provided, making sure that three specific values are correct may be sufficient.

On a separate note, I have in the past serialized expected results from services and embedded these results in my NUnit project, and then tested my actual results against my expected results to see if anything has changed... It turns out to be a pretty neat framework for testing when results are pretty complex objects...

Thanks for writing your blog, I find it very useful,

Jonathan Starr

Left by Jonathan Starr on Dec 21, 2007 8:25 AM

# re: How do you test XML documents?
Requesting Gravatar...

i have test bank in xml format i can not use it in any text format like word or pdf
how i can access this files in any readable
format like word or pfd formast

i f i cvan send you the files please tell me

Left by elsafty on Dec 15, 2010 9:40 AM

Your comment:
 (will show your gravatar)

Copyright © Mohammad Azam | Powered by: