testing-coroutines-with-runtest

Standardize JVM coroutine testing with kotlinx-coroutines-test and runTest.

Updated Jul 20, 2026
One-click install
npx skills add https://github.com/trancee/MeshLink-template --skill testing-coroutines-with-runtest-trancee
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: testing-coroutines-with-runtest
Source: https://github.com/trancee/MeshLink-template/tree/main/.agents/skills/android-testing-skills/jvm-tests/coroutines/testing-coroutines-with-runtest
Command: npx skills add https://github.com/trancee/MeshLink-template --skill testing-coroutines-with-runtest-trancee

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires kotlinx-coroutines-test.

What problem does it solve?

This skill resolves common testing pitfalls in Kotlin coroutines, such as tests hanging due to real-time delays, flaky asynchronous assertions, and missing Main dispatcher errors in JVM unit tests.

Core Features & Use Cases

  • Virtual Time Control: Precisely advance time using TestCoroutineScheduler to test timeouts, debouncing, and retries without waiting for wall-clock time.
  • Main Dispatcher Management: Provides canonical patterns for replacing Dispatchers.Main with test-friendly dispatchers to support ViewModel and lifecycle-scoped code.
  • Lifecycle Safety: Ensures proper cleanup of coroutines and prevents state leakage between tests using TestScope and backgroundScope.

Quick Start

Use the testing-coroutines-with-runtest skill to configure a MainDispatcherRule and execute a suspend function test using runTest.

Frequently Asked Questions about testing-coroutines-with-runtest

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I test Kotlin coroutines without waiting for real-time delays?

Fix missing Main dispatcher errors in JVM unit tests by replacing Dispatchers.Main with a test-friendly dispatcher. Implement a MainDispatcherRule to substitute the dispatcher, enabling proper ViewModel and lifecycle-scoped code verification.

How do I prevent state leakage and hanging coroutines between unit tests?

Prevent state leakage and hanging coroutines by managing coroutine lifecycle safety with TestScope and backgroundScope. Proper cleanup ensures background coroutines complete and prevents state leakage between tests.

Does runTest support Kotlin Multiplatform and Android asynchronous code verification?

Yes, runTest supports Kotlin Multiplatform and Android projects for asynchronous code verification. It standardizes suspend function testing on the JVM, satisfying the requirement for deterministic, non-flaky tests across these platforms.

Why are my Kotlin coroutine unit tests flaky and hanging due to asynchronous assertions?

Coroutine unit tests are flaky and hanging due to real-time delays and uncontrolled asynchronous execution. Standardize verification using runTest and virtual time manipulation to ensure deterministic execution and eliminate flaky assertions.