Tutorials

AI for PHP: How To Automate Unit Testing Using AI Assistant

Read this post in other languages:

Testing. If you’re anything like me, you’ll agree that it’s equal parts important and boring. It’s one of those things that isn’t much fun while you’re doing it, but you know it’ll pay dividends later on.

So, what makes testing so unpleasant?

For me, the biggest hurdle is the overhead that comes with the initial setup. I know that once I have a test in place, I’ll be happy to make changes and tweaks to it throughout the project’s lifespan. But getting started is what holds me back the most.

So what if that initial setup of writing a test could be simplified? What if we could get a rough outline of a test for free, use it as a starting point, and simply tweak it as needed? What if we could focus on the fun parts and have a computer do the boring stuff?

This is where AI can help us.

Just recently, we introduced a powerful tool into all our IDEs, including PhpStorm: JetBrains AI Assistant. In this blog post, I want to look at how we can use AI Assistant to automate the boring parts of programming, so that we can focus on the more exciting parts of testing.

Automated Unit Tests with AI Assistant

Generating unit tests is a perfect example of where AI Assistant can help us out. Let’s consider the following code snippet:

This is a snippet taken from a project called RFC Vote, which is open-source. You can find the full source code here. This project allows the PHP community to discuss open RFCs. People can make arguments for the pros and cons and have insightful discussions about the evolution of PHP.

This particular class is responsible for creating a comment on an existing argument. A comment belongs to a user, has a reference to an argument, and has a body. Finally, whenever a comment is written, notifications should be sent to all users following a specific argument.

Let’s use AI Assistant to scaffold a test for us and see what it can do.

Start by placing your cursor in the body of this __invoke method, then press Alt+Enter, and select AI Actions | Generate Unit Tests from the context menu.

AI Assistant will now write a draft version of a test for this class for you. If you want to learn how AI Assistant does all of this, you can read this post. When AI Assistant is done, you’ll see a draft version like so:

The most straightforward action is to press the Accept all button, which will create a new test file in the right location and copy this draft implementation into the real test file. You could, however, also press the Refresh button to generate a whole new implementation, in case you spotted any immediate problems.

In my case, I refreshed once or twice, pressed Accept all, and ran my test.

Pretty neat, right?

Now, hold on. There are a couple of other things to point out.

First, not all generations will run smoothly out of the box. For example, some versions included the RefreshDatabase trait, a common practice in Laravel tests. However, in this project, I have database testing set up a little differently, so I had to make one minor adjustment to the generated output. Sometimes, AI Assistant also generates code that doesn’t fully align with your code style. It does its best to take code style settings into account, but some details are tricky to get right.

Despite some shortcomings, keep in mind that our goal isn’t to generate the perfect test – it’s to have a scaffold in place that we can build on. AI Assistant is amazingly good at understanding the context of what we want to test and does a lot of things right. It saves time and allows us to focus on the more exciting parts of testing.

Pest automation

2023 has shown significant growth for the relatively new Pest testing framework. We at PhpStorm took over the development of the Pest plugin last year as well. Right now, AI Assistant support for Pest is a work in progress, and it will be available in the next major version, PhpStorm 2024.1. Stay tuned – we’ll definitely demonstrate how Pest users can benefit from AI Assistant in our upcoming blog posts.

Conclusion

Being able to scaffold unit tests automatically – with minimal manual work – is already pretty impressive, but there is some more fine-tuning to do.

Explore our next blog post from this series, where we look more closely at how we can fine-tune JetBrains AI Assistant’s test generation to save even more time. Subscribe to our blog in the subscription section below and don’t miss out on our news!

Useful links

Did you enjoy reading this blog post? Here are more from this series:

Resources:

Videos:

image description