在 Android 应用开发中,大家经常使用单元测试框架进行 UI 测试。随着技术演进,Android Studio 推出的 Journey Test 功能,依托 Gemini AI,为 UI 测试带来了全新的范式转变。
核心能力:自然语言与 AI 驱动的测试
Journey Test 最大的亮点在于结合了自然语言和 Gemini AI 的能力。开发者无需再埋头于复杂的代码编写,只需用日常的自然语言描述测试步骤,比如 "在邮箱输入框中输入 user@example.com""验证是否显示'邮箱为必填项'的错误提示"。
xml
<action>Type "user@example.com" into the email field.</action>
<action>Verify that an error message "Email is required" is displayed.</action>
Gemini AI 就能将这些描述转化为可执行的测试用例,让测试用例既方便人类阅读和理解,又能被机器精准执行,就像产品经理或 QA 描述测试计划,Android Studio 能将其转化为完整且功能齐全的 UI 测试,包含断言和导航等关键部分。
从创建到执行:完整工作流
1. 创建 Journey 文件
在 Android Studio 中,通过以下步骤创建 Journey 文件:
- 右键点击项目面板中的应用模块;
- 选择
New > Test > Journey File
; - 输入测试名称与描述,生成 XML 文件。
以登录界面测试为例,Journey Test 能对必填字段、邮箱格式有效性、按钮启用 / 禁用逻辑以及成功登录行为等方面进行验证。通过完整的 Journey Test XML 文件,能清晰看到自然语言指令是如何转化为全面的 UI 测试流程的,还有相关视频演示直观展示测试的实际运行情况。
2. 编辑测试步骤
可通过 XML 的 Design 或 Code 视图来描述测试步骤。
以下是测试登录界面的 XML 示例:
xml
<?xml version="1.0" encoding="utf-8"?>
<!-- formatter:off -->
<journey name="LoginJourneyTestRefactored">
<description>Tests the login screen's initial state, input validation, button actions</description>
<actions>
<!-- Step 1: 验证初始界面状态与禁用的登录按钮 -->
<action>Verify that the email text input field is empty.</action>
<action>Verify that an error message "Email is required" is displayed below the email field.</action>
<action>Verify that an error message "Password is required" is displayed.</action>
<action>Check the "Login" button is disabled.</action>
<!-- Step 2: 测试无效邮箱格式与禁用的登录按钮 -->
<action>Type "abc@-" into the email text input field and move to next action.</action>
<action>Verify that an error message containing "Invalid Email" is displayed.</action>
<action>Check the "Login" button is still disabled.</action>
<!-- Step 3: 测试无效域名与禁用的登录按钮 -->
<action>Just append "com" to the text in the email text input field and move to next action.</action>
<action>Verify that an error message "This Domain is not allowed" is displayed.</action>
<action>Check the "Login" button is still disabled.</action>
<!-- Step 4: 清空邮箱并验证必填项错误 -->
<action>Clear the email text input field and verify its empty.</action>
<action>Verify that an error message "Email is required" is displayed below the email field.</action>
<action>Verify that the email text input field is empty.</action>
<!-- Step 5: 测试有效邮箱、验证密码必填与禁用的登录按钮 -->
<action>Type "sarningshivukl@gmail.com" into the email text input field.</action>
<action>Verify that no error message is displayed for the email field.</action>
<action>Verify that an error message "Password is required" is still displayed.</action>
<action>Check the "Login" button is still disabled don't enter anything.</action>
<!-- Step 6: 输入有效密码并验证无错误 -->
<action>Type "admin@12345" into the password text input field.</action>
<action>Verify that no error message is displayed for the email field.</action>
<action>Verify that no error message is displayed for the password field.</action>
<!-- Step 7: 点击登录并验证跳转到主页 -->
<action>Click the "Login" button and verify that the next screen is the HomeScreen.</action>
</actions>
</journey>
<!-- formatter:on -->
打开 Design 视图如下,提供更友好的操作解决,可以自由拖动和编辑每个 action
3. 运行与调试
选择目标设备(模拟器或物理机),打开 Journey XML 文件后点击"运行",Android Studio 会:
- 部署应用到设备;
- 调用 Gemini AI 执行测试步骤;
- 生成包含截图 和 Gemini 推理过程的测试报告,便于调试。

高效编写 Journey 的技巧
以详细且有条理的方式编写 Journey,能提升 Gemini 准确且确定地运行预期测试的可能性。以下是助力更高效编写 Journey 的技巧:
分离断言与操作: Gemini 会持续评估应用状态,判断是否处于可继续测试流程的 "正确" 状态。不过,你可能需要将更复杂的断言定义为单独步骤。此外,用 "Verify that""Assert that" 或 "Check that" 等关键短语开头,让 Gemini 知晓该步骤是断言。
用滑动操作指引 Gemini:除了点击、键盘输入等基础交互,Gemini 还支持按特定方向滑动屏幕的操作。比如,要滚动内容或关闭部分 UI,只需描述滑动操作的方向和意图。默认情况下,Gemini 从屏幕中心滑动,但你可引导它在设备屏幕的不同区域滑动。
避免复杂交互与配置变更:Gemini 目前不支持所有交互和配置变更,像多指手势、屏幕旋转或折叠操作等。不过,团队正在努力提升 Gemini 的能力。
现状与局限
Journey Test 处于早期实验阶段,存在以下局限:
- 执行速度慢:相比 Espresso 测试,相同用例执行时间更长,暂不适合 CI 流水线;
- 稳定性不足:Gemini 可能误解析指令,导致测试"偶发失败";
- 文本操作缺陷:长文本输入时光标位置异常、删除操作不彻底等问题。
适用场景与替代建议
适用场景
- 需求文档与测试用例的协同对齐(产品、开发、测试团队共享自然语言用例);
- 原型阶段的快速 UI 流程验证;
- 作为现有 Espresso/Compose 测试的补充(非核心流程用例)。
不适用场景
- 生产环境的 CI/CD 流水线;
- 对性能敏感的高频测试;
- 复杂交互(如多手势、屏幕旋转)的测试。
总结
Journey Test 是 Android 测试领域的一次创新尝试,它用"自然语言 + AI"降低了 UI 测试的技术门槛。尽管当前存在局限性,但在团队协作 和早期验证场景下价值显著。未来随着 Gemini AI 能力的迭代,Journey Test 有望成为 Android 测试生态的重要组成部分。