Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested XML for Data Driven Unit Test

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:

  • "EnterText_LoginName"
  • "EnterText_Password"
  • "ClickButton_Login"
like image 792
Nashibukasan Avatar asked Nov 11 '22 02:11

Nashibukasan


1 Answers

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);
     }
}
like image 57
jebbo Avatar answered Nov 15 '22 08:11

jebbo