Test a contract through observable results
A unit test should reveal a behavioral regression without requiring real network services, wall-clock delays, or shared mutable fixtures. Name the scenario and expected result so a failure explains what broke.
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
fun remaining(total: Int, completed: Int): Int {
require(total >= 0 && completed in 0..total)
return total - completed
}
class RemainingTest {
@Test fun subtractsCompletedLessons() { assertEquals(3, remaining(5, 2)) }
@Test fun rejectsExcessCompletion() {
assertFailsWith<IllegalArgumentException> { remaining(2, 3) }
}
}
Configure the appropriate kotlin-test integration and runner for your learning project. Put JVM tests in src/test/kotlin. Android local tests and instrumented device tests have different execution environments.
Exercise
Add zero-total, all-complete, and negative-input cases. Temporarily introduce an off-by-one defect and confirm a test fails for the intended reason.
Check: tests should assert the public contract, not reproduce the implementation line by line or depend on execution order.