2017-05-30 33 views
1
驅動

你好我有以下文件:testexcel.xlsx>片1C#NUnit的3個數據從Excel

enter image description here

我想,因爲有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} 
    }; 

回答

2

我做了如下,它的工作

我有測試:

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

的CLASSE ExcelDataParser通過從類調用方法readExcelData()讀取Excel文件ExcelReader

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

這是ExcelReader類,其中包含的方法od 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

您需要在Excel文件讀取(見Optimal way to Read an Excel file (.xls/.xlsx)),然後從你的TestCaseSource返回數據。我不會在這裏閱讀Excel文件,因爲這在鏈接問題和網絡上的許多地方都有涉及,但您只需將您的TestCaseSource切換爲方法並且yield returnSelect結果即可。

不要忘記,你的TestCaseSource需求爲public static,這是更好的(但不要求)返回TestCaseData實例。

你的代碼看起來像這樣。

public static IEnumerable Data() 
{ 
    var rows = ReadExcel("testdata.xlsx"); 
    return rows.Select(row => new TestCaseData(row[0], row[1])); 
} 
+0

謝謝。但在「返回rows.select .....」我面臨以下錯誤:「不能將lambda表達式轉換爲類型'字符串',因爲它不是委託類型」。我已經包括「使用System.Linq;」和「System.Data.Entity;」在其他帖子上閱讀...任何線索? – joudaon