Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using List<string> type as DataRow Parameter

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?

like image 240
Shamim Hafiz - MSFT Avatar asked Nov 08 '18 00:11

Shamim Hafiz - MSFT


People also ask

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

Is it possible to pass a property as parameter in DataRow? Thanks. Short answer: NO!!!!. Attributes need constants values. This appears to be an XY problem. What is the ultimate goal you are trying to achieve?

What is a datarow type?

This C# tutorial shows the DataRow type from the System.Data namespace. It covers getting, adding and removing rows. DataRow represents a row in a DataTable. It is introduced by the System.Data namespace. It is added or removed from DataTables.

How to test multiple input options via datarows in unit tests?

If we had a method that accepted a complex type like a DateTime we can fudge our unit test to support testing multiple input options via DataRows by using one of valid attribute input options and massaging it to the correct type in the arrange section of our test.

What is the use of datarow field in C?

In complex programs, the Field extension can make life easier. DataRow Field. Summary. We used DataRow in C# programs. This is a powerful collection in System.Data that will greatly help your program and keep its code understandable. The DataRow is used in almost every program that also uses the DataTable type.


2 Answers

I found another cool way to do it with arrays! :)

The DataRow in MS Test allows you to pass params parameters. These will pass into the array in the test method signature.

    [TestMethod]
    [DataRow(true, "")]
    [DataRow(true, "", "")]
    [DataRow(true, "", "", "0")]
    [DataRow(true, "", "", "", "1")]
    [DataRow(true, "", "1", "", "0", "")]
    [DataRow(false, "1")]
    [DataRow(false, "", "1")]
    [DataRow(false, "", "", "1")]
    [DataRow(false, "", "", "1", "1")]
    [DataRow(false, "1", "1", "1", "1")]
    public void IsSparseRow(bool expected, params string[] row)
    {
        // Arrange

        // Act
        var actual = ExcelFileHelpers.IsSparseRow(row);

        // Assert
        Assert.AreEqual(expected, actual);
    }
like image 148
Jess Avatar answered Oct 12 '22 07:10

Jess


As the error message mentions, you cannot use Lists 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)
{
    // ...  
}
like image 40
dee-see Avatar answered Oct 12 '22 08:10

dee-see