如何在数据驱动的测试中设置数据行的DisplayName,其中数据源是XML,提供者是Microsoft.VisualStudio.TestTools.DataSource.XML。
XML:
<?xml version="1.0" encoding="utf-8" ?>
<environments>
<environment><name>IE</name></environment>
<environment><name>Chrome</name></environment>
</environments>
App Config:
<!-- CONNECTION STRINGS SETTINGS -->
<connectionStrings>
<add name="IE_Chrome" connectionString="IE_Chrome.xml" providerName="Microsoft.VisualStudio.TestTools.DataSource.XML"/>
</connectionStrings>
<!-- PARAMETERIZING TEST SETTINGS -->
<microsoft.visualstudio.testtools>
<dataSources>
<add name="IE_Chrome" connectionString="IE_Chrome" dataTableName="environment" dataAccessMethod="Sequential"/>
</dataSources>
</microsoft.visualstudio.testtools>
输出:
我想显示环境名称而不是“数据行0”。
答案 0 :(得分:1)
是否可以使用自定义数据源属性。一个例子可以是:
public class XmlCustomDataSourceAttribute : Attribute, ITestDataSource
{
public IEnumerable<object[]> GetData(MethodInfo methodInfo)
{
foreach (var environment in YourStaticXmlParser.GetEnvironments())
{
yield return new object[] { environment.Name };
}
}
public string GetDisplayName(MethodInfo methodInfo, object[] data)
{
if (data != null)
{
return string.Format("Target Environmment- {0} ({1})", methodInfo.Name, data[0]);
}
return null;
}
}
并且测试方法应该如下所示:
[DataTestMethod]
[XmlCustomDataSource]
public void Should_Blur(string environmentName)
{
var actualEnvironment = SomeMethodToGetActualEnvironment();
Assert.AreEqual(environmentName, actualEnvironment);
}