Concept Flow - Why testing matters
Write code
Run tests
Tests pass?
No→Fix bugs
Re-run tests
Code works well
Confident release
This flow shows how writing code, running tests, fixing bugs, and passing tests lead to confident, working software.
fun add(a: Int, b: Int): Int { return a + b } fun testAdd() { assert(add(2, 3) == 5) }
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Call add(2, 3) | 2 + 3 | 5 |
| 2 | Check if add(2, 3) == 5 | 5 == 5 | True |
| 3 | Test passes | assertion holds | No error |
| 4 | Code works as expected | - | - |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| a | - | 2 | 2 | 2 |
| b | - | 3 | 3 | 3 |
| result | - | 5 | 5 | 5 |
| testPass | false | false | true | true |
Why testing matters: - Write code and then run tests - Tests check if code works correctly - If tests fail, fix bugs and re-test - Passing tests mean code is reliable - Testing builds confidence before release
testSum?fun, followed by the function name and parentheses.fun testSum() { ... }.fun testMultiply() {
val result = 3 * 4
if (result == 12) {
println("Test passed")
} else {
println("Test failed")
}
}
testMultiply()result is assigned 3 * 4, which equals 12.result == 12 is true, so it prints "Test passed".fun testIsEven() {
val number = 5
if (number % 2 = 0) {
println("Even")
} else {
println("Odd")
}
}
testIsEven()number % 2 equals 0.calculateDiscount returns the correct discount for a price. Which approach best shows why testing matters?calculateDiscount(100) and checks if the result is 10. calls the function and checks the result, which helps find mistakes and trust the code.Pass the test and this pattern moves unknown