简体   繁体   中英

Can I Easily Make DataTable data for Unit Tests?

Thank you for reading. I want to unit test some functions which is used Datatable as input parameter.

But, in program, DataTable was created by Database. So, i want to make sample DataTable Data for unit test.

But, it is hard. because, there is 20 columns. So, when i typing all the columns and each row, i feel it is really waste time.

Can I Easily Make DataTable data for Unit Tests? for example, in debugging mode, there is a datatable with data. i want to use feature like "make datatable data as code" button. Then, datatable is created at that moment.

--------------add------------ when i debug program, there is a datatable created by real-database. And i want to make unit test using sample data which is from real-database. So, i typing all the columns and each row which is from debugging mode. But, my datatable is consist of 20 columns, So it is hard to enter all data. There is any other way to do it?

I have two solutions for this task, you can choose one that is more suitable for you.

public class Person
{
public string Name { get; set; }
public int Rank { get; set; }
}

 public static DataTable getTestTableFromList()
{
    List<Person> list = new List<Person> { new Person { Name = "Bob", Rank = 5 }, new Person { Name = "Jack", Rank = 7 } };
    DataTable result = convertListToDT(list);
    return result;
}
private static DataTable convertListToDT<T>(IEnumerable<T> listToConvert)
{
    Type type = typeof(T);
    var tpProp = type.GetProperties();

    DataTable resultTable = new DataTable();
    foreach (PropertyInfo pi in tpProp)
    {
        resultTable.Columns.Add(new DataColumn(pi.Name, pi.PropertyType));
    }

    foreach (T listRow in listToConvert)
    {
        object[] obArr = new object[tpProp.Length];
        for (int i = 0; i < tpProp.Length; i++)
        {
            obArr[i] = tpProp[i].GetValue(listRow, null);
        }

        resultTable.Rows.Add(obArr);
    }

    return resultTable;
}

public static DataTable getTestTableFromCsv()
{
List<Type> typesList = new List<Type>() { typeof(int), typeof(string), typeof(string), typeof(float), typeof(DateTime) };
string csv =
@"0,'Chocolate','Cake',8.95,'1927-07-11'
1,'Lemon','Cake',8.95,'1998-11-12'
2,'Casino','Cake',15.95,'2011-01-15'
3,'Opera','Cake',15.95,'2014-12-08'
4,'Strawberry','Cake',11.95,'2015-09-18'
5,'Truffle','Cake',15.95,'1979-08-03'
6,'Chocolate','Eclair',3.25,'1973-07-05'
7,'Coffee','Eclair',3.5,'1991-05-07'
8,'Vanilla','Eclair',3.25,'1989-03-11'
9,'Napoleon','Cake',13.49,'1977-01-13'
10,'Almond','Tart',3.75,'1979-04-17'
11,'Apple','Pie',5.25,'1981-02-19'";
string[] csvArr = csv.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
DataTable result = new DataTable();
for (int ic = 0; ic < typesList.Count; ic++)
{
    DataColumn iColumn = new DataColumn("Column_" + ic.ToString(), typesList[ic]);
    result.Columns.Add(iColumn);
}
for (int ir = 0; ir < csvArr.Length; ir++)
{
    DataRow row = result.NewRow();
    string[] values = csvArr[ir].Split(',');
    for (int col = 0; col < result.Columns.Count; col++)
    {
        Type tp = result.Columns[col].DataType;
        if (tp == typeof(int))
        {
            row[col] = int.Parse(values[col]);
        }
        if (tp == typeof(string))
        {
            row[col] = values[col].Replace("'", "").Trim();
        }
        if (tp == typeof(float))
        {
            string str = values[col];                        
            row[col] = float.Parse(str,CultureInfo.InvariantCulture);
        }
        if (tp == typeof(DateTime))
        {
            string str = values[col].Replace("'","").Trim();
            DateTime curDate = DateTime.Parse(str, System.Globalization.CultureInfo.InvariantCulture);
            row[col] = curDate;
        }
        
    }
    result.Rows.Add(row);
}
return result;
}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM