2017-06-05 84 views
0

免責聲明:我是一位在工作中學習的相對編碼新手。發佈SpecFlow結果到TestRail

我已經設置了一個使用Cucumber和C#Selenium的Specflow項目並下載了TestRail API。我遵循現有示例將測試結果發佈到方案末尾的靜態測試導軌ID。

{ Gurock.TestRail.APIClient client = new Gurock.TestRail.APIClient(「https://testrail.placeholder.com/testrail」); client.User =「[email protected]」; //將用戶的電子郵件放在這裏 client.Password =「password」; //把你的用戶的密碼,這裏

  Dictionary<string, object> testResult = new Dictionary<string, object>(); 
     if (null != ScenarioContext.Current.TestError) 
     { 
      testResult["status_id"] = "5"; //failed; 
      testResult["comment"] = ScenarioContext.Current.TestError.ToString(); 
     } 
     else 
     { 
      testResult["status_id"] = "1"; //passed 
     } 
      client.SendPost("add_result_for_case/:run_id/:case_id"); //Here I am using a hardcoded test id.} 

我可以在上面的代碼鏈接到方案通過使用基於場景標籤,例如如果一個

if (ScenarioContext.Current.ScenarioInfo.Tags.Contains("case_id")) 

但問題是,我將不得不復制上面的代碼爲每個方案,每次都有一個獨特的IF語句和標記。我想要的是一種使發帖準確無誤的方式,這樣我只需要一段代碼就可以將每個場景的結果發送到正確的靜態TestRail ID。

回答

0

我會以CaseID標籤爲前綴,以便您可以將它們與普通標籤區分開來。
讓我們從TC_說,讓你的標籤被命名爲喜歡TC_1,TC_42,...

爲了讓現在的測試用例ID,你必須要找到與TC_在ScenarioContext.Current.ScenarioInfo.Tags

開始一個條目該代碼,這可能是這樣的:

var tags = ScenarioContext.Current.ScenarioInfo.Tags; 
var testCaseIds = tags 
        .Where(i => i.StartsWith("TC_")) //get all entries that start with TC_ 
        .Select(i => i.Substring(3)); //get only the part after TC_ 
        .ToList(); 

現在你有一個測試用例ID的列表,可以傳遞到TestRails API。