agentleFS
agenticlukeagenticluke/kotlin-test-pilot-plus/skill/SKILL.md

kotlin-testing

Skill0 starsChanged 49 days agoMITView on GitHub

Write and improve Kotlin tests with Kotest, MockK, coroutine test tools, property tests, Kover, and the red-green-refactor TDD cycle. Use for new Kotlin code, missing tests, test bugs, slow or flaky tests, and coverage setup.

13 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use nuxt4-patterns, agent-payment-x402 and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • loulanyueSkill

    nuxt4-patterns

    Nuxt 4 app patterns for hydration safety, performance, route rules, lazy loading, and SSR-safe data fetching with useFetch and useAsyncData.

    Also in 10 projects that use this

  • TironFoxSkill

    agent-payment-x402

    Add x402 payment execution to AI agents — per-task budgets, spending controls, and non-custodial wallets via MCP tools. Use when agents need to pay for APIs, services, or other agents.

    Also in 6 projects that use this

  • TironFoxSkill

    openclaw-persona-forge

    为 OpenClaw AI Agent 锻造完整的龙虾灵魂方案。根据用户偏好或随机抽卡, 输出身份定位、灵魂描述(SOUL.md)、角色化底线规则、名字和头像生图提示词。 如当前环境提供已审核的生图 skill,可自动生成统一风格头像图片。 当用户需要创建、设计或定制 OpenClaw 龙虾灵魂时使用。 不适用于:微调已有 SOUL.md、非 OpenClaw 平台的角色设计、纯工具型无性格 Agent。 触发词:龙虾灵魂、虾魂、OpenClaw 灵魂、养虾灵魂、龙虾角色、龙虾定位、 龙虾剧本杀角色、龙虾游戏角色、龙虾 NPC、龙虾性格、龙虾背景故事、 lobster soul、lobster character、抽卡、随机龙虾、龙虾 SOUL、gacha。

    Also in 5 projects that use this

  • RojiosSkill

    dart-flutter-patterns

    Production-ready Dart and Flutter patterns covering null safety, immutable state, async composition, widget architecture, popular state management frameworks (BLoC, Riverpod, Provider), GoRouter navigation, Dio networking, Freezed code generation, and clean architecture.

    Also in 6 projects that use this

  • loulanyueSkill

    santa-method

    Multi-agent adversarial verification with convergence loop. Two independent review agents must both pass before output ships.

    Also in 8 projects that use this

  • ronmkrSkill

    quarkus-security

    Quarkus Security best practices for authentication, authorization, JWT/OIDC, RBAC, input validation, CSRF, secrets management, and dependency security.

    Also in 5 projects that use this

The file

Show its rules and text

We split this skill into the rules it gives an agent

