Saltar al contenido
Back to home
Session 06 Python 2025-2026 1º

Testing, debugging and code quality

Introduction to debugging techniques, basic testing, and best practices to improve code quality, maintainability, and reliability.

🎯 Objectives

  • Learn how to identify and fix errors (debugging)
  • Introduce basic testing in Python
  • Improve code quality and readability
  • Apply real-world best practices

🧠 Key Concepts

🔍 Debugging

  • Reading errors (tracebacks)
  • Strategic use of print()
  • Basic use of pdb (Python debugger)

✅ Basic Testing

  • What is a test
  • Manual vs automated tests
  • Introduction to assert
def suma(a, b):
    return a + b

assert suma(2, 3) == 5
assert suma(-1, 1) == 0

⚠️ Error Handling (review + improvement)

try:
    numero = int(input("Enter a number: "))
except ValueError:
    print("Invalid input")

Best practices:

  • Catch specific exceptions
  • Avoid generic except:

🧼 Code Quality

Basic principles:

  • Clear naming (user_age better than x)
  • Small, reusable functions
  • Avoid code duplication

📏 Style (PEP8)

Example:

def calcular_media(numeros):
    return sum(numeros) / len(numeros)

Recommended tools:

  • black (auto-formatting)
  • flake8 (style checking)

🛠️ Practical Exercise

  1. Given a simple program:

    • Identify bugs
    • Fix them
  2. Add:

    • Input validation
    • At least 3 tests using assert

🚀 Expected Outcome

  • Cleaner and more structured code
  • Ability to detect errors independently
  • First contact with real testing practices

📌 Task

  • Improve a previous program by adding:

    • Validation
    • Error handling
    • Basic tests