Assembly: MbUnit (in MbUnit.dll) Version: 3.3.0.0 (3.3.610.0)
Syntax
| C# |
|---|
public class XmlDataAttribute : ContentAttribute |
| Visual Basic (Declaration) |
|---|
Public Class XmlDataAttribute _ Inherits ContentAttribute |
Remarks
An XML data set selects nodes from an XML document using XPath expressions. The selected nodes are returned as XPathNavigator objects but MbUnit will automatically convert the values to the types requested by the test such as strings or ints.
Two XPath expressions are used.
- Item Path : An XPath expression that selects a set of nodes that are used to uniquely identify records. For example, the item path might be used to select the containing element of each Book element in an XML document of Books. The item path is specified in the constructor.
- Binding Path : An XPath expression that selects a node relative to the item path that contains a particular data value of interest. For example, the binding path might be used to select the Author attribute of a Book element in an XML document of Books. The binding path is specified by the DataBinding.
Examples
The XML may contain metadata at the row level by adding metadata elements in the
http://www.gallio.org/ namespace containing metadata entries. In the following example,
the row values would be selected using an Item Path of "//row" and a Binding Path of "@value".
Additionally, some rows would have metadata as specified.
CopyC#<data>
<row value="somevalue">
<metadata xmlns="http://www.gallio.org/">
<entry key="Description" value="A row..." />
<entry key="Author" value="Me" />
</metadata>
</row>
<row value="anothervalue" />
</data>
This example reads data from an Embedded Resource called Data.xml within the same namespace as the test fixture. Notice that the binding xpath expressions are specified for each parameter using the BindAttribute attribute.
Data files:
CopyC#<shoppingCart>
<item name="Bananas">
<unitPrice>0.85</unitPrice>
<quantity>3</quantity>
</item>
<item name="Cookies">
<unitPrice>0.10</unitPrice>
<quantity>10</quantity>
</item>
<-- Comment: mmm! -->
<item name="Shortbread">
<unitPrice>2.25</unitPrice>
<quantity>1</quantity>
</item>
</shoppingCart>
A simple test.
CopyC#public class AccountingTests
{
[Test]
[XmlData("//item", ResourcePath = "Data.xml")]
public void ShoppingCartTotalWithSingleItem(
[Bind("@name")] string item,
[Bind("unitPrice")] decimal unitPrice,
[Bind("quantity")] decimal quantity)
{
var shoppingCart = new ShoppingCart();
shoppingCart.Add(item, unitprice, quantity);
Assert.AreEqual(unitPrice * quantity, shoppingCart.TotalCost);
}
}
Inheritance Hierarchy
System..::.Attribute
Gallio.Framework.Pattern..::.PatternAttribute
Gallio.Framework.Pattern..::.DecoratorPatternAttribute
Gallio.Framework.Pattern..::.DataPatternAttribute
MbUnit.Framework..::.ContentAttribute
MbUnit.Framework..::.XmlDataAttribute
