Ejemplo n.º 1
0
        public AzureTestCase CreatTestCase(AzureTestCase testCase)
        {
            var uri = new Uri(_uri);

            var credentials   = new VssBasicCredential(string.Empty, _personalAccessToken);
            var patchDocument = new JsonPatchDocument();

            patchDocument.Add(new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path      = "/fields/System.Title",
                Value     = testCase.Title,
            });

            patchDocument.Add(new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path      = "/fields/Microsoft.VSTS.Common.Priority",
                Value     = testCase.Priority,
            });

            if (!string.IsNullOrEmpty(testCase.AreaPath))
            {
                patchDocument.Add(new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path      = "/fields/System.AreaPath",
                    Value     = testCase.AreaPath,
                });
            }

            if (!string.IsNullOrEmpty(testCase.IterationPath))
            {
                patchDocument.Add(new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path      = "/fields/System.IterationPath",
                    Value     = testCase.IterationPath,
                });
            }

            if (!string.IsNullOrEmpty(testCase.Description))
            {
                patchDocument.Add(new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path      = "/fields/System.Description",
                    Value     = testCase.Description,
                });
            }

            if (!string.IsNullOrEmpty(TestStepsService.GenerateTestStepsHtml(testCase.TestSteps)) || !string.IsNullOrEmpty(testCase.TestStepsHtml))
            {
                if (testCase.TestSteps.Any())
                {
                    patchDocument.Add(new JsonPatchOperation()
                    {
                        Operation = Operation.Add,
                        Path      = "/fields/Microsoft.VSTS.TCM.Steps",
                        Value     = TestStepsService.GenerateTestStepsHtml(testCase.TestSteps),
                    });
                }
                else
                {
                    patchDocument.Add(new JsonPatchOperation()
                    {
                        Operation = Operation.Add,
                        Path      = "/fields/Microsoft.VSTS.TCM.Steps",
                        Value     = testCase.TestStepsHtml,
                    });
                }
            }

            patchDocument.Add(new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path      = "/fields/Microsoft.VSTS.TCM.AutomatedTestName",
                Value     = testCase.AutomatedTestName,
            });

            patchDocument.Add(new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path      = "/fields/Microsoft.VSTS.TCM.AutomatedTestStorage",
                Value     = testCase.AutomatedTestStorage,
            });

            if (!string.IsNullOrEmpty(testCase.RequirementUrl))
            {
                patchDocument.Add(
                    new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path      = "/relations/-",
                    Value     = new
                    {
                        rel        = "System.LinkTypes.Hierarchy-Reverse",
                        url        = testCase.RequirementUrl,
                        attributes = new Dictionary <string, object>()
                        {
                            { "isLocked", false },
                            { "name", "Parent" },
                            { "comment", "added automatically" },
                        },
                    },
                });
            }

            VssConnection connection = new VssConnection(uri, credentials);
            WorkItemTrackingHttpClient workItemTrackingHttpClient = connection.GetClient <WorkItemTrackingHttpClient>();

            try
            {
                WorkItem result = workItemTrackingHttpClient.CreateWorkItemAsync(patchDocument, _project, "Test Case").Result;
                return(ConvertWorkItemToAzureTestCase(result));
            }
            catch (AggregateException ex)
            {
                Debug.WriteLine("Error creating test case: {0}", ex.InnerException.Message);
                return(null);
            }
        }
        public TestCase InitTestCase(TestCasesContext testCasesContext)
        {
            var createdTestCase  = new AzureTestCase();
            var existingTestCase = testCasesContext.TestCase != null?ServicesCollection.Current.Resolve <AzureTestCase>() : null;

            string requirementUrl = string.Empty;

            if (!string.IsNullOrEmpty(testCasesContext.RequirementId))
            {
                requirementUrl = AzureQueryExecutor.GetWorkItems(testCasesContext.RequirementId).First().Url;
            }

            // If it is an existing manual test case- just set the associated automation and the relate the requirements.
            if (testCasesContext.TestCaseId != null)
            {
                existingTestCase = _azureTestCasesService.AssociateAutomationToExistingTestCase(testCasesContext.TestCaseId, testCasesContext.TestFullName, testCasesContext.TestProjectName, requirementUrl);

                return(ConvertAzureTestCaseToTestCase(existingTestCase));
            }

            if (existingTestCase == null)
            {
                existingTestCase = _azureTestCasesService.FindTestCasesByAssociatedAutomation(testCasesContext.TestFullName, testCasesContext.TestProjectName).FirstOrDefault();
            }

            if (existingTestCase != null)
            {
                bool shouldUpdate = false;

                // Existing test case was found. Update the test cases if a collection is provided
                if (testCasesContext.TestSteps != null && testCasesContext.TestSteps.Any())
                {
                    // Compare the existing test cases and the New ones using Serialization. Update if they are different.
                    if (TestStepsService.GenerateTestStepsHtml(testCasesContext.TestSteps) != existingTestCase.TestStepsHtml)
                    {
                        existingTestCase.TestStepsHtml = TestStepsService.GenerateTestStepsHtml(testCasesContext.TestSteps);
                        shouldUpdate = true;
                    }
                }

                if (testCasesContext.TestCaseName != existingTestCase.Title)
                {
                    existingTestCase.Title = testCasesContext.TestCaseName;

                    shouldUpdate = true;
                }

                if (requirementUrl != existingTestCase.RequirementUrl)
                {
                    existingTestCase.RequirementUrl = requirementUrl;

                    shouldUpdate = true;
                }

                if (testCasesContext.TestCaseDescription != existingTestCase.Description ||
                    (string.IsNullOrEmpty(testCasesContext.TestCaseDescription) && !string.IsNullOrEmpty(testCasesContext.Precondition)))
                {
                    if (!string.IsNullOrEmpty(testCasesContext.Precondition))
                    {
                        existingTestCase.Description = $"{testCasesContext.TestCaseDescription}{Environment.NewLine}Precondition:{Environment.NewLine}{testCasesContext.Precondition}";
                    }
                    else if (!string.IsNullOrEmpty(existingTestCase.Description))
                    {
                        existingTestCase.Description = testCasesContext.TestCaseDescription;
                    }

                    shouldUpdate = true;
                }

                if (shouldUpdate)
                {
                    existingTestCase = _azureTestCasesService.UpdateTestCase(existingTestCase);
                }

                createdTestCase = existingTestCase;
            }
            else
            {
                var testCase = new AzureTestCase()
                {
                    Title                = testCasesContext.TestCaseName,
                    Description          = testCasesContext.TestCaseDescription,
                    AutomatedTestName    = testCasesContext.TestFullName,
                    AutomatedTestStorage = testCasesContext.TestProjectName,
                    TestStepsHtml        = TestStepsService.GenerateTestStepsHtml(testCasesContext.TestSteps),
                    RequirementUrl       = requirementUrl,
                    AreaPath             = testCasesContext.GetAdditionalPropertyByKey("AreaPath"),
                    IterationPath        = testCasesContext.GetAdditionalPropertyByKey("IterationPath"),
                    Priority             = testCasesContext.GetAdditionalPropertyByKey("Priority"),
                };

                if (!string.IsNullOrEmpty(testCasesContext.Precondition))
                {
                    testCase.Description = $"{testCasesContext.TestCaseDescription}{Environment.NewLine}Precondition:{Environment.NewLine}{testCasesContext.Precondition}";
                }

                createdTestCase = _azureTestCasesService.CreatTestCase(testCase);
            }

            ServicesCollection.Current.RegisterInstance(createdTestCase);

            return(ConvertAzureTestCaseToTestCase(createdTestCase));
        }