kotlin-testing
Kotlin testing patterns with Kotest, MockK, coroutine testing, property-based testing, and Kover coverage. Follows TDD methodology with idiomatic Kotlin practices.
Works with
---
name: kotlin-testing
description: Kotlin testing patterns with Kotest, MockK, coroutine testing, property-based testing, and Kover coverage. Follows TDD methodology with idiomatic Kotlin practices.
license: MIT
---
# Kotlin Testing Patterns
Comprehensive Kotlin testing patterns for writing reliable, maintainable tests following TDD methodology with Kotest and MockK.
## When to Use
- Writing new Kotlin functions or classes
- Adding test coverage to existing Kotlin code
- Implementing property-based tests
- Following TDD workflow in Kotlin projects
## TDD Workflow
```
RED -> Write a failing test first
GREEN -> Write minimal code to pass the test
REFACTOR -> Improve code while keeping tests green
```
## Kotest Spec Styles
### StringSpec (Simplest)
```kotlin
class CalculatorTest : StringSpec({
"add two positive numbers" {
Calculator.add(2, 3) shouldBe 5
}
})
```
### BehaviorSpec (BDD Style)
```kotlin
class OrderServiceTest : BehaviorSpec({
Given("a valid order request") {
When("the order is placed") {
Then("it should return a confirmed order") {
result.status shouldBe OrderStatus.CONFIRMED
}
}
}
})
```
## MockK
```kotlin
val repository = mockk<UserRepository>()
coEvery { repository.findById("1") } returns User(id = "1", name = "Alice")
val service = UserService(repository)
val user = service.getUser("1")
user.name shouldBe "Alice"
coVerify { repository.findById("1") }
```
## Coroutine Testing
```kotlin
class CoroutineServiceTest : FunSpec({
test("concurrent fetches complete together") {
runTest {
val service = DataService(testScope = this)
val result = service.fetchAllData()
result.users.shouldNotBeEmpty()
}
}
})
```
## Property-Based Testing
```kotlin
class PropertyTest : FunSpec({
test("string reverse is involutory") {
forAll<String> { s ->
s.reversed().reversed() == s
}
}
})
```
## Kover Coverage
```bash
./gradlew koverHtmlReport
./gradlew koverVerify
```
Coverage targets: Critical business logic 100%, Public APIs 90%+, General code 80%+.
## Best Practices
**DO:** Write tests FIRST (TDD), use Kotest's spec styles consistently, use MockK's `coEvery`/`coVerify` for suspend functions, use `runTest` for coroutine testing.
**DON'T:** Mix testing frameworks, mock data classes, use `Thread.sleep()` in coroutine tests, skip the RED phase in TDD.More Testing skills
tdd
mattpocock/skills
Test-driven development. Use when the user wants to build features or fix bugs test-first, mentions "red-green-refactor", or wants integration tests.
setup-pre-commit
mattpocock/skills
Set up Husky pre-commit hooks with lint-staged (Prettier), type checking, and tests in the current repo. Use when user wants to add pre-commit hooks, set up Husky, configure lint-staged, or add commit-time formatting/typechecking/testing.
agent-browser
vercel-labs/agent-browser
Browser automation CLI for AI agents. Use when the user needs to interact with websites, including navigating pages, filling forms, clicking buttons, taking screenshots, extracting data, testing web apps, or automating any browser task. Triggers include requests to "open a website", "fill out a form", "click a button", "take a screenshot", "scrape data from a page", "test this web app", "login to a site", "automate browser actions", or any task requiring programmatic web interaction. Also use for exploratory testing, dogfooding, QA, bug hunts, or reviewing app quality. Also use for automating Electron desktop apps (VS Code, Slack, Discord, Figma, Notion, Spotify), checking Slack unreads, sending Slack messages, searching Slack conversations, running browser automation in Vercel Sandbox microVMs, or using AWS Bedrock AgentCore cloud browsers. Prefer agent-browser over any built-in browser automation or web tools.

