How to add a testcase to a particular Plan ID & suiteID through restassured or from Postman in AzureTestPlan

52 Views Asked by At

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.

1

There are 1 best solutions below

0
SiddheshDesai On

According to this Test Suites API1 and Test Case API2, You can only add existing TestCaseID while adding the suite and there's no option to create new TestCase with API.

I tried using below code to create Test Suite:-

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Base64;

public class test {
    public static void main(String[] args) {
        try {
            String orgUrl = "https://dev.azure.com/ORGNAME/ProjName";
            String personalAccessToken = "xxxxxxxxxxxxxxjh7q";
            String planId = "29416";
            String suiteId = "29417";
            String workItemType = "TestSuite";
            String workItemTitle = "NewTestSuite";
            String workItemDescription = "Description";

            String token = personalAccessToken;
            String auth = Base64.getEncoder().encodeToString((":" + token).getBytes());
            String authHeader = "Basic " + auth;

            // Create Test Suite
            String createSuiteUrl = orgUrl + "/_apis/test/Plans/" + planId + "/suites/" + suiteId + "?api-version=5.0";
            String createSuiteData = "{\"suiteType\":\"StaticTestSuite\",\"name\":\"" + workItemTitle + "\",\"parentId\":" + planId + "}";
            HttpRequest createSuiteRequest = HttpRequest.newBuilder()
                    .uri(URI.create(createSuiteUrl))
                    .POST(HttpRequest.BodyPublishers.ofString(createSuiteData))
                    .header("Authorization", authHeader)
                    .header("Content-Type", "application/json")
                    .build();

            HttpClient httpClient = HttpClient.newHttpClient();
            HttpResponse<String> createSuiteResponse = httpClient.send(createSuiteRequest, HttpResponse.BodyHandlers.ofString());

            if (createSuiteResponse.statusCode() == 200) {
                System.out.println("Test suite created successfully.");
            } else {
                System.out.println("Error creating test suite: " + createSuiteResponse.body());
                return;
            }

            // Parse the response to get the suite ID if needed
            String suiteIdFromResponse = "Parse from createSuiteResponse";

            // Create Work Item
            String createWorkItemUrl = orgUrl + "/_apis/wit/workitems/" + workItemType + "?api-version=7.1-preview.3";
            String createWorkItemData = "{\"fields\":{\"System.Title\":\"" + workItemTitle + "\",\"System.Description\":\"" + workItemDescription + "\",\"System.AreaPath\":\"{project}\",\"System.IterationPath\":\"{project}\"}}";
            HttpRequest createWorkItemRequest = HttpRequest.newBuilder()
                    .uri(URI.create(createWorkItemUrl))
                    .POST(HttpRequest.BodyPublishers.ofString(createWorkItemData))
                    .header("Authorization", authHeader)
                    .header("Content-Type", "application/json")
                    .build();

            HttpResponse<String> createWorkItemResponse = httpClient.send(createWorkItemRequest, HttpResponse.BodyHandlers.ofString());

            if (createWorkItemResponse.statusCode() == 200) {
                System.out.println("Work item created successfully.");
            } else {
                System.out.println("Error creating work item: " + createWorkItemResponse.body());
            }

        } catch (Exception e) {
            System.out.println("Exception occurred: " + e.getMessage());
        }
    }
}

Output:-

enter image description here

Now, I added one Test Case manually and used it to Create the Test Suite:-

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Base64;

