2016-03-17 50 views
0

我正在通過引用名爲'data.csv'的文件中提供的數據來執行數據驅動的測試。要求是這樣的,即在執行測試方法之後,它應該在這個相同的文件中顯示每個測試方法(或者來自該輸入文件的每一行)的執行結果(即通過/失敗),即'data.csv',通過更新結果這個文件。如何使用c#將codedui測試的結果寫入csv文件?

回答

0

下面的程序會在輸出文件中添加一個新列(命名爲'Result')。

/* 
* OLD data: 
* Column1,Column2,Column3 
* A,B,C 
*/ 

//total number of records should be same 
List<string> newColumnData = new List<string>() { "Pass" }; // This list //can be the output for all the iterations that you have stored in a list 
List<string> lines = File.ReadAllLines("C://test.csv").ToList(); 
//add new column to the header row 
lines[0] += ",Column 4"; 
int index = 1; 
//add new column value for each row. 
lines.Skip(1).ToList().ForEach(line => 
{ 
    //-1 for header 
    lines[index] += "," + newColumnData[index - 1]; 
    index++; 
}); 
//write the new content 
File.WriteAllLines("C://test.csv", lines); 

/* 
* NEW data: 
* Column1,Column2,Column3,Result 
* A,B,C, Pass 
*/ 
0

您可以使用TestContext提供的任何目錄來寫入文件。

TestContext.TestRunResultsDirectory 
TestContext.TestResultsDirectory 
TestContext.TestRunDirectory 

但是,如果你想爲您的測試運行數據/統計我會建議使用MTM及其相關的自動化測試系統,它會跟蹤每一個測試過跑和生產報告的是(餅圖,如)狀態。

0
public controlTypeEntered wpf_Control<controlTypeEntered>(List<wpfControls_PropertyTypes> propertyType, List<String> propertyValue, WpfControl controlWindow) where controlTypeEntered : WpfControl 

    { 
     controlTypeEntered wpfElement = (controlTypeEntered)Activator.CreateInstance(typeof(controlTypeEntered), new object[] { controlWindow }); 
     Hashtable ht = new Hashtable(); 
     for (var i = 0; i < propertyType.Count; i++) 
     { 
      ht.Add(propertyType[i], propertyValue[i]); 
     } 

     for (var i = 0; i < propertyType.Count; i++) 
     { 
      // Identifying the control on basis of its search properties. 
      if (propertyType[i] == wpfControls_PropertyTypes.ClassName) 
      { 
       wpfElement.SearchProperties[WpfControl.PropertyNames.ClassName] = propertyValue[i]; 
      } 
      else if (propertyType[i] == wpfControls_PropertyTypes.ControlType) 
      { 
       wpfElement.SearchProperties[WpfControl.PropertyNames.ControlType] = propertyValue[i]; 
      } 
      else if (propertyType[i] == wpfControls_PropertyTypes.FriendlyName) 
      { 
       wpfElement.SearchProperties[WpfControl.PropertyNames.FriendlyName] = propertyValue[i]; 
      } 
      else if (propertyType[i] == wpfControls_PropertyTypes.Name) 
      { 
       wpfElement.SearchProperties[WpfControl.PropertyNames.Name] = propertyValue[i]; 
      } 
      else if (propertyType[i] == wpfControls_PropertyTypes.TechnologyName) 
      { 
       wpfElement.SearchProperties[WpfControl.PropertyNames.TechnologyName] = propertyValue[i]; 
      } 
     } 
     return (controlTypeEntered)Convert.ChangeType(wpfElement, typeof(controlTypeEntered)); 
    }