//RB: This function allows to add cases for a dedicated project, suite, section public static void AddCases(Dictionary <string, Tuple <int, string> > dict, string path, string testType, object project, object suiteName, object sectionName, string sectionId) { var dictNameSuite = GetSuites(project); var dictNameSections = GetSections(project, dictNameSuite, suiteName.ToString()); List <string> TestRailLists = new List <string>(); RArray = TestExporter.Get1("get_cases/" + AppSettings[project.ToString()] + "&suite_id=" + dictNameSuite[suiteName.ToString()] + "§ion_id=" + dictNameSections[sectionName.ToString()]); if (RArray.Count == 0) { foreach (string key in dict.Keys) { JObject r = TestExporter.CreateTestName("add_case/" + sectionId, key); JToken s = r.First; string[] t = (s.ToString()).Split(':'); } } else { if (RArray.Count() != dict.Count()) { //In this step, add any tests not present in TestRail in case of 429 exception, you can keep adding tests foreach (var item in RArray.Children()) { var obj = JObject.Parse(item.ToString()); var title = (string)obj.SelectToken("title"); TestRailLists.Add(title.Trim()); } foreach (var elem in dict.Keys.ToList()) { if (!TestRailLists.Contains(elem.Trim())) { JObject r = TestExporter.CreateTestName("add_case/" + sectionId, elem); } } } else { Console.WriteLine("Test Cases have been already added"); } } }
//RB: This function is a workaround to delete several tests in a row public static void DeleteTestsWorkAround(Dictionary <string, Tuple <int, string> > testsFromFile, string path, object project, Dictionary <string, string> dictNameSuite, Dictionary <string, string> dictNameSections, object suiteName, object sectionName) { var getCases = TestExporter.Get1("get_cases/" + AppSettings[project.ToString()] + "&suite_id=" + dictNameSuite[suiteName.ToString()] + "§ion_id=" + dictNameSections[sectionName.ToString()]); List <string> ListCasesToDelete = new List <string>(); if (getCases.Count != 0) { // List the tests that are not in the file foreach (var item in getCases.Children()) { var obj = JObject.Parse(item.ToString()); var title = (string)obj.SelectToken("title"); if (!testsFromFile.Keys.Contains(title)) { ListCasesToDelete.Add(title); } } // Create a section TestExporter.CreateSection("add_section/" + AppSettings[project.ToString()], "TestsToBeDeleted"); // Get the section and section Id dictNameSections = GetSections(project, dictNameSuite, suiteName.ToString()); string sectionId = dictNameSections["TestsToBeDeleted"]; // Add the unnecessary tests to the section foreach (var elem in ListCasesToDelete) { JObject r = TestExporter.CreateTestName("add_case/" + sectionId, elem); } // Delete the section TestExporter.Post("delete_section/" + sectionId); } }
//RB: This function below compares the current running tests with the tests currently present in TestRail: // - if the test is not present in TestRail, it will add it, // - if the test is not present in the file, it will delete it from TestRail, // - finally, it will return the correct list of tests cases public static void CompareAndUpdate(Dictionary <string, Tuple <int, string> > testsFromFile, string path, string testType, object project, Dictionary <string, string> dictNameSuite, Dictionary <string, string> dictNameSections, object suiteName, object sectionName) { List <Tuple <string, string> > testRailTestsList = new List <Tuple <string, string> >(); List <Tuple <string, string> > TestcasesWithDuplicates = new List <Tuple <string, string> >(); List <string> fileTestsList = new List <string>(); Dictionary <string, string> difference = new Dictionary <string, string>(); var testsFromTestRails = TestExporter.Get1("get_cases/" + AppSettings[project.ToString()] + "&suite_id=" + dictNameSuite[suiteName.ToString()] + "§ion_id=" + dictNameSections[sectionName.ToString()]); fileTestsList = testsFromFile.Keys.ToList(); //First step, clean the tests that are not in the file foreach (var item in testsFromTestRails.Children()) { var obj = JObject.Parse(item.ToString()); var title = (string)obj.SelectToken("title"); string caseId = (string)obj.SelectToken("id"); testRailTestsList.Add(Tuple.Create(title, caseId)); if (!fileTestsList.Contains(title)) { JObject r = TestExporter.DeleteTestCase("delete_case/" + caseId, caseId); } } //Second step, clean any duplicates in the TestRail section TestcasesWithDuplicates = testRailTestsList.GroupBy(s => s.Item1) .SelectMany(grp => grp.Skip(1)) .ToList(); if (TestcasesWithDuplicates.Count() > 0) { foreach (var dupli in TestcasesWithDuplicates) { JObject r = TestExporter.DeleteTestCase("delete_case/" + dupli.Item2, dupli.Item2); } } testRailTestsList = testRailTestsList.Distinct().ToList(); //RB: This line is currently added as the API tests will only show the failed one if (testType != "API") { //Third step, add any tests not present in TestRail foreach (var elem in fileTestsList) { bool tupleHadProduct = testRailTestsList.Any(m => m.Item1 == elem); if (!tupleHadProduct) { string sectionId = dictNameSections[sectionName.ToString()]; JObject r = TestExporter.CreateTestName("add_case/" + sectionId, elem); } } } else { foreach (var elem in testsFromFile) { bool tupleHadProduct = testRailTestsList.Any(m => m.Item1 == elem.Key); if (elem.Value.Item1 == 5 && !tupleHadProduct) { var testCase = (elem.Key.Length > 250) ? elem.Key.Remove(240) : elem.Key; string sectionId = dictNameSections[sectionName.ToString()]; JObject r = TestExporter.CreateTestName("add_case/" + sectionId, testCase); } } } }