生成单元测试场景
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在为应用编写单元测试时,Gemini 可以根据您要测试的代码的上下文建议测试场景。在生成单元测试方案时,Gemini 会为测试添加详细的名称和说明,以便您更好地了解每项建议测试的意图。您需要自行实现每个测试的正文。
如需生成单元测试方案,请执行以下操作:
- 前往要为其生成单元测试方案的类。
- 右键点击类名称,然后从上下文菜单中选择 Gemini > Generate Unit Test Scenarios。
- 在随即显示的对话框中,选择要为其生成方案的类的方法,并设置测试的目标软件包。
- 点击确定。
- 确认测试的目标目录,然后点击 OK。
- 如果测试类已存在,请确认是否要让 Gemini 建议更新现有文件。
Gemini 处理请求后,您应该会看到一个包含建议的单元测试的新文件,或者一个差异文件,供您接受对现有文件的建议更改。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-31。
[null,null,["最后更新时间 (UTC):2025-07-31。"],[],[],null,["# Generate unit test scenarios\n\nWhen writing unit tests for your app, Gemini can suggest test scenarios using\nthe context of the code you want to test. When generating unit test scenarios,\nGemini includes detailed names and descriptions for your tests, so that you\nbetter understand the intention for each suggested test. You need to implement\nthe body of each test yourself.\n\nTo generate unit test scenarios, do the following:\n\n1. Navigate to the class you want to generate unit test scenarios for.\n2. Right-click on the class name and select **Gemini \\\u003e Generate Unit Test Scenarios** from the context menu.\n3. 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.\n4. Click **OK**.\n5. Confirm the destination directory for your tests and click **OK**.\n6. If the test class already exists, confirm whether you want Gemini to suggest updates to the existing file.\n\nAfter Gemini processes the request, you should see either a new file with the\nsuggested unit tests or a diff for you to accept recommended changes to an\nexisting file."]]