Unit 5: Code Testing and Debugging — Short Questions
12th Class Computer Science · Unit 5: Code Testing and Debugging
Testing checks whether an application meets its requirements and ensures that all functions produce correct results. It reduces the risk of crashes or incorrect outputs and verifies that changes do not break existing features. Applications that are thoroughly tested are more trustworthy and stable, which increases user confidence.
Syntax errors occur when code does not follow the language's rules (e.g., a missing colon), causing the program to fail before it runs. Logic errors occur when the program runs but produces an incorrect result because the formula or logic used is wrong. Runtime errors occur while the program is running, such as a ZeroDivisionError when dividing by zero. Other common bugs include data errors and resource leaks.
Unit testing is the process of testing individual parts or units of a program, such as single functions, to ensure they work correctly. It is important because it helps catch errors early, makes debugging easier, and checks whether each component produces the expected output, which improves code reliability.
unittest is a built-in Python module that provides a framework for creating test cases, while pytest is an external module that simplifies writing and running tests using simple functions and the assert statement without needing test case classes. Both modules help verify that code behaves as expected by testing individual functions, classes, or entire modules, which improves program stability and reduces errors in larger applications.
A test case defines specific inputs and the expected output for a function, for example testing a calculator function for addition and subtraction. Executing test cases allows Python to report failures or errors, and repeated testing ensures that changes in code do not break existing functionality, saving time and improving accuracy in development.
In PyCharm (and similarly VS Code), a breakpoint is set by clicking the line number next to the code in the margin. When the program runs and reaches that line, execution pauses, allowing the programmer to inspect variable values and see the current state of the program.
Watch expressions track the values of chosen variables while the program is running and update automatically whenever those values change. This helps the programmer identify incorrect or unexpected data during execution and observe how data changes step by step in real time.
Step-by-step debugging executes code one line or function at a time. In an IDE the programmer can Step Into a function to see how it works internally, Step Over a line to execute it without entering any function it calls, or Continue execution until the next breakpoint. This makes it easier to follow the program's logic and locate bugs accurately.
Python allows catching multiple exceptions within one try-except block, so each type of exception can be handled with its own specific response. This approach prevents unexpected crashes and ensures proper error recovery, which improves program stability and the programmer's control over errors.
Python provides the cProfile module, which generates a profiling report showing how much time each function takes during execution, and the timeit module, which is used to compare the execution time of different pieces of code (for example, comparing a slow manual-loop sum with the fast built-in sum() function).