Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

visual studio 2012 - MSTest data driven Test set DisplayName for Rows from DataSource

How can I set the DisplayName of a Data Row in data driven tests, where the data source is a XML and the provider is 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>

The Output:

enter image description here

I'd like to display the Environment Name instead of "Data Row 0".

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is it possible with a custom data source attribute. An example can be:

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

and the test method should look like:

        [DataTestMethod]
        [XmlCustomDataSource]
        public void Should_Blur(string environmentName)
        {
            var actualEnvironment = SomeMethodToGetActualEnvironment();
            Assert.AreEqual(environmentName, actualEnvironment);
        }

Finally your test detail will be: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...