Here's a preview of a new fixture that will be available in the next release of MbUnit. The fixtures loads XML data from files and feeds it to the different methods, hence it's Data Driven Unit Testing. I'll make a real article on this when the fixture stabilizes but here's a first example.
[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:
sample.xml
<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:
User
[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;} } }
XmlDataProvider
ForEachTest
XmlSerializer
XmlNode
Here's a screenshot that shows have MbUnit loads the XML and creates the different test case.
Page rendered at Thursday, December 04, 2008 8:50:03 AM UTC
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.