Saltar a contenido

1.4 Pruebas

Una prueba unitaria comprueba automáticamente que una función devuelve lo esperado. En Kotlin se usa kotlin.test sobre JUnit.

Dependencia (Gradle Kotlin DSL)

dependencies {
    testImplementation(kotlin("test"))
}

tasks.test {
    useJUnitPlatform()
}

La función

// src/main/kotlin/Calculadora.kt
fun suma(a: Int, b: Int): Int = a + b

fun dividir(a: Int, b: Int): Double {
    require(b != 0) { "b no puede ser 0" }
    return a.toDouble() / b
}

La prueba

// src/test/kotlin/CalculadoraTest.kt
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class CalculadoraTest {
    @Test
    fun sumaDosPositivos() {
        assertEquals(5, suma(2, 3))
    }

    @Test
    fun dividirPorCeroLanzaExcepcion() {
        assertFailsWith<IllegalArgumentException> { dividir(1, 0) }
    }
}
./gradlew test

Patrón AAA

Arrange (preparar), Act (ejecutar), Assert (comprobar).