RyanSchlomer.com

Sr QA Consultant

Automation for Creating Test Cases in Azure

Posted by:

|

On:

|

,
Azure

I previously discussed how I used ChatGPT for test case creation. The test cases for Azure’s Create Item API were generated by ChatGPT and outputted into an XML structure. That’s where it ended. Although that could still be of benefit in some situations, there is still more automation that can be implemented for creating test cases. My goal from the start was to get ChatGPT to generate the test cases and find an API that can create them in a project management application. I ended up choosing Azure, but I’m sure other tools like Jira or IBM’s Application Lifecycle Management can do the same.

TestCases XML

As mentioned above, in the previous post I had ChatGPT output the API test cases into an XML structure. XML is easy to work with, at least for me. JSON, a text file, or any format can work. I chose XML and will use the System.Xml.Serialization namespace to load my test case file.

I knew I needed a class for XML deserialization, so I asked ChatGPT to create one for me to save on typing. The class is below, except I added the ConvertStepsToAzureFormat() method later.

using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
using System.Text;

namespace AzureTestCases
{
    [XmlRoot("TestCases")]
    public class TestCases
    {
        [XmlElement("TestCase")]
        public List<TestCase> TestCaseList { get; set; }
    }

    public class TestCase
    {
        [XmlElement("Title")]
        public string Title { get; set; }

        [XmlElement("SummaryDescription")]
        public string SummaryDescription { get; set; }

        [XmlElement("Precondition")]
        public string Precondition { get; set; }

        [XmlArray("Steps")]
        [XmlArrayItem("Step")]
        public List<Step> Steps { get; set; }

        public string ConvertStepsToAzureFormat()
        {
            StringBuilder azureSteps = new StringBuilder("<steps id=\"0\" last=\"" + Steps.Count + "\">");

            for (int i = 0; i < Steps.Count; i++)
            {
                azureSteps.Append($"\r\n    <step id=\"{i + 1}\" type=\"ActionStep\">");
                azureSteps.Append($"\r\n        <parameterizedString isformatted=\"true\">{Steps[i].Action}</parameterizedString>");
                azureSteps.Append($"\r\n        <parameterizedString isformatted=\"true\">{Steps[i].ExpectedResult}</parameterizedString>");
                azureSteps.Append("\r\n        <description/>");
                azureSteps.Append("\r\n    </step>");
            }

            azureSteps.Append("\r\n</steps>");
            return azureSteps.ToString();
        }
    }

    public class Step
    {
        [XmlElement("Action")]
        public string Action { get; set; }

        [XmlElement("ExpectedResult")]
        public string ExpectedResult { get; set; }

    }
}

Create Test Case App

I created a console application that reads the test case file and calls Azure’s Create Item API to create the test cases. You will need an Azure account and will need to get a personal access token.

The code loads the test case file and calls the API for each test case in the file. I had to add the await Task.Delay(1000); on line 34 because the API didn’t like the requests coming too fast.

using System;
using System.Text;
using AzureTestCases;
using System.Xml.Serialization;
using Newtonsoft.Json;

class Program
{
    static async Task Main(string[] args)
    {
        string organization = "<organization>";
        string project = "<project>";
        string personalAccessToken = "<personal access token>";
        string workItemType = "Test Case";

        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic",
            Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalAccessToken))));

        string testcaseFile = @"Azure Create Work Item API Test Cases.txt";
        string directory = @"C:\Test\";

        XmlSerializer serializer = new XmlSerializer(typeof(TestCases));
        StreamReader reader = new StreamReader(directory + testcaseFile);
        TestCases testCases = (TestCases)serializer.Deserialize(reader);
        reader.Close();

        foreach (TestCase testCase in testCases.TestCaseList)
        {
            
            await CreateTestCase(testCase, client, organization, project, workItemType);

            // Wait for 1 second; the API doesn't like a blast of requests.
            await Task.Delay(1000);
        }
    }

    static async Task CreateTestCase(TestCase testCase, HttpClient client, string organization, string project, string workItemType)
    {

        var updateObjects = new object[]
        {
            new { op = "add", path = "/fields/System.Title", value = testCase.Title },
            new { op = "add", path = "/fields/System.Description", value = testCase.SummaryDescription },
            new { op = "add", path = "/fields/Microsoft.VSTS.TCM.Steps", value = testCase.ConvertStepsToAzureFormat() }
        };

        var json = JsonConvert.SerializeObject(updateObjects);

        var request = new HttpRequestMessage(HttpMethod.Post, $"https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/${workItemType}?api-version=6.0")
        {
            Content = new StringContent(json, Encoding.UTF8, "application/json-patch+json")
        };

        var response = await client.SendAsync(request);
        var responseBody = await response.Content.ReadAsStringAsync();
    }
}

Azure Work Items

You can see below the six work items that were added to my project in Azure:

Here are the steps added for Test Case 74:

Bottom Line

Can you imagine how much time this can save even if a project had just a few APIs or a small web application with 10 pages? Now think about hundreds of API calls and/or a large web application with hundreds of pages!

Leveraging the power of AI and programmatically creating test cases not only streamlines your testing process but also significantly reduces the manual effort involved in test case creation. This approach frees up your testers who can then focus on testing the application.