您可以:
读取EXCEL文件:
class ExcelReader
{
public List<TestCaseData> ReadExcelData(string excelFile, string sheetname)
{
string cmdText = "SELECT * FROM [" + sheetname+ "$]";
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;
}
您的测试用例将保持如下状态:
[TestFixture]
class TC_1
{
public static IEnumerable<TestCaseData> BudgetData
{
get
{
List<TestCaseData> testCaseDataList = new ExcelReader().ReadExcelData(//path//document.xlsx",
"SheetName");
if (testCaseDataList != null)
foreach (TestCaseData testCaseData in testCaseDataList)
yield return testCaseData;
}
}
[Test]
[TestCaseSource(typeof(TC_1), "BudgetData")]
public void TestCase1(string attribbutte1, string .....)
{
........................................
注意:您需要安装参考“Acces数据库引擎”才能读取Excel文件
注2:默认情况下,第一行为标题,对于每一新行,将执行TC
更改数据时,同一代码被执行5次