Debugging Tips

Debugging Problems

Practice Incremental Development

The goal of incremental development is to avoid long and complex debugging sessions by adding and testing only a small amount of code at a time.

Steps for Incremental Development

  1. Understand what you want to do. This will allow you to establish the logic and write appropriate tests cases.
  2. Write code and continuously validate by running unit tests.

Writing Tests Cases

A test case refers to the individual unit of testing. Each test case validates the functionality of a program.

Types of Tests Cases:

Running the tests can help to identify situations where a change in code in one place breaks the correct operation of some other code.

How do we write tests cases?

Testing with Assert Statement

Python provides a statement called assert  that can be used when debugging code.

The assert keyword lets you test if a condition in your code returns True, if not, a runtime error will occur and the program will raise an AssertionError.

It is typically employed in scenarios where you want to check that certain conditions hold true during the execution of your code.

Basic Syntax

assert expression[, message] # [] to indicate that 'message' is not mandatory

Example:

# Without message
x = 10
assert x > 5  # If x is not greater than 5, an AssertionError is raised with no specific message

# With message
y = 3
assert y > 5, "Value of y should be greater than 5"  # If y is not greater than 5, an AssertionError is raised with the specified message
Powered by Forestry.md