My aim is to have 'nested' data in each of my unit test Iterations. I want to do this so I can have a set of Data to call upon, as well as a list of Actions (described by strings) that are then interpreted and performed in my tests. I currently have tests running in VS2013 via Test Explorer utilising non-nested data (eg. no Data/Actions sub item groups) correctly.
For example, my data could be:
<TestData>
<Iteration>
<Data>
<LoginName>admin</LoginName>
<Password>admin</Password>
</Data>
<Actions>
<Action>EnterText_LoginName</Action>
<Action>EnterText_Password</Action>
<Action>ClickButton_Login</Action>
</Actions>
</Iteration>
</TestData>
I would like to access the elements in Data as per a normal non-nested test (dataElements["element"]
), however, I would like to have the Actions elements in a list. I have tried the following with no success:
var data = TestContext.DataRow.GetChildRows("Iteration_Data");
var actions = TestContext.DataRow.GetChildRows("Iteration_Actions");
GetChildRows seems the correct method, but I am unable to see any data in the returned object that resembles my XML elements - I only get 1 DataRow
object that has an ItemArray
of 3 values (0, {}, 0). How do I retrieve a list of my Action elements so I can access the text:
I had your same problem and i resolved in this way.
This is my XML
<?xml version="1.0" encoding="utf-8" ?>
<root>
<parent>
<field1>1234</field1>
<field2>4700</field2>
<child>
<name>john</name>
<age>2</age>
</child>
<child>
<name>jack</name>
<age>3</age>
</child>
</parent>
</root>
The datsource of TestMethod must be the node XML parent that contains data and the list of node children that you want to read. This is the test method:
[TestMethod]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
"App_Data\\TestsInput\\Controllers\\Identity\\Tests\\Test.xml",
"parent",
DataAccessMethod.Sequential)]
public void MyFirstTest()
{
//get a normal node XML
int field1= Convert.ToInt32(TestContext.DataRow["field1"]);
//get the list of fields
DataRow[] datas = TestContext.DataRow.GetChildRows("parent_child");
foreach (DataRow data in datas)
{
string name= data["name"].ToString();
int age= Convert.ToInt32(data["age"]);
//example
Assert.IsTrue(age==2);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With