Search code examples
c#excelseleniumnunitoledb

C# NUnit 3 Data Driven from Excel


Hello I have the following file: testexcel.xlsx > sheet 1

enter image description here

I want to execute this test twice as there are 2 rows with data.

[Test]
[TestCaseSource("Data")]
public void Login(String username, String password)
{
    loginPageModel.DoLogin(username, password);
}

How can I convert that excel data into this kind of data as explained in NUnit 3 official documentation?

static object[] Data = {
        new object[] {username, password}
    };

Solution

  • What i did is the following and it's working

    I have the Test:

    [Test TestCaseSource(typeof(ExcelDataParser),"BudgetData") Category("1")]
    public void AchterBudget(string min, string max)
    {
    .....
    }
    

    The classe ExcelDataParser which reads the excel file by calling the method readExcelData() from the class ExcelReader

    class ExcelDataParser
    {
    static string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
    static string actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
    static string projectPath = new Uri(actualPath).LocalPath;
    static string excelPath = projectPath + @"com.seloger.resources\excelData\";
    
    public static IEnumerable<TestCaseData> BudgetData
    {
      get
       {
          List<TestCaseData> testCaseDataList = new ExcelReader().ReadExcelData(excelPath + "AcheterBudgetData.xlsx");
    
    if (testCaseDataList != null)
       foreach (TestCaseData testCaseData in testCaseDataList)
                                    yield return testCaseData;
                        }
                    }
        }
    

    And this is the class ExcelReader which contains the method ReadExcelData that converts every row from the excel file to a TestCaseData:

    class ExcelReader
        {
            public List<TestCaseData> ReadExcelData(string excelFile, string cmdText = "SELECT * FROM [Feuil1$]")
            {
                if (!File.Exists(excelFile))
                    throw new Exception(string.Format("File name: {0}", excelFile), new FileNotFoundException());
                string connectionStr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES\";", excelFile);
                var ret = new List<TestCaseData>();
                using (var connection = new OleDbConnection(connectionStr))
                {
                    connection.Open();
                    var command = new OleDbCommand(cmdText, connection);
                    var reader = command.ExecuteReader();
                    if (reader == null)
                        throw new Exception(string.Format("No data return from file, file name:{0}", excelFile));
                    while (reader.Read())
                    {
                        var row = new List<string>();
                        var feildCnt = reader.FieldCount;
                        for (var i = 0; i < feildCnt; i++)
                            row.Add(reader.GetValue(i).ToString());
                        ret.Add(new TestCaseData(row.ToArray()));
                    }
                }
                return ret;
            }
        }