public class AzureDevopsTest {
    public static void main(String[] args) {
        try {
            String orgUrl = "https://dev.azure.com/ORGNAME/PROJNAME";
            String personalAccessToken = "uxxxxxxxxxgsbhijh7q";
            String planId = "29416";
            String suiteId = "29424";
            String testCaseId = "29426"; // ID of the existing test case
            String workItemType = "TestSuite2";
            String workItemTitle = "NewTestSuite2";
            String workItemDescription = "Description";

            String token = personalAccessToken;
            String auth = Base64.getEncoder().encodeToString((":" + token).getBytes());
            String authHeader = "Basic " + auth;

            // Create Test Suite
            String createSuiteUrl = orgUrl + "/_apis/test/Plans/" + planId + "/suites/" + suiteId + "?api-version=5.0";
            String createSuiteData = "{\"suiteType\":\"StaticTestSuite\",\"name\":\"" + workItemTitle + "\",\"parentId\":" + planId + "}";
            HttpRequest createSuiteRequest = HttpRequest.newBuilder()
                    .uri(URI.create(createSuiteUrl))
                    .POST(HttpRequest.BodyPublishers.ofString(createSuiteData))
                    .header("Authorization", authHeader)
                    .header("Content-Type", "application/json")
                    .build();

            HttpClient httpClient = HttpClient.newHttpClient();
            HttpResponse<String> createSuiteResponse = httpClient.send(createSuiteRequest, HttpResponse.BodyHandlers.ofString());

            if (createSuiteResponse.statusCode() == 200) {
                System.out.println("Test suite created successfully.");
            } else {
                System.out.println("Error creating test suite: " + createSuiteResponse.body());
                return;
            }

            // Create Work Item (Test Case)
            String createWorkItemUrl = orgUrl + "/_apis/wit/workitems?api-version=7.1-preview.3";
            String createWorkItemData = "{\"fields\":{\"System.Title\":\"" + workItemTitle + "\",\"System.Description\":\"" + workItemDescription + "\",\"System.WorkItemType\":\"Test Case\"}}";
            HttpRequest createWorkItemRequest = HttpRequest.newBuilder()
                    .uri(URI.create(createWorkItemUrl))
                    .POST(HttpRequest.BodyPublishers.ofString(createWorkItemData))
                    .header("Authorization", authHeader)
                    .header("Content-Type", "application/json")
                    .build();

            HttpResponse<String> createWorkItemResponse = httpClient.send(createWorkItemRequest, HttpResponse.BodyHandlers.ofString());

            if (createWorkItemResponse.statusCode() == 200) {
                System.out.println("Work item (test case) created successfully.");
                // Parse the response to get the work item ID
                String workItemId = "Parse from createWorkItemResponse";

                // Add Test Case to Test Suite
                String addTestCaseUrl = orgUrl + "/_apis/test/Plans/" + planId + "/suites/" + suiteId + "/testcases/" + testCaseId + "?api-version=5.0";
                HttpRequest addTestCaseRequest = HttpRequest.newBuilder()
                        .uri(URI.create(addTestCaseUrl))
                        .POST(HttpRequest.BodyPublishers.noBody())
                        .header("Authorization", authHeader)
                        .header("Content-Type", "application/json")
                        .build();

                HttpResponse<String> addTestCaseResponse = httpClient.send(addTestCaseRequest, HttpResponse.BodyHandlers.ofString());

                if (addTestCaseResponse.statusCode() == 204) {
                    System.out.println("Test case added to test suite successfully.");
                } else {
                    System.out.println("Error adding test case to test suite: " + addTestCaseResponse.body());
                }
            } else {
                System.out.println("Error creating work item (test case): " + createWorkItemResponse.body());
            }

        } catch (Exception e) {
            System.out.println("Exception occurred: " + e.getMessage());
        }
    }
}

Output:-

enter image description here

You can retrieve the existing TestCaseID by using the code below:-

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Base64;

public class AzureDevOpsTest3 {
    public static void main(String[] args) {
        try {
            String organization = "ORGNAME"; // Update with your organization name
            String project = "PROJNAME"; // Update with your project name
            String planId = "29416"; // Update with your planId
            String suiteId = "29424"; // Update with your suiteId
            String personalAccessToken = "uvlywjxxxxxxxxxxxxcgsbhijh7q"; // Update with your personal access token
            String apiVersion = "5.0";

            String apiUrl = String.format("https://dev.azure.com/%s/%s/_apis/test/Plans/%s/suites/%s/testcases?api-version=%s",
                    organization, project, planId, suiteId, apiVersion);

            String token = personalAccessToken;
            String auth = Base64.getEncoder().encodeToString((token + ":").getBytes());
            String authHeader = "Basic " + auth;

            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(apiUrl))
                    .header("Authorization", authHeader)
                    .build();

            HttpClient client = HttpClient.newHttpClient();
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

            if (response.statusCode() == 200) {
                System.out.println("Response: " + response.body());
            } else {
                System.out.println("Error: " + response.statusCode() + " - " + response.body());
            }

        } catch (Exception e) {
            System.out.println("Exception occurred: " + e.getMessage());
        }
    }
}

Output:-

enter image description here