Debugging Tips
Debugging Problems
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
- Understand what you want to do. This will allow you to establish the logic and write appropriate tests cases.
- 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:
- Unit Tests: Focuses on individual units or components of a program (e.g., function). The goal is to test each unit in isolation.
- Functional Tests: Focuses on the entire system or a larger part of it. Goal is to validate the overall functionality and behaviour of the program. This often involves multiple components working together.
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?
Understand what you want to achieve with the test cases. Identify the specific functionality or behavior you are testing.
Inputs: Define the inputs or conditions that will be used to test the functionality. This includes the data, parameters, or states that the system will encounter during testing.
Outputs: Determine the expected outcomes or results for each test case. This involves defining the expected values or behaviors based on the defined inputs.
Write Test Cases: Document each test case with the following components:
- Test Case Name/ID: A unique identifier or name for the test case.
- Description: A brief description of the test case, explaining the purpose and scenario being tested.
- Input Data: Specify the input data or conditions for the test case.
- Expected Output: Define the expected output or result for the given inputs.
- Steps to Reproduce: Detailed steps on how to reproduce the test scenario.
- Preconditions: Any specific conditions that must be met before running the test.
- Postconditions: The expected state or conditions after the test has been executed.
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.
An assertion error happens when python encounters an assertion statement. Python evaluates the expression to the right of the word assert; if that expression is True everything is fine and the program continues. If the expression is False Python raises an error and stops.
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