Nested XML for Unit Test data management

My goal is to have โ€œnestedโ€ data in each of my unit test Iterations. I want to do this so that I have a dataset to call, as well as a list of actions (described by strings) that are then interpreted and executed in my tests. Currently, I have tests running in VS2013 using Test Explorer using incorrectly nested data (e.g. groups of data / action items).

For example, my data might 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 the Data according to the usual non-nested test ( dataElements["element"] ), however I would like to have Actions elements in the list. I tried the following without success:

 var data = TestContext.DataRow.GetChildRows("Iteration_Data"); var actions = TestContext.DataRow.GetChildRows("Iteration_Actions"); 

GetChildRows seems like the right method, but I canโ€™t see any data in the returned object that looks like my XML elements - I get only 1 DataRow object that has an ItemArray of 3 values โ€‹โ€‹(0, {}, 0). How to get a list of my Action elements so that I can access the text:

  • "EnterText_LoginName"
  • "EnterText_Password"
  • "ClickButton_Login"
+5
source share
1 answer

I had the same problem and solved it 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 TestMethod data source must be the parent of an XML node that contains the data and a list of node children that you want to read. This is a testing 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); } } 
0
source

Source: https://habr.com/ru/post/1201302/


All Articles