0
0
Swiftprogramming~15 mins

Why testing matters in Swift - 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 program that adds two numbers and then write a test to check if the addition is correct. This shows why testing your code is important.
📋 What You'll Learn
Create two variables with exact values
Create a variable to hold the sum
Write a simple test using an if statement to check the sum
Print the test result exactly as specified
💡 Why This Matters
🌍 Real World
Testing is used in all software projects to make sure programs work correctly before people use them.
💼 Career
Knowing how to write tests is important for software developers to deliver reliable and bug-free applications.
Progress0 / 4 steps
1
Create two number variables
Create two variables called number1 and number2 with values 5 and 7 respectively.
Swift
Need a hint?

Use let to create constants for the numbers.

2
Add the two numbers
Create a variable called sum that adds number1 and number2.
Swift
Need a hint?

Use the + operator to add the two numbers.

3
Write a test to check the sum
Write an if statement that checks if sum equals 12. Inside the if, create a variable called testResult and set it to "Test passed". Otherwise, set testResult to "Test failed".
Swift
Need a hint?

Use if sum == 12 to compare and set testResult accordingly.

4
Print the test result
Write a print statement to display the value of testResult.
Swift
Need a hint?

Use print(testResult) to show the test result.