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)); }
public DTC.TestCase InitTestCase(TestCasesContext testCasesContext) { QT.ServiceResponse <QT.TestCase> result = default; QT.TestCase qTestCreatedTestCase = new QT.TestCase(); QT.TestCase qTestExistingTestCase = testCasesContext.TestCase != null?ServicesCollection.Current.Resolve <QT.TestCase>() : null; if (string.IsNullOrEmpty(testCasesContext.SuiteId)) { throw new ArgumentException("You must specify a qTest SuiteId."); } // Generate the identification token placed in the beginning of the description. string formattedAutomationId = $"#QE-{testCasesContext.TestCaseId}"; if (!string.IsNullOrEmpty(testCasesContext.TestCaseDescription)) { testCasesContext.TestCaseDescription = $"{formattedAutomationId}-{testCasesContext.TestFullName} \r\n{testCasesContext.TestCaseDescription}"; } else { testCasesContext.TestCaseDescription = $"{formattedAutomationId}-{testCasesContext.TestFullName} \r\n"; } long projectId = ConfigurationService.GetSection <QTestDynamicTestCasesSettings>().ProjectId; if (!string.IsNullOrEmpty(testCasesContext.TestCaseId)) { // Check if a test case with this ID exists in qTest if (qTestExistingTestCase == null) { qTestExistingTestCase = _testDesignService.ListTestCase(projectId, testCasesContext.SuiteId.ToLong(), expandSteps: true).Data?.FindLast(tcase => tcase.Description.Trim().StartsWith(formattedAutomationId)); } if (qTestExistingTestCase != null) { // Existing test case was found. Update the test cases if a collection is provided if (testCasesContext.TestSteps != null && testCasesContext.TestSteps.Any()) { QT.TestCase updateCase = new QT.TestCase(); bool shouldUpdate = false; // Compare the existing test cases and the New ones using Serialization. Update if they are different. if (testCasesContext.TestSteps.Select(x => x.Description).Stringify() != qTestExistingTestCase.TestSteps.Select(x => x.Description).Stringify()) { updateCase.TestSteps = new List <QT.TestStep>(); foreach (var testStep in testCasesContext.TestSteps) { string description = IsValidJson(testStep.Description) ? testStep.Description : JsonConvert.ToString(testStep.Description).Trim('"'); string expected = IsValidJson(testStep.Expected) ? testStep.Expected : JsonConvert.ToString(testStep.Expected).Trim('"'); updateCase.TestSteps.Add(new QT.TestStep() { Description = description, Expected = expected }); } shouldUpdate = true; } // Set precondition if (testCasesContext.Precondition != qTestExistingTestCase.Precondition) { updateCase.Precondition = testCasesContext.Precondition; shouldUpdate = true; } // Update using the SDK if diffs are found if (shouldUpdate) { result = _testDesignService.UpdateTestCase(projectId, qTestExistingTestCase.Id, updateCase); } } qTestCreatedTestCase = qTestExistingTestCase; } else { // Create brand-new test case // The specific here is that we need to create a very basic test case, and then update it with the details QT.TestCase testCase = new QT.TestCase() { Name = testCasesContext.TestCaseName, ParentId = testCasesContext.SuiteId.ToLong(), Description = testCasesContext.TestCaseDescription, }; // Call SDK testDesign service result = _testDesignService.CreateTestCase(projectId, testCase); if (result.Data != null) { result = UpdateBasicTestCase(result.Data.Id, testCasesContext.Precondition); } } } if (result != null && !result.IsSuccess) { throw new InvalidOperationException(string.Format("Cannnot create test case [{0}] - {1}", result.Error.Code, result.Error.Message)); } else if (result != null) { qTestCreatedTestCase = result.Data; } // Set requirement relation once the test case has been created if (!string.IsNullOrEmpty(testCasesContext.RequirementId)) { IList <long> testCaseList = new List <long> { qTestCreatedTestCase.Id }; var requirementResponse = _projectService.LinkTestCaseRequirement(projectId, new QT.LinkTestCaseRequirement() { RequirementId = testCasesContext.RequirementId.ToLong(), TestCases = testCaseList }); if (requirementResponse != null && !requirementResponse.IsSuccess) { throw new InvalidOperationException($"Cannnot link test case {qTestCreatedTestCase.Id} to requirement {testCasesContext.RequirementId}. {requirementResponse.Error}"); } } ServicesCollection.Current.RegisterInstance(qTestCreatedTestCase); TestCase createdTestCase = new TestCase(testCasesContext.TestCaseId, qTestCreatedTestCase.Name, qTestCreatedTestCase.Description, qTestCreatedTestCase.Precondition); createdTestCase.TestSteps = testCasesContext.TestSteps; return(createdTestCase); }