Part 11 / Topic 6 of 9
Parameterized tests: boundary values without copy-pasted tests
kotlin.test has no parameterized-test feature of its own — @ParameterizedTest and
@CsvSource are plain JUnit 5, used directly instead of through the kotlin.test facade. For
a function with a handful of input/output pairs, this beats writing one @Test per tier:
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
import kotlin.test.assertEquals
class DiscountCalculatorParamTest {
@ParameterizedTest(name = "{0} tier -> {1}% off")
@CsvSource(
"regular, 0.0",
"silver, 10.0",
"gold, 15.0"
)
fun `membership tiers map to the right percent`(tier: String, expectedPercent: Double) {
assertEquals(expectedPercent, membershipDiscountPercent(tier), 0.001)
}
}
Each @CsvSource row runs as its own test, with its own pass/fail and its own name in the
report (name = "{0} tier -> {1}% off" substitutes the row's actual values) — one method,
three real, separately-reported test cases.
Full file: examples/project-layout/src/test/kotlin/com/stepshala/billing/DiscountCalculatorParamTest.kt.
Two more small, standalone examples of the same two patterns — a plain @Test suite
(examples/standalone-password-strength-test.kt) and a second @ParameterizedTest/@CsvSource
suite on a different boundary-value function (examples/standalone-grade-boundaries-test.kt) —
live directly in examples/, outside the project-layout/ folder, each compiled and run with a
flat kotlinc invocation against the same three jars section 7 downloads (their file headers
have the exact commands); both were verified the same way as project-layout itself.
Done here? The practice set for Part 11 — including the solutions — is on the part page.