加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

用于数据驱动单元测试的嵌套XML

发布时间:2020-12-16 23:29:23 所属栏目:百科 来源:网络整理
导读:我的目标是在每个单元测试迭代中都有“嵌套”数据.我想这样做,所以我可以有一组要调用的数据,以及一系列动作(由字符串描述),然后在我的测试中解释和执行.我目前通过测试资源管理器在VS2013中运行测试,使用非嵌套数据(例如,无数据/操作子项组). 例如,我的数据
我的目标是在每个单元测试迭代中都有“嵌套”数据.我想这样做,所以我可以有一组要调用的数据,以及一系列动作(由字符串描述),然后在我的测试中解释和执行.我目前通过测试资源管理器在VS2013中运行测试,使用非嵌套数据(例如,无数据/操作子项组).

例如,我的数据可能是:

<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”])访问Data中的元素,但是,我想在列表中包含Actions元素.我试过以下但没有成功:

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

GetChildRows似乎是正确的方法,但我无法在返回的对象中看到任何类似于我的XML元素的数据 – 我只得到1个DataRow对象,其ItemArray为3个值(0,{},0).如何检索我的Action元素列表,以便我可以访问该文本:

>“EnterText_LoginName”
>“EnterText_Password”
>“ClickButton_Login”

解决方法

我有同样的问题,我以这种方式解决了.

这是我的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_DataTestsInputControllersIdentityTestsTest.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);
     }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读