How to Automatically Generate Test File with AI
Tags: testing, ai, tooling, tutorial
Published: 2023-01-18 16:17:36

Talks around unit tests and TDD in general have been quite polarizing. On one hand, it makes sense to have a measurable gatekeeper in place especially for large projects. On the other hand, it's just an awfully lot of work for such an unproductive task. Not to mention the learning curve that can vary for beginners, and often lack of documentation or learning sources for some language. But no matter one's strong opinion on the matter, at the end of the day, it's either gonna be a job done or a tech debt looming over us all.

Thankfully, the latest development on AI have made it possible to automate some part of the test writing process. Keep in mind that a lot of these tools are mostly research preview or usable prototype, which would require signing up for access.

Here's some tool you can use to write test from scratch, or refine existing test to be better:

## 1. Github's Testpilot
After launching their AI pair programmer, [Copilot](https://github.com/features/copilot), last year, Github cited [Testpilot](https://githubnext.com/projects/testpilot/) as one of their many [next exciting product](https://githubnext.com/). Similar to Copilot, Testpilot come as part of a VSCode extension and is still a usable prototype. 


![Using Testpilot on Github Copilot Lab's Extension](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4wx1gdkyc9tkvlpnd07.png)


To use it, you need to signup to [Github Copilot](https://github.com/github-copilot/signup) access, install [Github Copilot Extension](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot) and then install [Github Copilot Labs Extension](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot-labs) on VSCode. Keep in mind that Github Copilot access is a $10 plan per month by the time of writing this article.

## 2. OpenAI Codex
A free alternative to Testpilot would be [OpenAI Codex](https://openai.com/blog/openai-codex/) that comes in limited beta. It doesn't come in the form of VSCode extension, but instead a website where you can copy your code and insert the prompt to.


![Using OpenAI Beta Playground](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xz4ibljriwj75qk1qoi4.png)


To access this page, you need to [sign up](https://beta.openai.com/signup) for an OpenAI account, and then go to its [playground](https://beta.openai.com/playground) as seen in the image above. The page provides you with many tuning options you can tinker with, but which could be too verbose for beginner. This is suitable for when you want to experiment and need more freedom with prompts.

## 3. Auto-test CLI
The next one would be a shameless plug from myself. Auto-test is a CLI tool that uses OpenAI Codex under the hood, but can hopefully provide a better experience as you can do the test generation process from the CLI. You only need to tell the filename, and it will generate the test file complete with all the test cases inside.


![Running auto-test from terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zvqjnqa3ddp5w64aafd.png)



To use it, you need to [install auto-test](https://github.com/atikahe/auto-test/blob/v0.1.0/README.md) and include it in the `$PATH` of your machine. Then, get your [API Key](https://beta.openai.com/account/api-keys) from OpenAI's account page and export it to your terminal profile. And that's it! You can use `auto-test` command and point it to any file you want to be tested.

The result of the test may vary. If the result is unsatisfactory to you, you can add a custom prompt that helps the AI better understand your code and generate better test. What has worked for me is by explaining a little bit about my code and what I want it to accomplish, usually with an example of input and output. Or just providing a summary of the code would also suffice. You can add custom prompt using the `--prompt` or `-p` flag.


![Running auto-test with custom prompt](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2bujxtjikz0pqy7af6bl.png)


If you want more freedom of the prompt and completely override the default one, you can always add `--override` or `-o` flag at the end of the command.

Keep in mind that in v0.1 version, the generated test file might lack important things such as importing dependencies or mocking external functions. You would still need to add this yourself, but, on the bright side, at least we're no longer writing test from scratch :)