How can I pass property as parameter in MSTest DataRow

匆匆过客 提交于 2019-12-04 05:12:16

问题


I have a DataTestMethod in my unit test using MSTest, and I have a string property in my constructor, let's say I have this code for example:

[DataTestMethod]
[DataRow("test")] 
[DataRow(stringproperty)] // How is this?

public void TestMethod(string test) {
    Assert.AreEqual("test", test);
}

Is it possible to pass a property as parameter in DataRow?

Thanks.


回答1:


Yes if you use the DynamicDataAttribute

[DynamicData("TestMethodInput")]
[DataTestMethod]
public void TestMethod(string test)
{
    Assert.AreEqual("test", test);
}

public static IEnumerable<object[]> TestMethodInput
{
    get
    {
        return new[]
        {
            new object[] { stringproperty },
            new object[] { "test" }
        };
    }
}



回答2:


Try this

private List<string> testData=new List<string>();
testData.Add("test");
testData.Add("test1");

private IEnumerable<object[]> ListOfTestData =>
    new List<string[]> 
    {
        new[] {testData[0]},
        new[] {testData[1]}
    };

[DataTestMethod]
[DynamicData (nameof (ListOfTestData))]
public void TestMethod(string test) 
{
    Assert.AreEqual("test", test);
}


来源:https://stackoverflow.com/questions/47298140/how-can-i-pass-property-as-parameter-in-mstest-datarow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!