我所做的是以下内容,它正在工作
我有测试:
[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; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)