commonTestのkotlin.test
プラットフォームに依存しないユニットテストを一度だけ書きます
「commonTestのkotlin.test」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Test Once, Trust Everywhere
Shared code deserves shared tests. With kotlin.test you write a single suite that proves your logic on every target. 🧪
Add the Test Dependency
First, pull kotlin.test into your common test source set so the assertions are available on all platforms.
commonTest.dependencies {
implementation(kotlin("test"))
}Where Common Tests Live
Your portable tests belong in commonTest, the source set that mirrors commonMain and compiles for every target.
The @Test Annotation
Mark any function with @Test and the multiplatform runner treats it as a test case to execute.
@Test
fun greetsByName() {
// assertions go here
}assertEquals
The everyday assertion is assertEquals: it compares an expected value to the actual result of your shared code.
assertEquals("Hi, Sam", greet("Sam"))assertTrue and assertFalse
For boolean checks reach for assertTrue or assertFalse, which fail when the condition is not what you expect.
assertTrue(price > 0)
assertFalse(items.isEmpty())Checking for Null
Use assertNull and assertNotNull to express that a value should, or should not, be absent.
assertNotNull(user.email)Asserting Exceptions
To prove that bad input fails, wrap the call in assertFailsWith and name the exception you expect.
assertFailsWith<IllegalArgumentException> {
parse("not-a-number")
}Setup with @BeforeTest
Need fresh state per test? A function marked @BeforeTest runs before each case to prepare it.
@BeforeTest
fun setup() {
calc = TaxCalculator()
}One Assertion, One Idea
Keep each test focused: a clear name plus a single core assertion makes failures instantly readable.
It Runs on Every Target
Because the test sits in commonTest, the same case runs under the Android and iOS test tasks, guarding both apps at once. ✅
Quick Check
Pick the right common assertion.
Recap
Add kotlin.test, place cases in commonTest, mark them @Test, and assert once to verify shared logic everywhere.
よくある質問
「commonTestのkotlin.test」レッスンは無料ですか?
はい。「commonTestのkotlin.test」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「commonTestのkotlin.test」で何を学びますか?
プラットフォームに依存しないユニットテストを一度だけ書きます ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「commonTestのkotlin.test」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- commonTestのkotlin.test
- suspend関数とFlowをテストする
- Ktor Clientとリポジトリをフェイク化する
- AndroidとiOSのターゲットでテストを実行する