Curso De Testing Kotlin 〈iPhone〉

@Test fun `state flow emits new values`() = runTest { val viewModel = MyViewModel() val collector = viewModel.stateFlow.test { viewModel.doAction("Click") // Await the next emission val item = awaitItem() assertEquals("Loading", item.status) // Ensure no extra items came cancelAndIgnoreRemainingEvents() } } A professional Kotlin project separates test sources by type:

result.shouldBe(200) list.shouldContain("Kotlin") exception.shouldThrow<IllegalArgumentException> Kotlin Coroutines are amazing for production, but they are a nightmare for testing if you don't know the tricks. A naive test will pass when it should fail because the coroutine hasn't finished yet. The Golden Rule: runTest Never use Thread.sleep() in tests. Use kotlinx-coroutines-test . curso de testing kotlin

import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull class CalculatorTest { @Test fun `state flow emits new values`() =

class MathProps : StringSpec({ "Addition should be commutative" { forAll { a: Int, b: Int -> // The property we want to test (a + b) == (b + a) } } "String length should be non-negative" { forAll<Int> { length -> val str = "x".repeat(length.coerceAtLeast(0)) str.length >= 0 } } }) Use kotlinx-coroutines-test