Exemplo n.º 1
0
        public List<TestCaseTypeModel> GetCaseTypes()
        {
            // Get case types from TestRail
            var caseTypesJson = (JArray)SendRequest(_testRailApiClient.SendGet, "get_case_types", null);

            // Format case types list
            var caseTypes = new List<TestCaseTypeModel>();
            foreach (var caseTypeJson in caseTypesJson)
            {
                var caseType = new TestCaseTypeModel
                {
                    Id = (int)caseTypeJson["id"],
                    Name = (string)caseTypeJson["name"],
                    IsDefault = (bool)caseTypeJson["is_default"],
                };

                caseTypes.Add(caseType);
            }

            return caseTypes;
        }
        public List <TestCaseModel> GetTestsFromNunit3File(string xmlFilePath, List <TestCaseTypeModel> testCaseTypes)
        {
            var testCases = new List <TestCaseModel>();

            // Load the XML file
            var doc = new XmlDocument();

            doc.Load(xmlFilePath);

            // Get test fixtures
            var testFixtureNodes = doc.SelectNodes("//test-suite[@type='TestFixture']");

            foreach (XmlNode testFixtureNode in testFixtureNodes)
            {
                // Get test fixture name
                var fixtureDesc       = testFixtureNode.SelectSingleNode("./properties/property[@name='Description']")?.Attributes["value"]?.InnerText;
                var fixtureName       = testFixtureNode.Attributes["name"]?.InnerText;
                var testFixtureName   = fixtureDesc ?? fixtureName;
                var fixtureFullName   = testFixtureNode.Attributes["fullname"]?.InnerText.Replace($".{fixtureName}", "");
                var fixtureCategories = GetTestCaseCategories(testFixtureNode);

                // Get test cases (non-parameterized)
                var testCaseNodes = testFixtureNode.SelectNodes("./test-case");
                foreach (XmlNode testCaseNode in testCaseNodes)
                {
                    // Get categories
                    var tcCategories = GetTestCaseCategories(testCaseNode);
                    tcCategories.AddRange(fixtureCategories);

                    // Create model and add to list
                    var testCase = new TestCaseModel
                    {
                        FixtureName     = testFixtureName,
                        Title           = GetTestCaseName(testCaseNode),
                        Tags            = string.Join(",", tcCategories),
                        FixtureFullName = fixtureFullName,
                        TestResult      = GetTestResult(testCaseNode)
                    };
                    testCases.Add(testCase);
                }

                // Get test cases (parameterized)
                var testSuiteParameterizedNodes = testFixtureNode.SelectNodes("./test-suite[@type='ParameterizedMethod']");
                foreach (XmlNode testSuiteParameterizedNode in testSuiteParameterizedNodes)
                {
                    // Get categories
                    var tcpCategories = GetTestCaseCategories(testSuiteParameterizedNode);
                    tcpCategories.AddRange(fixtureCategories);

                    // Iterate through parameterized test cases
                    var testSuiteParameterizedTestCases = testSuiteParameterizedNode.SelectNodes("./test-case");
                    foreach (XmlNode testSuiteParameterizedTestCase in testSuiteParameterizedTestCases)
                    {
                        var testCaseParameterized = new TestCaseModel
                        {
                            FixtureName     = testFixtureName,
                            Title           = GetTestCaseName(testSuiteParameterizedTestCase),
                            Tags            = string.Join(",", tcpCategories),
                            FixtureFullName = fixtureFullName,
                            TestResult      = GetTestResult(testSuiteParameterizedTestCase)
                        };
                        testCases.Add(testCaseParameterized);
                    }
                }
            }

            // Final formatting for test cases
            foreach (var t in testCases)
            {
                // Sort the tags
                var tags = t.Tags.Split(',').OrderBy(tag => tag).ToList();
                t.Tags = string.Join(",", tags);

                // Make sure Title doesn't exceed max length in TestRail
                if (t.Title.Length > 250)
                {
                    t.Title = t.Title.Substring(0, 250);
                }

                // Case Type
                TestCaseTypeModel associatedCaseType = null;
                foreach (var tag in tags)
                {
                    associatedCaseType = testCaseTypes.Find(x => x.Name.ToLower().Equals(tag));
                    if (associatedCaseType != null)
                    {
                        break;
                    }
                }
                associatedCaseType = associatedCaseType ?? testCaseTypes.First(x => x.IsDefault);
                t.TypeId           = associatedCaseType.Id;

                // Is Automated
                t.IsAutomated = tags.Contains("automated");
            }

            return(testCases);
        }