栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从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;        }    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/649283.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号