Kotlin test assertions - Time & Space Complexity
We want to understand how the time it takes to run Kotlin test assertions changes as the number of tests or checks grows.
How does adding more assertions affect the total time to run them all?
Analyze the time complexity of the following code snippet.
fun testSum() {
val numbers = listOf(1, 2, 3, 4, 5)
val result = numbers.sum()
assert(result == 15) { "Sum should be 15" }
}
fun testAll() {
testSum()
testSum()
testSum()
}
This code runs a simple sum test three times, each with one assertion checking the result.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The sum() function loops through the list to add numbers.
- How many times: sum() runs once per testSum call, and testSum is called three times.
As the number of tests or assertions increases, the total work grows proportionally.
| Input Size (number of tests) | Approx. Operations |
|---|---|
| 10 | 10 sums and 10 assertions |
| 100 | 100 sums and 100 assertions |
| 1000 | 1000 sums and 1000 assertions |
Pattern observation: Doubling the number of tests doubles the total operations.
Time Complexity: O(n * m)
This means the time grows with both the number of tests (n) and the size of data each test processes (m).
[X] Wrong: "Assertions run instantly and don't affect time complexity."
[OK] Correct: Each assertion checks a condition, so more assertions mean more checks and more time.
Understanding how test assertions scale helps you write efficient tests and reason about test suite performance in real projects.
"What if each test had multiple assertions instead of one? How would the time complexity change?"
Practice
Solution
Step 1: Understand assertion types
assertEqualsis used to check if two values are equal.Step 2: Match assertion to purpose
assertTruechecks if a condition is true,assertNullchecks for null, andassertFailschecks for exceptions.Final Answer:
assertEquals(expected, actual) -> Option AQuick Check:
Equality check = assertEquals [OK]
- Confusing assertTrue with assertEquals
- Using assertNull for equality checks
- Using assertFails to check values
Solution
Step 1: Recall Kotlin assertion syntax
assertTruetakes a Boolean condition as a parameter inside parentheses.Step 2: Identify correct syntax
OnlyassertTrue(condition)is valid Kotlin syntax; others are incorrect.Final Answer:
assertTrue(condition) -> Option CQuick Check:
assertTrue uses parentheses for condition [OK]
- Using curly braces instead of parentheses
- Omitting parentheses
- Using colon instead of parentheses
val result = 5 assertEquals(5, result)
Solution
Step 1: Check values compared
The expected value is 5 and actual value stored inresultis also 5.Step 2: Understand assertEquals behavior
Since both values are equal,assertEqualspasses without throwing an error.Final Answer:
Test passes without error -> Option AQuick Check:
Equal values = test passes [OK]
- Assuming assertEquals throws error on equal values
- Confusing expected and actual order
- Thinking assertEquals returns a value
assertEquals(10, 5 + 3)
Solution
Step 1: Calculate actual value
The expression5 + 3equals 8.Step 2: Compare expected and actual
Expected is 10 but actual is 8, soassertEqualsfails and throws AssertionError.Final Answer:
Test fails because expected and actual differ -> Option DQuick Check:
Different values = test fails [OK]
- Assuming test passes despite value mismatch
- Thinking syntax error occurs
- Confusing runtime exceptions with assertion failures
Solution
Step 1: Understand exception testing
To check if a function throws an exception, useassertFailswith a lambda calling the function.Step 2: Evaluate other options
assertEqualscompares values, not exceptions;assertNullchecks for null;assertTruechecks a Boolean condition.Final Answer:
assertFails { function(invalidInput) } -> Option BQuick Check:
Exception test = assertFails [OK]
- Using assertEquals to check exceptions
- Using assertNull expecting exception
- Using assertTrue for exception testing
Ready to prove it?
Pass the test and this pattern moves unknown
