private QT.ServiceResponse <QT.TestCase> UpdateBasicTestCase(long?testCaseId, string precondition) { QT.ServiceResponse <QT.TestCase> result; QT.TestCase existingTestCase = new QT.TestCase(); existingTestCase.Precondition = precondition; existingTestCase.Properties = ConfigurationService.GetSection <QTestDynamicTestCasesSettings>().FieldValues; long projectId = ConfigurationService.GetSection <QTestDynamicTestCasesSettings>().ProjectId; result = _testDesignService.UpdateTestCase(projectId, (long)testCaseId, existingTestCase); return(result); }
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); }