2014-08-28 111 views
5

我的目標是在每個單元測試迭代中都有「嵌套」數據。我想這樣做,以便我可以調用一組數據,以及一系列操作(由字符串描述),然後在我的測試中進行解釋和執行。我目前通過測試資源管理器在VS2013中運行測試,正確使用非嵌套數據(例如無數據/動作子項組)。用於數據驅動單元測試的嵌套XML

例如,我的數據可以是:

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

我想訪問的數據元素作爲每一個正常的非嵌套試驗(dataElements["element"]),然而,我想有動作的元件在一個列表中。我曾嘗試沒有成功如下:

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

GetChildRows似乎是正確的方法,但我無法看到返回的對象類似於我的XML元素的任何數據 - 我只得到一個具有ItemArray 1個DataRow對象3個值(0,{},0)。我該如何找回我的動作元素的列表,以便我可以訪問文本:

  • 「EnterText_LoginName」
  • 「EnterText_Password」
  • 「ClickButton_Login」

回答

0

我有你同樣的問題,我以這種方式解決了。

這是我的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> 

TestMethod的的datsource必須是包含數據和你想讀子節點列表中的節點XML父。 這是測試方法:

[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); 
    } 
}