0Pricing
Kotlin Multiplatform Academy · Lezione

kotlin.test in commonTest

Scriva una sola volta unit test indipendenti dalla piattaforma.

kotlin.test in commonTest è una lezione Kotlin Multiplatform Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Kotlin Multiplatform Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Kotlin Multiplatform Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

Domande Frequenti

La lezione «kotlin.test in commonTest» è gratuita?

Sì — il testo completo di «kotlin.test in commonTest» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Kotlin Multiplatform Academy, passa a CoddyKit PRO. Il corso Kotlin Multiplatform Academy include 4 lezioni in totale.

Cosa imparerò in «kotlin.test in commonTest»?

Scriva una sola volta unit test indipendenti dalla piattaforma. Eserciti Kotlin Multiplatform Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Kotlin Multiplatform Academy?

Non è richiesta alcuna esperienza precedente. Kotlin Multiplatform Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.

Quanto tempo richiede la lezione «kotlin.test in commonTest»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Kotlin Multiplatform Academy?

Sì. Ogni lezione Kotlin Multiplatform Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. kotlin.test in commonTest
  2. Testare funzioni suspend e Flow
  3. Simulare il client e i repository Ktor
  4. Eseguire i test sui target Android e iOS
← Torna a Kotlin Multiplatform Academy