[ Web Proxy ]
URL:
Viewing: https://developer.android.com/studio/gemini/generate-unit-tests [Back]  [Original]

Generate unit tests with Gemini  |  Android Studio  |  Android Developers Skip to main content
Essentials Design & Plan Develop Google Play Blog
Search:
Android Studio
Android Developers [Android Developers]

Generate unit tests with Gemini Stay organized with collections Save and categorize content based on your preferences.

Note: Generate unit tests is a preview feature and only available in Android Studio Panda 4 | 2025.3.4 Canary 3 or higher. If you are on an earlier version, you can generate unit test scenarios.

Gemini in Android Studio can generate comprehensive, compilable unit tests for your Kotlin and Java code. Gemini analyzes your source code to identify constructor dependencies, business logic branches, and edge cases, and then automatically creates a complete test class. This includes the generation of setUp methods, mock initialization, and individual test cases tailored to your project's specific architecture and coding style.

Gemini detects your project's existing configuration and uses the appropriate frameworks and mocking libraries. This helps you bootstrap test classes, saving you time on repetitive setup.

Prerequisites

This feature is available in Android Studio Panda 4 | 2025.3.4 Canary 3 or higher.

Generate unit tests from the editor

To generate unit tests from the Android Studio editor, do the following:

  1. Open a Kotlin or Java source file in the Android Studio editor.
  2. Right-click a class name or a specific method, or select a block of code, and then select AI > Generate Unit Tests.

Generate unit tests from the tool window

To ask Gemini directly to generate unit tests, do the following:

  1. Click Agent in the tool window bar.
  2. Enter a request such as "Generate unit tests for this file" or "Write unit tests for MyClass".
Figure 1. Unit test generation demo


Figure 2. Unit test generation with code selection demo

Generate unit test scenarios

Note: The ability to generate only test scenarios is being upgraded by Generate unit tests, which offers more comprehensive test generation, including test implementation.

When writing unit tests for your app, Gemini can suggest test scenarios using the context of the code you want to test. When generating unit test scenarios, Gemini includes detailed names and descriptions for your tests, so that you better understand the intention for each suggested test. You need to implement the body of each test yourself.

To generate unit test scenarios, do the following:

  1. Navigate to the class you want to generate unit test scenarios for.
  2. Right-click on the class name and select Gemini > Generate Unit Test Scenarios from the context menu.
  3. In the dialog that appears, select the methods of the class that you want to generate scenarios for, and set the destination package for the tests.
  4. Click OK.
  5. Confirm the destination directory for your tests and click OK.
  6. If the test class already exists, confirm whether you want Gemini to suggest updates to the existing file.

After Gemini processes the request, you should see either a new file with the suggested unit tests or a diff for you to accept recommended changes to an existing file.

Unit test scenario generation demo [Unit test scenario generation demo] Figure 3. Unit test scenario generation demo


Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2026-04-09 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-04-09 UTC."],[],[]]

Web Proxy Viewer  |  New URL  |  Original Page