在 Android 与 iOS 目标上运行测试
在每个平台上执行完整测试套件
在 Android 与 iOS 目标上运行测试 是 CoddyKit 上的免费 Kotlin Multiplatform Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Kotlin Multiplatform Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Kotlin Multiplatform Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
One Suite, Every Target
A common test compiles for each platform, so you can run the exact same suite on Android and on iOS. 🚀
Gradle Drives It All
You launch tests through Gradle tasks, each one tied to a specific platform target of your shared module.
Run the Android Tests
The testDebugUnitTest task runs your common and JVM tests on the local JVM, no emulator needed.
./gradlew :shared:testDebugUnitTestRun on the iOS Simulator
A target-named task runs the same shared tests on the iOS simulator via Kotlin/Native's runner.
./gradlew :shared:iosSimulatorArm64TestPick the Right Arch
The iOS task name encodes the architecture, so Apple Silicon uses iosSimulatorArm64 while older setups differ.
Run Everything at Once
Calling the broad allTests task executes every target's suite in a single command.
./gradlew :shared:allTestsReading the Report
Gradle writes an HTML report under build/reports, showing which case failed on which platform.
Catch Platform Drift
Running on both targets reveals subtle differences, like number formatting, before they reach your users.
iOS Tests Need a Mac
The iOS simulator target only runs on macOS with Xcode installed, while JVM tests run anywhere.
Wire It Into CI
In CI, run the JVM tests on any runner and the iOS suite on a macOS runner to cover everything.
Green on Both Means Done
When the suite passes on Android and iOS, you have real confidence that shared logic behaves identically. ✅
Quick Check
Pick the Android test task.
Recap
Use Gradle tasks per target: testDebugUnitTest for Android and iosSimulatorArm64Test for iOS, or allTests for both.
常见问题解答
「在 Android 与 iOS 目标上运行测试」课时是免费的吗?
是的 — 「在 Android 与 iOS 目标上运行测试」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Kotlin Multiplatform Academy 课程的其余内容,请升级到 CoddyKit PRO。 Kotlin Multiplatform Academy 课程共包含 4 节课。
「在 Android 与 iOS 目标上运行测试」这节课中我会学到什么?
在每个平台上执行完整测试套件 你通过在浏览器中直接运行的动手代码来练习 Kotlin Multiplatform Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Kotlin Multiplatform Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Kotlin Multiplatform Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「在 Android 与 iOS 目标上运行测试」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Kotlin Multiplatform Academy 课中编写并运行代码吗?
能。每节 Kotlin Multiplatform Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- commonTest 中的 kotlin.test
- 测试挂起函数与 Flow
- 伪造 Ktor 客户端与仓库
- 在 Android 与 iOS 目标上运行测试