This page last changed on May 04, 2007 by grahamhay.
[DataFixture]
 [XmlDataProvider("../../sample.xml","//User")]
 [XmlDataProvider("../../sample.xml","DataFixture/Customers")]
 public class DataDrivenTests
 {
     [ForEachTest("User")]
     public void ForEachTest(XmlNode node)
     {
         Assert.IsNotNull(node);
         Assert.AreEqual("User",node.Name);
         Console.WriteLine(node.OuterXml);
     }


     [ForEachTest("User", DataType = typeof(User))]
     public void ForEachTestWithSerialization(User user)
     {
         Assert.IsNotNull(user);
         Console.WriteLine(user.ToString());
     }
 }

The file sample.xml looks like this:

<DataFixture>
   <Employees>
     <User Name="Mickey" LastName="Mouse" />
   </Employees>
   <Customers>
     <User Name="Jonathan" LastName="de Halleux" />
     <User Name="Voldo" LastName="Unkown" />
   </Customers>
 </DataFixture>

and the User class like this:

[XmlRoot("User")]
 public class User
 {
     private string name;
     private string lastName;
     public User()
     {}
     [XmlAttribute("Name")]
     public String Name
     {
     get{ return this.name;}
     set{ this.name = value;}
     }
     [XmlAttribute("LastName")]
     public String LastName
     {
     get{ return this.lastName;}
     set{ this.lastName = value;}
     }
 }

How does it work ?

The XmlDataProvider attributes are used to specify an XML filename that contains the data (of course, you can put several of those).

Then a first XPath expression is applied to the data.

Each selected node is then feeded to the ForEachTest which applies a second XPath expression on the node. This allows a fine grained selection of the data.

You can also specify the desired output data type. XmlSerializer is then used to convert XmlNode into the desired object.

Document generated by Confluence on Jun 11, 2007 11:56