How can we pass a List<string>
to a DataRow
parameter in [DataTestMethod]
I am trying something like:
[DataTestMethod]
[DataRow(new List<string>() {"Iteam1"})]
[TestCategory(TestCategories.UnitTest)]
public void MyTest(IEnumerable<string> myStrings)
{
// ...
}
I am getting a compile error:
An attribute argument must be a constant expression, typeof expression
or array creation expression of an attribute parameter type
Is it even possible to pass List like this?
Best Answer
As the error message mentions, you cannot use
List
s in attributes but you can use arrays.To really use a
List
or any other type you can useDynamicDataAttribute
.The method or property given to the
DynamicDataAttribute
must return anIEnumerable
of object arrays. These object arrays represent the parameters to be passed to your test method.If you always have a fixed number of items in your list you can avoid using lists altogether