问题描述
我需要编写一个由包含数据集合的数据文件驱动的单元测试.出于明显的原因,csv文件不合适,但是XML建议使用它.但是我似乎无法使其正常工作.
I need to write a unit test driven by a data file that contains collections of data. For obvious reasons a csv file isn't appropriate, but XML suggests itself. However I can't seem to get it to work.
这是数据文件的一部分(仅显示一个测试记录):
Here's a fragment of the data file (only one test record shown):
<?xml version="1.0" encoding="utf-8" ?>
<testData>
<testRecord>
<displayColumnNames>
<name>Include</name>
<name>SampleInt1</name>
<name>SampleInt2</name>
<name>SampleInt3</name>
<name>SampleInt4</name>
<name>SampleInt5</name>
</displayColumnNames>
<valueColIDs>
<valueColID>1</valueColID>
<valueColID>3</valueColID>
<valueColID>5</valueColID>
</valueColIDs>
<calculations>
<calculation colID ="2"><![CDATA[500 * [:5]]></calculation>
<calculation colID ="5"><![CDATA[500 * [:2]]></calculation>
</calculations>
<expected>
<item>5</item>
</expected>
</testRecord>
</testData>
基本上,我希望能够从每个测试记录(例如"displayColumnNames")中读取大量数据集合,并遍历测试中的值.每个这样的集合中元素的数量将随测试案例的不同而变化.
Basically I expected to be able to read in a number of data collections from each test record (e.g. "displayColumnNames") and iterate over the values in my test. The number of elements in each such collection will vary from test case to test case.
但是,立即遇到的绊脚石是我的代码行:
However the immediate stumbling block is my code line:
var displayColumnNames = TestContext.DataRow["displayColumnNames"];
会引发测试时错误列'displayColumnNames'不属于表testRecord.".
which throws a test-time error "Column 'displayColumnNames' does not belong to table testRecord.".
我可以使它与简单的扁平" xml数据文件很好地协作,但这对我没有帮助.我是在尝试VS2008(即MSTest)无法处理的事情,还是在做愚蠢的事情?
I can get this to work nicely with simple 'flat' xml data files, but that doesn't help me. Am I attempting something that VS2008 (ie MSTest) cannot handle, or am I doing something stupid?
推荐答案
您可以使用
TestContext.DataRow.GetChildRows("testRecord_displayColumnNames")
访问嵌套节点.关系名称-"testRecord_displayColumnNames"可以修改为更深入地进入xml层次结构. HTH
to access the nested nodes. The relation name - "testRecord_displayColumnNames" can be modified to go deeper into the xml hierarchy. HTH
这篇关于使用嵌套的xml数据文件进行数据驱动的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!