Files
android-client/docs/testing.md
2026-09-03 19:03:35 +08:00

35 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 测试
## 运行命令
```bash
./gradlew test # 纯 JVM 模块(core:model/domain/common)单元测试
./gradlew testDebugUnitTest # Android 模块单元测试(本地 JVM)
./gradlew connectedDebugAndroidTest # 仪器测试(需连接模拟器/真机)
```
## 目录与命名
- JVM 单元测试:`src/test/kotlin/`;Android 单元测试:`src/test/kotlin/`。
- 仪器测试:`src/androidTest/kotlin/`。
- 测试类命名 `<Subject>Test`;测试方法名用反引号描述意图(`fun \`invoke delegates to implementation\`()`)。
## 测什么
- **领域层优先**(`core:domain`):UseCase、纯逻辑、状态转换——用 `kotlinx-coroutines-test` 的 `runTest` + `truth` 断言,快速且稳定。
- **数据层**(`core:data`):Repository 用 fake 数据源(实现 `core:domain` 里的接口),不真打网络/数据库。
- **UI**:`@HiltViewModel` 的 `ViewModel` 用 `StateFlow` 断言状态;Composable 用 Compose UI test。
- 网络 / 数据库边界用接口隔离,用 fake/dummy 替换真实实现。
## 依赖
- `junit`:`@Test`
- `truth`:断言(更可读的 `assertThat(...)`)
- `turbine`:Flow 测试(需要时再加)
- `kotlinx-coroutines-test`:`runTest`
## 约定
- 不写"假测试"(无断言的空壳);每条测试验证一个明确行为。
- 提交 PR 时在描述里写清运行的命令与结果。