Generative AI data driven testing, a magical example
AI can either be a boon or a curse. Depends on how we handle it. PC: https://www.monkeyuser.com/

Generative AI data driven testing, a magical example

The wizarding world is undergoing a recession, so they looked upon the muggle world for work. Now the closest thing to magic in the muggle world is AI, so our friends from Hogwarts sought Google Gemini AI's advice to consider where they fit in and made a list. You can watch it below, do watch it till the end 😉.

Accurate? Debatable. Fun? Hell yeah!

There are 4 important tools and their libraries which made this article possible, it's going to be a long read so brace yourself.

1. Google AI Studio:

The google AI studio uses Gemini AI models to generate responses. Gemini might infamously be known to suggest drinking something unusual to avoid kidney stones, but its resilient. While other AI tools have closed their daily quota of free API requests after a few hits, Gemini is able to generate valid responses enough number of times so that I could finish the debugging and make this article possible. You can generate the token needed to access AI Studio through API here. Below is the curl which contains the query I have used to generate the data. Parsing a json felt less harrowing than a csv or a text file, hence my choice.

There is a crucial benefit of customization, for example if you want your AI's API service to create only 5 entries, you can change the search query to "generate a list of five Harry potter characters with their names and single worded corporate job roles in simple json".

customized request

Talking about API hits, let's check out the second tool.

2. Jackson:

I usually use RestAssured for my API testing, but I have decided to explore Jackson this time, and it turned out to be perfect. Combining it with RestAssured can make the API tests even more robust. Let's check out the code:

a. post API for the above curl request using Jackson. Accio! 💫

The above code is a part of the main class Gemini.java which also contains the methods for building POJO for the request built in the method above. It hits the API and stores the names and roles of the characters and return it and return it as a map. The code looks lengthier as safety settings mentioned by google need to be added. The size of the map generated above will be used to decide the number of rows deleted omitting Seamus Finnigan as he is the showstopper of this article.

Now one can ask me, you have generated the data, good. How are you going to use this data? This brings the third tool.

3. reqres.in: This is one of the friendliest APIs on the internet to practice API testing. It supports CRUD (Create, Read, Update, Delete) actions related to user management. So, we are going to create and delete users in reqres API using the map generated by the aiDataGenerator() method above.

User creation: Erecto! 💫

The above code returns a user ID along with the timestamp of the user creation.

User Deletion: Avada Kedavra! ☠

The code above belongs to Reqres.java class. In between the user creation and deletion, the data is documented which brings us to the final tool of the article.

4. Google cloud API Library: Google has a vast collection of tools and products. In order to access them via APIs you can use this library. All you have to do is create a project on google cloud, activate the necessary APIs in the library, enable the oauth consent and generate credentials for a desktop app and download the client secret json which can be used for interacting with google API's. You can get a clear idea about the process in this documentation. So, let's have a look at the methods that add and remove rows from the google sheet.

Adding rows: Aparecium! 💫

Removing rows: Evanesco! 💫

depending on the count of the number of users created the number of rows that should be deleted will be calculated. the methods above are available in GoogleSheetsIntegration.java class.

We have reached the end of the article. Hope your experience was magical.

GitHub code link: https://github.com/vinayviga/AutomationSnippets/tree/809591acca1fc70c91d39f9083e7008c51d11434/snippets/GenerativeAI_DataDriven_TestExample

PS:

  1. Some of you might be wondering what are all those words with exclamation marks before every code snippet? Those are spells from Harry potter movies. I tried to mimic the actions performed by the code in those snippets using spells 💫.

  2. I did not optimize the code and put it in a framework because it's the underlying concept I wanted to highlight, so a main method felt sufficient.

  3. In case of any queries or suggestions or even if you like what you've read, do send me an invite or a message 🙂

  4. I could have written my sob story as a post in LinkedIn and ask for job references, however I refrain from that approach. Instead, I share what I have learnt and put forth my effort. Do reach out if you have or know of an opportunity in case you think or feel that I, the test engineer who wrote this, and a few other articles is worth it.

Thank you, and stay tuned for more...

To view or add a comment, sign in

Explore topics