Friday, May 14, 2004

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:

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

Screenshot

Here's a screenshot that shows have MbUnit loads the XML and creates the different test case.

posted on Friday, May 14, 2004 12:28:00 PM UTC  #    Comments [13]
Tracked by:
"Generic cialis." (Generic cialis.) [Trackback]
"warrantless searches" (online) [Trackback]
"huge turds" (online) [Trackback]
Monday, June 06, 2005 6:03:43 PM UTC
Peli's Blog
Monday, June 06, 2005 6:03:44 PM UTC
Barry Gervin's Software Architecture Perspectives
Monday, June 06, 2005 6:03:45 PM UTC
Peli's Blog
Monday, June 06, 2005 6:03:46 PM UTC
Dynamic Generation of test cases
Mark Levison
Monday, June 06, 2005 6:03:46 PM UTC
Jonathan, this is great. It looks like <a title="MbUnit, Generating Unit Testing and Model Based Testing Framework for .NET Framework" href="http://mbunit.tigris.org" target="_blank">MbUnit</a> is testing each case several times. Can I turn that off?
John Hatton
Monday, June 06, 2005 6:03:47 PM UTC
Actually, it creates a test for each selected XmlNode. So this is the normal behavior.
<br>
<br>If you think you have a bug, it would be easier if you could send a sample or even better a fixture that illustrates it :)
Jonathan de Halleux
Monday, June 06, 2005 6:03:47 PM UTC
Sorry, I'm talking about this sample shown on this page (and in the DataDrivenTests.cs). I must be misunderstanding something; but it appears from the screenshot that there's something combinatorial going on. I see 4*6=24 tests in the screen shot, but only (3 users) * (2 test methods). Make sense?
John Hatton
Monday, June 06, 2005 6:03:48 PM UTC
I'll check that.
Jonathan de Halleux
Monday, June 06, 2005 6:03:49 PM UTC
I see it now. It's becuase the example has //user both in the [XmlDataProvider] and in the [ForEachTest(&quot;//User&quot;)]. So every user is used n*n multiple times.
John Hatton
Monday, June 06, 2005 6:03:49 PM UTC
Andrew Stopford's Weblog
Monday, June 06, 2005 6:03:50 PM UTC
Andrew Stopford's Weblog
Monday, June 06, 2005 6:03:50 PM UTC
Andrew Stopford's Weblog
Monday, June 06, 2005 6:03:51 PM UTC
Andrew Stopford's Weblog
Comments are closed.