49 rules. 0 are common, 39 are distinctive, and 10 are very specific to this project.

  • 0

    Common

    Most of the 13 edited versions we read keep these.

  • 39

    Distinctive

    This author's own choices. Worth a look.

  • 10

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Write small, clear, and stable Kotlin tests.

    line 11
  2. Distinctive:

    Use this skill when you:

    line 15
  3. Distinctive:

    Add a Kotlin function or class

    line 17
  4. Distinctive:

    Add tests to old Kotlin code

    line 18
  5. Distinctive:

    Test suspend functions or flows

    line 20
  6. Distinctive:

    Follow test-driven development, or TDD

    line 23
  7. Distinctive:

    Read the code and its build files.

    line 27
  8. Distinctive:

    Write one test that fails for the right reason.

    line 30
  9. Distinctive:

    Run that test and check the failure.

    line 31
  10. Distinctive:

    Write the least code needed to pass.

    line 32
  11. Distinctive:

    Run the full test set.

    line 33
  12. Distinctive:

    Check coverage for missed cases.

    line 35
  13. Distinctive:

    Do not change build tool or test library unless the task needs it.

    line 37
  14. Very specific:

    Use StringSpec for short, direct cases.

    line 41
  15. Very specific:

    Use FunSpec when setup or hooks help.

    line 42
  16. Very specific:

    Use BehaviorSpec when Given, When, and Then make a rule easier to read.

    line 43
  17. Distinctive:

    Use a property test when many inputs follow the same rule.

    line 44
  18. Distinctive:

    Use an integration test when real file, data, or network parts must work together.

    line 45
  19. Distinctive:

    Test what code does. Do not test private methods on their own.

    line 47
  20. Distinctive:

    Use clear test names. Each name should state one rule.

    line 79
  21. Distinctive:

    Use the most exact matcher you can.

    line 83
  22. Distinctive:

    Check error type and useful error data.

    line 107
  23. Very specific:

    Use every and verify for normal functions.

    line 140
  24. Very specific:

    Use coEvery and coVerify for suspend functions.

    line 141
  25. Very specific:

    Use relaxed = true only when unused calls do not matter.

    line 142
  26. Distinctive:

    Verify key effects, not every small call.

    line 145
  27. Distinctive:

    Avoid mocking the class under test.

    line 146
  28. Very specific:

    Do not hide missing setup with broad any() rules unless the input does not matter.

    line 147
  29. Very specific:

    Use runTest from kotlinx-coroutines-test.

    line 151
  30. Very specific:

    Do not use real sleep calls.

    line 180
  31. Distinctive:

    Test empty, error, and cancel cases.

    line 188
  32. Distinctive:

    Do not leave jobs running after the test ends.

    line 189
  33. Distinctive:

    Use property tests for rules that should hold for many values.

    line 193
  34. Distinctive:

    Keep property tests stable:

    line 211
  35. Distinctive:

    Add direct tests for key edge cases.

    line 214
  36. Distinctive:

    Keep each property simple.

    line 217
  37. Distinctive:

    Do not use random data when a short table of cases is clearer.

    line 218
  38. Very specific:

    Check that it fails because validateEmail is missing or not done.

    line 249
  39. Distinctive:

    Run the test again. Then run all tests:

    line 269
  40. Distinctive:

    Add more rules only when the app needs them.

    line 275
  41. Distinctive:

    Check cases that fit the code:

    line 279
  42. Distinctive:

    Use fixed clocks, test data, and random seeds.

    line 297
  43. Distinctive:

    Use the Kover tasks already set by the project.

    line 301
  44. Distinctive:

    Run the changed test by itself.

    line 316
  45. Distinctive:

    Check that no test depends on run order.

    line 318
  46. Distinctive:

    Check that coroutine jobs end.

    line 319
  47. Distinctive:

    Check that mocks do not leak between tests.

    line 320
  48. Distinctive:

    Check key errors and edge cases.

    line 321
  49. Distinctive:

    Run Kover if coverage is part of the task.

    line 322

“Common” is measured: kept by at least 6 in 10 of the 13 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Kotlin Testing

Original skill by ECC. Credit ECC in any copy or shared version of this skill.

Write small, clear, and stable Kotlin tests. Use Kotest for tests, MockK for test doubles, coroutine test tools for suspend code, and Kover for coverage.

When to Use

Use this skill when you:

  • Add a Kotlin function or class
  • Add tests to old Kotlin code
  • Fix a broken or flaky test
  • Test suspend functions or flows
  • Add property tests
  • Set up Kover coverage
  • Follow test-driven development, or TDD

Main Steps

  1. Read the code and its build files.
  2. Find the public result that must be true.
  3. Pick the smallest useful test type.
  4. Write one test that fails for the right reason.
  5. Run that test and check the failure.
  6. Write the least code needed to pass.
  7. Run the full test set.
  8. Clean up the code while tests stay green.
  9. Check coverage for missed cases.

Do not change build tool or test library unless the task needs it.

Pick the Right Test

  • Use StringSpec for short, direct cases.
  • Use FunSpec when setup or hooks help.
  • Use BehaviorSpec when Given, When, and Then make a rule easier to read.
  • Use a property test when many inputs follow the same rule.
  • Use an integration test when real file, data, or network parts must work together.

Test what code does. Do not test private methods on their own.

TDD Cycle

RED      Write one failing test.
GREEN    Write the least code that makes it pass.
REFACTOR Make the code clearer.
REPEAT   Add the next rule.

A RED test must fail because the feature is missing. A test that fails due to bad setup does not count.

Basic Kotest Example

package com.example.math

import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe

class CalculatorTest : StringSpec({
    "adds two numbers" {
        Calculator.add(2, 3) shouldBe 5
    }

    "adds negative numbers" {
        Calculator.add(-2, -3) shouldBe -5
    }
})

Use clear test names. Each name should state one rule.

Matchers

Use the most exact matcher you can.

result shouldBe expected
result shouldNotBe wrongValue

name shouldStartWith "Al"
name shouldContain "lic"

items shouldHaveSize 3
items shouldContain "book"
items.shouldBeEmpty()

value.shouldBeNull()
value.shouldNotBeNull()
value.shouldBeInstanceOf<User>()

count shouldBeGreaterThan 0

shouldThrow<IllegalArgumentException> {
    validateAge(-1)
}.message shouldBe "Age must be positive"

Check error type and useful error data. Do not only check that some error happened.

For money and decimal values, use the exact type used by the app. For floating point values, use a small allowed gap.

MockK

Mock only code outside the unit under test. Prefer real value objects and small fake classes when they are easy to build.

