0Pricing
Kotlin Multiplatform Academy · Lesson

kotlin.test in commonTest

Write platform-agnostic unit tests once.

kotlin.test in commonTest is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “kotlin.test in commonTest” lesson free?

Yes — the full text of “kotlin.test in commonTest” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.

What will I learn in “kotlin.test in commonTest”?

Write platform-agnostic unit tests once. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Multiplatform Academy?

No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “kotlin.test in commonTest” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Multiplatform Academy lesson?

Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. kotlin.test in commonTest
  2. Test suspend Functions & Flows
  3. Fake the Ktor Client & Repos
  4. Run Tests on Android & iOS Targets
← Back to Kotlin Multiplatform Academy