0
0
Kotlinprogramming~15 mins

Why testing matters in Kotlin - See It in Action

Choose your learning style9 modes available
Why testing matters
📖 Scenario: Imagine you are building a simple calculator app that adds two numbers. You want to make sure your addition works correctly every time, even if you change your code later.
🎯 Goal: You will create a small Kotlin program that adds two numbers and write a simple test function to check if the addition works correctly. This will show why testing your code is important.
📋 What You'll Learn
Create two variables with exact values
Create a function to add two numbers
Create a test function to check the addition
Print the test result
💡 Why This Matters
🌍 Real World
Testing helps developers make sure their apps work correctly before users see them. It saves time and avoids bugs.
💼 Career
Writing tests is a key skill for software developers. It shows you can write reliable and maintainable code.
Progress0 / 4 steps
1
DATA SETUP: Create two variables with exact values
Create two variables called num1 and num2 with values 5 and 7 respectively.
Kotlin
Need a hint?

Use val to create variables and assign the numbers 5 and 7.

2
CONFIGURATION: Create a function to add two numbers
Create a function called add that takes two Int parameters named a and b and returns their sum.
Kotlin
Need a hint?

Define a function with fun keyword and return the sum of a and b.

3
CORE LOGIC: Create a test function to check the addition
Create a function called testAdd that calls add(num1, num2) and checks if the result equals 12. Store the check result in a variable called testPassed.
Kotlin
Need a hint?

Inside testAdd, call add(num1, num2) and compare it to 12 using ==.

4
OUTPUT: Print the test result
Inside the testAdd function, add a line to print "Test passed!" if testPassed is true, otherwise print "Test failed!". Then call testAdd().
Kotlin
Need a hint?

Use an if statement to print the correct message based on testPassed. Then call testAdd() to run the test.