0Pricing
Kotlin Multiplatform Academy · Lekcja

kotlin.test w commonTest

Pisz raz niezależne od platformy testy jednostkowe.

kotlin.test w commonTest to bezpłatna lekcja Kotlin Multiplatform Academy na CoddyKit. To lekcja 1 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Kotlin Multiplatform Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Kotlin Multiplatform Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Często zadawane pytania

Czy lekcja „kotlin.test w commonTest” jest bezpłatna?

Tak — pełny tekst „kotlin.test w commonTest” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Kotlin Multiplatform Academy, przejdź na CoddyKit PRO. Kurs Kotlin Multiplatform Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „kotlin.test w commonTest”?

Pisz raz niezależne od platformy testy jednostkowe. Ćwiczysz Kotlin Multiplatform Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Kotlin Multiplatform Academy?

Nie wymagamy żadnego doświadczenia. Kotlin Multiplatform Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 1 z 4.

Ile czasu zajmuje lekcja „kotlin.test w commonTest”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Kotlin Multiplatform Academy?

Tak. Każda lekcja Kotlin Multiplatform Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. kotlin.test w commonTest
  2. Testowanie funkcji suspend i Flow
  3. Atrapy klienta Ktor i repozytoriów
  4. Uruchamianie testów dla celów Androida i iOS
← Powrót do Kotlin Multiplatform Academy