Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass property as parameter in MSTest DataRow

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 Answers

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" }
        };
    }
}
like image 125
ovolo Avatar answered Dec 20 '25 21:12

ovolo