Using List<string> type as DataRow Parameter
As the error message mentions, you cannot use List
s in attributes but you can use arrays.
[DataTestMethod]
[DataRow(new string[] { "Item1" })]
[TestCategory(TestCategories.UnitTest)]
public void MyTest(string[] myStrings)
{
// ...
}
To really use a List
or any other type you can use DynamicDataAttribute
.
[DataTestMethod]
[DynamicData(nameof(GetTestData), DynamicDataSourceType.Method)]
[TestCategory(TestCategories.UnitTest)]
public void MyTest(IEnumerable<string> myStrings)
{
// ...
}
public static IEnumerable<object[]> GetTestData()
{
yield return new object[] { new List<string>() { "Item1" } };
}
The method or property given to the DynamicDataAttribute
must return an IEnumerable
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
[DataTestMethod]
[DataRow("Item1", "Item2")]
[TestCategory(TestCategories.UnitTest)]
public void MyTest(string string1, string string2)
{
// ...
}