How can we get better at writing great test suites using AI?

Steve and Caz working alongside each other

We explore Test Driven Development with an AI twist!

I’m probably not the first to the table on this one, but while investigating the many benefits of using AI in development, I pondered the value of feeding a suite of tests to the AI engine and then asking it to write code to make the tests pass.



For those of us trying to stick to the best practices of an Agile working environment, Test Driven Development can often be a sticking point. I’m sure that once you become a seasoned test-driven developer things speed up, but how about those getting started? What’s a good introduction to the processes, and how can we get better at writing great test suites?

Though discussions around the moral use of AI in development continue unabated, the fact remains that this technology exists and is being used in our industry. We probably owe it to ourselves as developers to explore its many facets, rather than shy away from it.

Before we start, during my many conversations with AI, I realised that it needs a lot of help. We must be highly prescriptive in what we ask for. On the plus side of that situation; this should encourage us to write great tests!

I’m using ChatGPT for this experiment, and I’ll be asking it to return the code in Typescript, however, you could just as easily request your language of choice. This is my opening gambit:

Write me a typescript function to pass the following tests:
const date = new Date(“12/03/1983”)
const result: string = getDateAsString(date)
assert(result).toEqual(“12th March, 1983”)

Note: this date format works in the UK, but others of you may need to adjust the output requirements (or input date) in order for the test to make sense.

The response was a set of functions that indeed passed the test! Another nicety was the explanation of the code it was writing. Handy content should we need to add comments in the code for any reason.

Code snippet sceenshot

I want to expand this function to highlight certain days. For this example, I have used 25th December (Christmas Day) to return a specifically formatted response, though you could use any other day. I said to Chat-GPT:

Now make it also pass the following tests:

const date = new Date(“25/12/1983");
const result: string = getDateAsString(date);
assert(result).toEqual(“Christmas Day, 1983”);

The response came quickly, and a new version of the function was returned which included these additional lines of code:

Screenshot of a line of code

It also chose to add an additional function to return the month name instead of using date.toLocalDateString; who knows what it was thinking.

The more tests you add, theoretically, the more robust the code that comes back. However, before you rush off and inundate your favourite AI tool with a slew of test assertions, please note that I have a blooper reel of failed replies. Some responses added extra characters I didn’t ask for, some ignored the format completely or chose to ignore some of the test specs.

For this reason, it’s important we still have a sizable hand in building the final code, and it is unlikely that AI will replace us any time soon, but that doesn’t mean we should ignore this technology, or shy away from making use of it.

Test Driven Development is an often overlooked process that is a real benefit for those needing to develop reliable code. It’s often cast aside due to budget constraints, deadlines and the like and if we can put AI to work in this area, our code quality can only get better - and our stress levels will likely fall too!

Permission to share this content was given by ChatGPT itself; I quote:

AI language code snippet

Remember to use AI responsibly… and Happy Coding!


Looking for something else?

Search over 400 blog posts from our team

Want to hear more?

Subscribe to our monthly digest of blogs to stay in the loop and come with us on our journey to make things better!