C# NUnit 3 数据驱动自 Excel
C# NUnit 3 Data Driven from Excel
您好,我有以下文件:testexcel.xlsx > sheet 1
我想执行此测试两次,因为有 2 行数据。
[Test]
[TestCaseSource("Data")]
public void Login(String username, String password)
{
loginPageModel.DoLogin(username, password);
}
如何将 excel 数据转换成 NUnit 3 official documentation 中解释的这种数据?
static object[] Data = {
new object[] {username, password}
};
您需要读取 Excel 文件(参见 Optimal way to Read an Excel file (.xls/.xlsx)),然后 return 您的 TestCaseSource
中的数据。我不会在这里阅读 Excel 文件,因为链接问题和网络上的许多地方都涵盖了这一点,但您只需要将 TestCaseSource
切换为方法和 yield return
或 Select
结果。
不要忘记您的 TestCaseSource
需要 public static
并且 return TestCaseData
实例更好(但不是必需的)。
您的代码将如下所示。
public static IEnumerable Data()
{
var rows = ReadExcel("testdata.xlsx");
return rows.Select(row => new TestCaseData(row[0], row[1]));
}
我所做的是以下内容并且它正在工作
我有测试:
[Test TestCaseSource(typeof(ExcelDataParser),"BudgetData") Category("1")]
public void AchterBudget(string min, string max)
{
.....
}
classe ExcelDataParser 通过从 class ExcelReader
调用方法 readExcelData() 来读取 excel 文件
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;
}
}
}
这是 class ExcelReader,它包含将 excel 文件中的每一行转换为 TestCaseData 的方法 ReadExcelData:
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;
}
}
如果您打算将测试数据和代码分开。请看JsonSectionReader.
这个包提供了将测试数据存储在嵌入式json文件中的支持,并且对反序列化有很好的支持。
您好,我有以下文件:testexcel.xlsx > sheet 1
我想执行此测试两次,因为有 2 行数据。
[Test]
[TestCaseSource("Data")]
public void Login(String username, String password)
{
loginPageModel.DoLogin(username, password);
}
如何将 excel 数据转换成 NUnit 3 official documentation 中解释的这种数据?
static object[] Data = {
new object[] {username, password}
};
您需要读取 Excel 文件(参见 Optimal way to Read an Excel file (.xls/.xlsx)),然后 return 您的 TestCaseSource
中的数据。我不会在这里阅读 Excel 文件,因为链接问题和网络上的许多地方都涵盖了这一点,但您只需要将 TestCaseSource
切换为方法和 yield return
或 Select
结果。
不要忘记您的 TestCaseSource
需要 public static
并且 return TestCaseData
实例更好(但不是必需的)。
您的代码将如下所示。
public static IEnumerable Data()
{
var rows = ReadExcel("testdata.xlsx");
return rows.Select(row => new TestCaseData(row[0], row[1]));
}
我所做的是以下内容并且它正在工作
我有测试:
[Test TestCaseSource(typeof(ExcelDataParser),"BudgetData") Category("1")]
public void AchterBudget(string min, string max)
{
.....
}
classe ExcelDataParser 通过从 class ExcelReader
调用方法 readExcelData() 来读取 excel 文件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;
}
}
}
这是 class ExcelReader,它包含将 excel 文件中的每一行转换为 TestCaseData 的方法 ReadExcelData:
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;
}
}
如果您打算将测试数据和代码分开。请看JsonSectionReader.
这个包提供了将测试数据存储在嵌入式json文件中的支持,并且对反序列化有很好的支持。