CodingNic

Testing and Best Practices

Why Testing Matters

Testing and Best Practices 24 min read

Why Testing Matters

Why Testing Matters

Writing code is only part of programming.

You also need to know if your code works correctly.

That is where testing helps.

What Is Testing?

Testing means checking that your program gives the expected result.

You compare:

  • expected output
  • actual output

If they match, the code works for that case.

Why Testing Is Important

Testing helps you:

  • catch bugs early
  • prevent broken features
  • save debugging time
  • improve confidence
  • make changes safely

Example Without Testing

python
def add(a, b):
    return a - b

The function should add, but it subtracts.

Without testing, the bug may go unnoticed.

Manual Test

python
def add(a, b):
    return a - b

print(add(2, 3))

Output

text
-1

Expected result:

text
5

The test shows the function is wrong.

Fix the Code

python
def add(a, b):
    return a + b

print(add(2, 3))

Output

text
5

Test Many Cases

Good code should work for different inputs.

python
print(add(1, 1))
print(add(0, 5))
print(add(-2, 3))

Output

text
2
5
1

Real Example

If a payment function fails, users may be charged incorrectly.

Testing helps prevent serious problems.

Testing Types (Simple View)

  • Manual testing = run code yourself
  • Automated testing = code checks code

You will learn automated testing soon.

Code Along

Create a function:

python
def square(n):
    return n * n

Test it with:

  • 2
  • 3
  • 5

Mini Challenge

This function is wrong:

python
def double(n):
    return n + n + 1

Tasks:

  • Test with 4
  • Find the bug
  • Fix it

Expected output after fix:

text
8

Real World Use Case

Companies test login systems, payments, reports, APIs, and mobile apps every day.

Quiz

  1. What is testing?
  2. Why is testing useful?
  3. What is the difference between expected and actual output?
  4. What can happen if code is not tested?

Assignment

Create a cube(n) function and test it with three different values.

Summary

You learned why testing matters and how tests help find bugs before users do.