Beispiel #1
0
        //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()] + "&section_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);
                    }
                }
            }
        }