Generating unit tests with Amazon Q
Amazon Q Developer agent provides an AI-powered unit test generation capability that automates the creation of unit tests throughout the software development lifecycle. This feature helps developers focus on accelerating feature development while ensuring code quality.
The Amazon Q agent for unit tests automates the following steps:
-
Test case identification: The agent uses your project structure, existing code, and targeted file in the workspace to identify appropriate test cases.
-
Mock and stub creation: Amazon Q generates necessary mocks and stubs for isolated testing.
-
Test code generation: The agent produces unit tests based on the identified test cases.
The Amazon Q agent for unit tests supports Java and Python projects in VS Code and JetBrains IDEs. To learn about the supported test frameworks for each programming language, see Language and framework support for unit test generation.
Prerequisites
To use the unit test generation feature, you must download and install the Amazon Q IDE extension for VS Code or JetBrains IDEs. Follow the instructions in Installing the Amazon Q Developer extension or plugin in your IDE to set up the extension.
Note
While recommended, a functional project with a test framework setup in your IDE is optional.
Generate unit tests with /test
After writing code, developers can initiate unit test generation in two primary ways:
-
Type
/test
in the Amazon Q chat, optionally specifying instructions for the class, function, or method to consider. -
Highlight a section of code, open the right-click Amazon Q menu, and choose the Generate tests option.
When developers use the /test
command without additional prompt, the
agent:
-
Examines the currently active file in the IDE.
-
Checks for the existence of a corresponding unit test file for this active file in a test directory.
-
If no such test file is found, it automatically creates a new one. The new test file is named after the original file, with a
test
prefix or suffix appended based on the test framework in use, and is placed in the test directory. -
If a test file already exists, it appends the new unit tests to the existing file.
Unit test generation process
The process involves the following steps:
-
Input inference: Amazon Q infers the target code, output file, and appropriate test and mock based on the workspace context and configured test framework. The agent identifies where to place the generated tests, either in an existing or new test file, and determines the target code to test. Developers can provide optional instructions to specify their target code and target file name.
-
Context enrichment: Amazon Q unit tests agent uses the open project as context, allowing it to generate tests that align with the IDE project's code and dependencies.
-
Test generation: The Amazon Q agent infers the appropriate inputs for unit test generation or allows users to provide that information manually. It then generates the unit tests. Throughout this process, the agent provides progress updates in the chat.
Note
-
When working with large projects, the initial project analysis may take some time. During this process, the progress indicator might remain at 0% for an extended period. This is normal behavior as Amazon Q indexes and uploads the project files.
-
Unit test generation is performed one file at a time. For multiple files, use the
/test
command separately on each file.
-
-
User review: Amazon Q provides a diff for the final generated tests. Developers can review the generated tests and choose to:
-
Accept the suggested tests.
-
Reject the changes.
-
Usage and considerations
The Amazon Q agent for unit tests is subject to general usage limits based on your subscription tier. These limits are shared across all Amazon Q features.
Note
Each /test
command counts toward your usage limit within the free tier.
For information about Amazon Q tiers of service, quotas, and pricing, see Understanding tiers of service for Amazon Q.
You can use the Amazon Q dashboard in the AWS Management Console to track unit test generation metrics such as the number of unit tests generated, accepted, and the acceptance rate. You can also monitor the usage of quota-limited features and access detailed cost reports by feature. To learn about the Amazon Q dashboard, see Amazon Q Developer dashboard.