Using ChatGPT to Write Unit Tests (A Step-by-Step Tutorial and Tips)

OpenAI’s ChatGPT has impacted software engineering in numerous aspects and developers are exploring ways to boost their efficiency with this innovative tool. One notable application gaining traction is using ChatGPT to write unit tests.

Imagine an AI assistant that doesn’t just understand your code but improves it, transforming both the pace and precision of your workflow. So the question is not can ChatGPT write unit tests but how? Let’s explore how integrating ChatGPT into testing workflow can enhance test case generation, and elevate code writing speed.

How Can ChatGPT Enhance Unit Test Writing?

Unit testing, an essential component of software development, has often been viewed as a time-consuming task, taking up to 35% of a developer’s time. It’s commonly seen as a ‘boring’ routine and, at times, even as technical debt. However, with the integration of ChatGPT, this perception is rapidly changing.

Using ChatGPT to write unit tests sufficiently optimizes the process. By leveraging its capabilities, developers can streamline their workflows, significantly reducing the time spent on writing tests. Furthermore, ChatGPT acts as a source of inspiration, generating ideas for code refactoring and improvement.

Generating Unit Tests with ChatGPT (Quick Tutorial)

Now, less words and more action. Let’s jump straight into the practice with an easy5-steps guide on ChatGPT unit tests writing:

  1. Open ChatGPT
  2. Prepare your prompt, e.g.: Generate unit tests for the following code: {your_code}
  3. Submit the prompt with your code
  4. Copy & Paste generated unit tests in your IDE
  5. Verify & refactor the generated unit tests
Unit Tests with ChatGPT

And that brings us to the end of this straightforward how-to guide. However, we’re not quite done yet. We have some valuable insights to share, so stay tuned! 

6 Tips How to Create a Good Prompt 

Creating effective prompts is the key to harnessing the full potential of ChatGPT for generating unit tests. A well-crafted prompt can make the difference between receiving a generic response and obtaining a highly relevant and useful output. To help you make the most out of ChatGPT’s capabilities, here are six tips on how to create a good prompt:

Be specific: One of the fundamental principles in prompt creation is to be specific.

  • Specify additional code contexts, such as models or interfaces to produce more relevant unit tests;
  • Specify libraries;
  • Specify components that should be mocked.

Ask ChatGPT to Refactor: Don’t limit your prompts to just generating tests from scratch. ChatGPT can be a valuable tool for refactoring existing unit tests.

  • Extract the method;
  • Use certain libraries to produce random values for models;
  • Change tests’ names according to your naming convention.

By incorporating these rules into your prompt-writing strategy, you can significantly enhance the quality and relevance of the unit tests generated by ChatGPT.

Unit Tests with ChatGPT prompt example

Practical Scenarios of Writing Unit Tests with ChatGPT 

In general, unit tests represent a routine task that Software Engineers often find mundane yet essential. It is not uncommon to encounter challenges when determining which test cases to include while writing unit tests. The introduction of ChatGPT has the potential to address this issue by providing a valuable starting point and reducing the need for extensive deliberation regarding test coverage.

Emphasizing the significance of a starting point is pertinent due to ChatGPT’s occasional inaccuracies, such as suggesting the use of non-existing libraries, making nonsensical assertions, or generating entirely meaningless tests. It is imperative to note that every result generated by ChatGPT should undergo verification and validation by an expert, typically an experienced Software Engineer. This underscores the importance of each line of code generated by GPT, as it serves as a beneficial launchpad for the subsequent test writing process.

Furthermore, ChatGPT excels in offering insights for potential code refactoring and identifying previously unaddressed test cases within your unit test codebase. As a starting point, ChatGPT not only streamlines the unit test writing process but also contributes to the enhancement of code quality and test coverage.

Writing Unit Tests with ChatGPT 

So, ChatGPT could be useful in the following scenarios:

  1. Generating unit tests from scratch
  2. Spotting and generating unit tests for uncovered test cases
  3. Generating, refactoring, and polishing ideas for unit tests

Let’s deep down to the basic one – Generate unit tests from scratch

Task. Generate unit tests from scratch (Java Example)

Now, let’s proceed with some practice in Java.

For those who are seeking practice, you can find a prepared a task in this repo –

Task Implementation. Generate unit tests from scratch (Java Example)

In the following example, we are using an open-source project Bobocode.

Let’s imagine that we are developing a bank application. A new feature has been developed – Account Analytics service which provides us with two methods, which should covered by unit tests:

 Unit Tests with ChatGPT example

Account has the following fields: 


Writing Unit Tests with ChatGPT prompt

Now we are all set! Let’s go to the ChatGPT and generate some unit tests!😄

Unit Tests with ChatGPT 

ChatGPT result: 

ChatGPT for Unit Tests

So, what stands out in this generated sample:

  • GPT has correctly named the unit tests as expected.
  • The tests follow the Arrange-Act-Assert pattern as desired.
  • Helpful methods have been included for creating Account stubs.
  • During the construction of the Account stub, all fields have been utilized.
  • The Faker library has been applied to generate values for the Account’s fields.

Areas for potential improvement:

  • Consider removing comments, as production-quality code typically doesn’t require them.
  • Address incorrect invocations of the Account builder.
  • Review and optimize imports.
  • Enhance the assertions within the unit tests.
  • Eliminate unused mocks from the setUp() method.

Now, let’s initiate the refactoring process.

ChatGPT for Unit Tests prep

For the sake of convenience, here you can access branches that showcase a comparison between the generated and refactored results.

Ultimately, we have transformed our unit tests into a more valuable and achieved successful results.

In conclusion, using ChatGPT to write unit tests undoubtedly expedites the process, offering a valuable starting point for developers. However, it is essential to bear in mind that the results generated by ChatGPT may not always be entirely accurate and should be subject to expert verification. Blindly relying on ChatGPT for test generation in a project is strongly discouraged. Instead, it should be seen as a powerful tool that, when used judiciously and in conjunction with human expertise, can significantly enhance the efficiency and effectiveness of unit test creation. By striking the right balance between automation and human oversight, software development teams can harness the full potential of ChatGPT while ensuring the integrity and reliability of their unit tests.