使用 atest 测试平台代码
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android Studio for Platform (ASfP) 与 atest
命令行工具集成,让您能够直接从 IDE 在已连接的设备或模拟器上运行测试。
前提条件
- 打开包含 AOSP 源代码的 ASfP 项目。
- 成功构建代码并将其刷写到设备或模拟器。
- 使用
source build/envsetup.sh
和 lunch
初始化构建环境。
运行测试
您可以通过多种方式在 ASfP 中使用 atest
运行测试:
边线运行图标:点击运行图标 play_circle
点击编辑器中测试类或方法旁边的图标,即可运行该特定测试。
右键点击菜单:在“Project”窗口或编辑器中,右键点击某个测试文件、类或方法,然后选择 Run 'atest'。
使用终端:在 ASfP 中打开集成终端(View > Tool Windows > Terminal),然后直接运行 atest
命令。例如:bash
atest MyTestModule
atest MyTestClass#myTestMethod
。
查看测试结果
测试结果会显示在 ASfP 中的 Run 工具窗口中。此窗口会显示测试进度、通过或失败状态,并提供每个测试的日志和输出。
测试配置
当您从边线或右键点击菜单运行测试时,ASfP 会自动创建临时运行或调试配置。您可以修改并保存这些配置,以便快速重新运行具有特定选项或标志的测试:
- 依次前往 Run > Edit Configurations。
- 找到要修改的
atest
配置,或点击 + 按钮并选择 atest 来创建新配置。
- 指定测试模块、类、方法,并添加任何必要的
atest
命令行选项。
测试提示
- 有针对性的测试:仅运行所需的特定测试,以节省时间。使用
Module:Class#Method
格式或其他 atest
过滤条件。
- 使用模拟器快照:为了在模拟器上更快地运行测试,请考虑使用快照快速恢复到已知良好状态。
- 检查日志:使用运行工具窗口中的日志诊断任何测试失败问题。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-09-05。
[null,null,["最后更新时间 (UTC):2025-09-05。"],[],[],null,["Android Studio for Platform (ASfP) integrates with the `atest` command-line\ntool, letting you run tests on your connected device or emulator directly from\nthe IDE.\n\nPrerequisites\n\n- Open an ASfP project with your AOSP source code.\n- Successfully build and flash your code to a device or emulator.\n- Initialize your build environment using `source build/envsetup.sh` and `lunch`.\n\nRun tests\n\nYou can run tests using `atest` in ASfP in several ways:\n\n- **Gutter run icons:** Click the **Run** icon play_circle\n\n next to a test class or method in the editor to run that specific test.\n- **Right-click menu:** Right-click on a test file, class, or method in the\n Project window or editor and select **Run 'atest'**.\n\n- **Using the terminal:** Open the integrated terminal in ASfP (**View \\\u003e Tool\n Windows \\\u003e Terminal** ) and run `atest` commands directly. For example: `bash\n atest MyTestModule\n atest MyTestClass#myTestMethod`\n\nView test results\n\nTest results are displayed in the **Run** tool window within ASfP. This window\nshows the test progress, pass or fail status, and provides logs and output for\neach test.\n\nTest configurations\n\nWhen you run a test from the gutter or right-click menu, ASfP automatically\ncreates a temporary run or debug configuration. You can modify and save these\nconfigurations to quickly re-run tests with specific options or flags:\n\n1. Go to **Run \\\u003e Edit Configurations**.\n2. Find the `atest` configuration you want to modify or create a new one by clicking the **+** button and selecting **atest**.\n3. Specify the test module, class, method, and add any necessary `atest` command-line options.\n\nTips for testing\n\n- **Targeted testing:** Run only the specific tests you need to save time. Use the format `Module:Class#Method` or other `atest` filters.\n- **Use emulator snapshots:** For faster test runs on emulators, consider using snapshots to quickly revert to a known good state.\n- **Check logs:** Use the logs in the **Run** tool window to diagnose any test failures."]]