从Excel驱动的C#NUnit 3数据 硒

从Excel驱动的C#NUnit 3数据 硒,第1张

从Excel驱动的C#NUnit 3数据 硒

我所做的是以下内容,它正在工作

我有测试:

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

通过调用类ExcelReader中的readExcelData()方法来读取Excel文件的类ExcelDataParser

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.resourcesexcelData";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;         }     }    }

这是ExcelReader类,其中包含方法ReadExcelData,该方法将每行从excel文件转换为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;        }    }


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5651477.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存