SKILL

kotlin-testing

Kotlin testing patterns using Kotest, MockK, coroutine testing, property-based testing, and Kover coverage. Follows TDD methodology and idiomatic Kotlin practices.

Machine translation
Read the original Chinese text

使用Kotest、MockK、协程测试、基于属性的测试和Kover覆盖率的Kotlin测试模式。遵循TDD方法论和地道的Kotlin实践。

Target: Software development & testing. Automatically collected · unverified.

Excerpt from the author’s documentation

Extracted from a fixed version of SKILL.md in its original language. This is the author’s documentation; installation, compatibility and runtime results have not been verified by this site.

Read the original Chinese text

Topics in the source

  • Kotlin 测试模式
  • 何时使用
  • 工作原理
  • 示例
  • 快速参考
  • Kotlin 的 TDD 工作流
  • RED-GREEN-REFACTOR 周期
  • Kotlin 中逐步进行 TDD

遵循 TDD 方法论,使用 Kotest 和 MockK 编写可靠、可维护测试的全面 Kotlin 测试模式。 编写新的 Kotlin 函数或类 为现有 Kotlin 代码添加测试覆盖率 实现基于属性的测试 在 Kotlin 项目中遵循 TDD 工作流 为代码覆盖率配置 Kover 1. 确定目标代码 — 找到要测试的函数、类或模块 2. 编写 Kotest 规范 — 选择与测试范围匹配的规范样式(StringSpec、FunSpec、BehaviorSpec) 3. 模拟依赖项 — 使用 MockK 来隔离被测单元 4. 运行测试(红色阶段) — 验证测试是否按预期失败 5. 实现代码(绿色阶段) — 编写最少的代码以使测试通过 6. 重构 — 改进实现,同时保持测试通过 7. 检查覆盖率 — 运行 ./gradlew koverHtmlReport 并验证 80%+ 的覆盖率 以下部分包含每个测试模式的详细、可运行示例: Kotest 规范 — Kotest 规范样式 中的 StringSpec、FunSpec、BehaviorSpec、DescribeSpec 示例 模拟 — MockK 中的 MockK 设置、协程模拟、参数捕获 TDD 演练 — Kotlin 的 TDD 工作流 中 EmailValidator 的完整 RED/GREEN/REFACTOR 周期 覆盖率 — Kover 覆盖率 中的 Kover 配置和命令 Ktor 测试 — Ktor testApplication 测试 中的 testApplication 设置 | 代码类型 | 目标 | |-----------|--------| | 关键业务逻辑 | 100% | | 公共 API | 90%+ | | 通用代码 | 80%+ | | 生成的 / 配置代码 | 排除 | 应做: 先写测试(TDD) 在整个项目中一致地使用 Kotest 的规范样式 对挂起函数使用 MockK 的 coEvery/coVerify 对协程测试使用 runTest 测试行为,而非实现 对纯函数使用基于属性的测试 为清晰起见使用 data class 测试固件 不应做: 混合使用测试框架(选择 Kotest 并坚持使用) 模拟数据类(使用真实实例) 在协程测试中使用 Thread.sleep()(改用 advanceTimeBy) 跳过 TDD 中的红色阶段 直接测试私有函数 忽略不稳定的测试 记住:测试就是文档。它们展示了你的 Kotlin 代码应如何使用。使用 Kotest 富有表现力的匹配器使测试可读,并使用 MockK 来清晰地模拟依赖项。

Source repository
affaan-m/ECC
Source file
docs/zh-CN/skills/kotlin-testing/SKILL.md
Fixed revision
d2ef2680594f9a17526a6e1a2e18b3d242c5d17b
Read the complete original documentation ↗

Sources and evidence

This record was collected automatically. Its documentation, suitability and runtime behavior have not been manually verified.

Author: affaan-m/ECC · original repository metadata. Documentation review: Not reviewed. Last source check: 2026-11-09T15:01:42.414Z.

Categories are inferred from names, descriptions and paths. They do not establish compatibility.

License and attribution

MIT. Third-party content and copyright remain with their authors. Refer to the original license and terms.

Original license ↗

Synchronization, copying and user-reported success do not mark a resource as runtime-tested.

Limitations and notes

  • Collecting metadata does not mean this site has installed, adapted, runtime-tested or security-reviewed the resource.