private TestCase ConvertAzureTestCaseToTestCase(AzureTestCase azureTestCase)
        {
            // NOTE: Since in Azure DevOps test steps are used as HTML we don't convert them back.
            var testCase = new TestCase(azureTestCase.Id.ToString(), azureTestCase.Title, azureTestCase.Description, string.Empty);

            return(testCase);
        }
Exemplo n.º 2
0
        private AzureTestCase ConvertWorkItemToAzureTestCase(WorkItem workItem)
        {
            var result = new AzureTestCase();

            if (workItem.Fields.ContainsKey("System.AreaPath"))
            {
                result.AreaPath = workItem.Fields["System.AreaPath"].ToString();
            }

            if (workItem.Fields.ContainsKey("System.IterationPath"))
            {
                result.IterationPath = workItem.Fields["System.IterationPath"].ToString();
            }

            if (workItem.Fields.ContainsKey("System.Title"))
            {
                result.Title = workItem.Fields["System.Title"].ToString();
            }

            if (workItem.Fields.ContainsKey("System.Description"))
            {
                result.Description = workItem.Fields["System.Description"].ToString();
            }

            if (workItem.Fields.ContainsKey("Microsoft.VSTS.Common.Priority"))
            {
                result.Priority = workItem.Fields["Microsoft.VSTS.Common.Priority"].ToString();
            }

            if (workItem.Fields.ContainsKey("Microsoft.VSTS.TCM.AutomatedTestName"))
            {
                result.AutomatedTestName = workItem.Fields["Microsoft.VSTS.TCM.AutomatedTestName"].ToString();
            }

            if (workItem.Fields.ContainsKey("Microsoft.VSTS.TCM.AutomatedTestStorage"))
            {
                result.AutomatedTestStorage = workItem.Fields["Microsoft.VSTS.TCM.AutomatedTestStorage"].ToString();
            }

            if (workItem.Fields.ContainsKey("Microsoft.VSTS.TCM.Steps"))
            {
                result.TestStepsHtml = workItem.Fields["Microsoft.VSTS.TCM.Steps"].ToString();
            }

            if (workItem.Id != null)
            {
                result.Id = (int)workItem.Id;
            }

            if (workItem.Relations != null && workItem.Relations.Any())
            {
                result.RequirementUrl = workItem.Relations.FirstOrDefault()?.Url;
            }

            return(result);
        }
Exemplo n.º 3
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));
        }