Trying to add a testcase to a particular suiteID in AzureTestPlan
try{
String orgUrl = "https://dev.azure.com/{organization}/{project}";
String personalAccessToken = "{xxx}";
String testPlanId = "2897";
String testSuiteId = "2899";
String workItemTitle = "TestCaseAut";
String workItemDescription = "New case";
String auth = Base64.getEncoder().encodeToString((":" + token).getBytes());
String authHeader = "Basic " + auth;
String createWorkItemUrl = orgUrl + "/_apis/wit/workitems?api-version=6.1-preview.1";
String workItemData = "{\"fields\":{\"Sysytem.Title\": \"" + workItemTitle + "\", \"System.Description\": \"" + workItemDescription + "\", \"System.WorkItemType\": \"Test Case\", \"EAI Code\":\"{xxx}\", \"Area\":\"{path}\", \"Iteration\":\"{path}\"}}";
HttpRequest createWorkItemRequest = HttpRequest.newBuilder()
.uri(URI.create(createWorkItemUrl))
.POST(HttpRequest.BodyPublishers.ofString(workItemData))
.header("Authorization", authHeader)
.header("Content-Type", "application/json")
.build();
HttpClient httpClient = HttpClient.newHttpClient();
HttpResponse<String> createWorkItemResponse = httpClient.send(createWorkItemRequest, HttpResponse.BodyHandlers.ofString());
String workItemId = createWorkItemResponse.body().split(":")[2].split(",")[0];
String addTestCaseUrl = orgUrl + "/_apis/testplan/plans/" + testPlanId + "/suites/" + testSuiteId + "/testcases/add?api-version=6.1-preview.1";
String addTestCaseData = "{\"suiteType\": \"StaticTestSuite\", \"testCaseIds\": [" + workItemId + "]}";
HttpRequest addTestCaseRequest = HttpRequest.newBuilder()
.uri(URI.create(addTestCaseUrl))
.POST(HttpRequest.BodyPublishers.ofString(addTestCaseData))
.header("Authorization", authHeader)
.header("Content-Type", "application/json")
.build();
HttpResponse<String> addTestCaseResponse = httpClient.send(addTestCaseRequest, HttpResponse.BodyHandlers.ofString());
System.out.println("Tc Added");
}
catch(Exception e){
System.out.println("Not created");
}
With the above code getting as Tc Added. But in actual there was no test case added to the particular suiteID in the Azure Testplan.
According to this Test Suites API1 and Test Case API2, You can only add existing
TestCaseIDwhile adding the suite and there's no option to create new TestCase with API.I tried using below code to create
Test Suite:-Output:-
Now, I added one Test Case manually and used it to Create the Test Suite:-
Output:-
You can retrieve the existing
TestCaseIDby using the code below:-Output:-