Test Automation with OpenAI at Neudesic

Jenny Li
Neudesic Innovation
6 min readFeb 8, 2023

I am incredibly excited about the potential that OpenAI brings to the table. With its advanced machine learning capabilities, OpenAI can and WILL revolutionize the way software testing is approached. From automating routine test case generation to helping identify new and creative ways to test software, OpenAI has the potential to streamline the testing process, improve test coverage, and uncover new insights into software quality and functionality.

In this blog, we will explore the different ways in which OpenAI can be integrated into test automation and how it can help improve the efficiency of our testing processes.

1. Data Generation — One of the most important aspects of testing is data generation. This can be a time-consuming process and can lead to inconsistencies if done manually. With OpenAI, it is possible to automate the process of data generation and ensure that the data used in testing is consistent and accurate. OpenAI can be used to generate test data based on a JSON model, ensuring that the data generated is compliant with the required structure.

2. Test Case Generation — Another area where OpenAI can be used to improve test automation is in the generation of test cases. This is particularly important in regression testing, where many test cases need to be executed. With OpenAI, it is possible to automate the generation of test cases, reducing the time and effort required to perform regression testing.

3. Improving Test Coverage — OpenAI can also be used to improve test coverage by analyzing the code and identifying areas that are not covered by tests. This can be particularly useful for identifying edge cases that are not covered by the existing test cases. With OpenAI, it is possible to generate new test cases that cover these edge cases, improving test coverage and ensuring that the application is thoroughly tested.

4. Enhancing Test Automation Frameworks — OpenAI can also be used to enhance existing test automation frameworks. For example, it can be used to optimize test case execution, making the tests run faster and more efficiently. It can also be used to identify and resolve any performance issues that may arise during the testing process.

The use of OpenAI in software testing has the potential to significantly improve the testing process. With its ability to enhance data generation, automate test case creation, and reinforce test automation frameworks, OpenAI has the power to take testing to new levels of efficiency. We are eager to adopt this innovative technology by incorporating OpenAI into our testing methodology today!

Using OpenAI to Automate Test Case Generation and Improve Test Coverage

In the fast-paced world of software development, the ability to test and validate applications quickly and efficiently is more important than ever. As software complexity continues to grow, so too does the need for more advanced testing tools and techniques. OpenAI can be integrated into the test automation process to generate test cases automatically.

This can also help cover a wider range of scenarios and edge cases that may not have been manually considered. Additionally, OpenAI can be used to generate test cases in a variety of formats, such as Gherkin, to support different testing frameworks.

What are the benefits?

Timesaving — Automating the generation of test cases using OpenAI can save a significant amount of time compared to manually writing and executing test cases. This time can then be used for more important tasks such as analyzing results and fixing defects.

Improved Accuracy — OpenAI can analyze code and determine all possible paths that a user may take when using the application. This means that it can generate more comprehensive test cases that cover all possible scenarios, reducing the risk of missed defects.

Cost-effective — Automating the generation of test cases using OpenAI can reduce the cost of testing compared to manual testing. This is because the process is faster, more accurate and less prone to human error.

Here is an example where OpenAI is used to generate test cases in Gherkin format for a mobile app login flow where user must be in a specific AD group, plus field validation and error messages.

BDD Test Cases Produced by Chat Bot

You can copy the generated test cases into your UI test automation suite’s feature file and start automating it. Utilizing OpenAI’s API to generate the test cases and integrating them into your test plan through an ADO API endpoint can streamline the process further. Although the generated scenarios and test cases may require refinement or additional details specific to the mobile app being tested, automating this process saves time, effort, and cost while leading to improved quality and quicker release cycles.

Integrate OpenAI into the Test Automation Framework

OpenAI’s GPT-3 endpoint is not publicly accessible and requires an API key to access. Here’s a general example of using OpenAI API to generate test data, checking against the schema in the model.json file. New data is generated only if the model changes, and the model.json file gets updated. This can be a useful tool to improve your Load & Performance automation framework, with access to the API.

const request = require('request');
const fs = require('fs');
const SwaggerParser = require('swagger-parser');

// OpenAI API endpoint and API key
const openaiEndpoint = 'https://api.openai.com/v1/engines/model-name/jobs';
const openaiApiKey = 'your-api-key-here';

// Read model.json file and parse the content
let modelData = JSON.parse(fs.readFileSync('model.json', 'utf8'));

// Compare the current schema in the model.json file with the latest Swagger URL
SwaggerParser.dereference(modelData.swaggerUrl).then(schema => {
if (JSON.stringify(schema) !== JSON.stringify(modelData.schema)) {
// If the schema has changed, generate new test data using OpenAI API
generateTestData(schema, modelData.modelName);
}
});

// Function to generate test data using OpenAI API
function generateTestData(schema, modelName) {
let options = {
url: openaiEndpoint,
headers: {
'Authorization': 'Bearer ' + openaiApiKey,
'Content-Type': 'application/json'
},
json: {
'model': modelName,
'prompt': 'Generate test data based on Swagger schema',
'temperature': 0.5,
'max_tokens': 1000,
'top_p': 1,
'n': 1,
'stop': '\n',
'presence_penalty': 0
}
};

request.post(options, (error, response, body) => {
if (!error && response.statusCode === 200) {
let testData = body.choices[0].text;
// Write the generated test data to a csv file
fs.writeFileSync('test_data.csv', testData, 'utf8');
console.log('Test data generated successfully!');
// Update the model.json file with the latest schema
modelData.schema = schema;
fs.writeFileSync('model.json', JSON.stringify(modelData), 'utf8');
} else {
console.error('Failed to generate test data: ' + error);
}
});
}

Some “gotchas” to be aware of when using OpenAI APIs:

Cost — OpenAI APIs are not free and can be expensive, especially for large-scale usage. It’s important to understand the pricing model and budget accordingly. Come up with cost reduction approaches that are applicable to you. Such as: optimize API usage; use API key with rate limites; use caching; monitor usage; consider alternative APIs.

Data Privacy and Security — OpenAI processes sensitive data and it’s important to ensure that your data is secure and protected from unauthorized access.

API Limitations — OpenAI APIs are still evolving and may have limitations such as API rate limits, limited data access, and compatibility issues. This tool is fairly new still, it may work, may not but I strangely recommend everyone in the testing space to explore its potentials.

Accuracy and Reliability — OpenAI APIs are still in their early stages and the accuracy and reliability of their outputs may not always be 100%.

Technical Complexity — Integrating OpenAI APIs into your systems and applications may require technical expertise and resources, which can be time-consuming and challenging.

Legal and Ethical Considerations — There may be legal and ethical considerations when using OpenAI APIs, such as data protection and intellectual property rights.

It’s important to thoroughly evaluate these factors before implementing OpenAI APIs in your systems and applications.

The field of OpenAI is still in its early stages, and as it gains more widespread use and adoption, it is expected to grow and evolve. While it may not work for everyone, I highly recommend those in the software testing industry to explore its potential benefits.

--

--