class UserServiceTest : FunSpec({
    val repository = mockk<UserRepository>()
    val service = UserService(repository)

    beforeTest {
        clearMocks(repository)
    }

    test("returns the saved user") {
        val user = User(id = "1", name = "Alice")
        every { repository.findById("1") } returns user

        service.findUser("1") shouldBe user

        verify(exactly = 1) {
            repository.findById("1")
        }
        confirmVerified(repository)
    }
})

Rules:

  • Use every and verify for normal functions.
  • Use coEvery and coVerify for suspend functions.
  • Use relaxed = true only when unused calls do not matter.
  • Clear shared mocks before each test.
  • Stub every call that affects the result.
  • Verify key effects, not every small call.
  • Avoid mocking the class under test.
  • Do not hide missing setup with broad any() rules unless the input does not matter.

Coroutine Tests

Use runTest from kotlinx-coroutines-test. It gives safe test scope and virtual time.

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import kotlinx.coroutines.test.runTest

class AsyncUserServiceTest : FunSpec({
    val repository = mockk<UserRepository>()
    val service = UserService(repository)

    test("loads a user") {
        runTest {
            val user = User(id = "1", name = "Alice")
            coEvery { repository.findById("1") } returns user

            service.getUser("1") shouldBe user

            coVerify(exactly = 1) {
                repository.findById("1")
            }
        }
    }
})

Do not use real sleep calls. Use virtual time tools such as advanceTimeBy, runCurrent, or advanceUntilIdle.

Pass a test dispatcher or clock into app code when time or thread choice matters. Restore any changed main dispatcher after the test.

For flows:

  • Collect only the values the test needs.
  • End collection in the test.
  • Test empty, error, and cancel cases.
  • Do not leave jobs running after the test ends.

Property Tests

Use property tests for rules that should hold for many values.

import io.kotest.core.spec.style.StringSpec
import io.kotest.property.Arb
import io.kotest.property.arbitrary.int
import io.kotest.property.checkAll
import io.kotest.matchers.shouldBe

class MathPropertyTest : StringSpec({
    "adding zero keeps the same value" {
        checkAll(Arb.int()) { value ->
            value + 0 shouldBe value
        }
    }
})

Keep property tests stable:

  • Limit data to valid input when needed.
  • Add direct tests for key edge cases.
  • Include empty, zero, negative, min, and max values when valid.
  • Save the random seed when a failure must be run again.
  • Keep each property simple.
  • Do not use random data when a short table of cases is clearer.

Concrete Usage Example

Task: add an email check.

First, write tests:

class EmailValidatorTest : StringSpec({
    "accepts a valid email" {
        validateEmail("user@example.com")
            .getOrNull() shouldBe "user@example.com"
    }

    "rejects a blank email" {
        validateEmail("").isFailure shouldBe true
    }

    "rejects an email with no at sign" {
        validateEmail("user.example.com").isFailure shouldBe true
    }
})

Run the test:

./gradlew test --tests '*EmailValidatorTest'

Check that it fails because validateEmail is missing or not done. Then add the least code needed:

fun validateEmail(email: String): Result<String> {
    if (email.isBlank()) {
        return Result.failure(
            IllegalArgumentException("Email cannot be blank")
        )
    }

    if ('@' !in email) {
        return Result.failure(
            IllegalArgumentException("Email must contain @")
        )
    }

    return Result.success(email)
}

Run the test again. Then run all tests:

./gradlew test

Add more rules only when the app needs them. Do not claim full email rule support from a small check.

Edge Cases

Check cases that fit the code:

  • Empty and blank text
  • Null values when the type allows null
  • Zero and negative numbers
  • Min and max values
  • Empty and very large lists
  • Duplicate items
  • Unicode text
  • Time zones and daylight saving time
  • Missing files
  • Bad data
  • Timeouts
  • Cancelled coroutines
  • Failed work after part of a task is done
  • Calls made more than once
  • Shared state between tests

Use fixed clocks, test data, and random seeds. Tests must not depend on the current time, test order, local machine, or internet.

Kover Coverage

Use the Kover tasks already set by the project. Common tasks are:

./gradlew koverHtmlReport
./gradlew koverVerify

Open the HTML report and check missed branches, not only the total score.

Aim for 80 percent or the project rule, whichever is set. Coverage is a guide. A high score does not prove good tests. Do not add weak tests only to raise the number. Do not test generated code unless the project asks for it.

Final Check

Before finishing:

  • Run the changed test by itself.
  • Run the full test set.
  • Check that no test depends on run order.
  • Check that coroutine jobs end.
  • Check that mocks do not leak between tests.
  • Check key errors and edge cases.
  • Run Kover if coverage is part of the task.
  • Report any test you could not run and why.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.