Introduction to Problem Solving and Programming in Python http://cis-linux1.temple.edu/~tuf80213/courses/temple/cis1051/
Overview Types of errors Testing methods Debugging in Python 2
Errors An error in a program is called a bug The process of finding and removing bugs is called debugging There are 3 kinds of errors in programming Syntax errors Runtime errors Logical errors 3
Syntax Errors Arise from grammatical mistakes in your program When the code written does not follow the syntactical rules of the language (Relatively) Easy to find and fix Immediately caught when the offending line is interpreted (or compiled) Many times reported by your IDE Example using a semicolon where you should use a comma 4
Syntax errors: incorrect names Names in Python (variables, functions) have rules for naming Valid characters are: A-Z a-z 0-9 _ Cannot begin with a number Cannot use a Python keyword (reserved word) 5
Python Keywords False None True and as assert break class continue def del elif else except finally for from global if import in is lambda nonlocal not or pass raise return try while with yield 6
Runtime Errors An error that is detected while the program is running. Caused by a statement that cannot be executed. Causes the program to terminate (Crash) Example dividing by zero 7
Logical Errors An error that is detected while the program is running. Semantic error. The program continues to run, but causes incorrect results Example average = a + b / 2 8
Required debugging effort Syntax error - Fixed before anyone even notices Runtime error - Inconvenient, but we can find and fix Logical error - Can be measured in hair pulled 9
Debugging The process of finding and removing errors in code Takes place when a bug is found (not proactive) Bugs can be found/triggered through Testing 10
Testing A process to show that the software runs as expected Can be manual or automated process Code is run with test cases Predetermined set of inputs with known results Tests are grouped (e.g. boundary testing checking the edge cases of valid input) 11
Testing cont. Testing can prove that a program has bugs, but it cannot prove that a program is bug-free Testing all possible inputs under all possible conditions is infeasible at best, impossible at worst e.g. A program that calculates daily loan interest payments Programmer uses 365 (days) as base of calculation Program is tested in a non-leap year against expected results and are correct During leap year, calculations are incorrect 12
Types of testing Black box System details are hidden White box Internal logic is visible tests code coverage, etc. Unit Test individual components or modules 13
Types of testing cont. Integration Tests if various components work together System Tests entire system (every combined portion) Regression Testing the entire application if a single component is changed Acceptance Performed by the customer. Ensures the application meets specifications. 14
Testing and debugging in Python Tracing Assert Debuggers 15
Tracing The process of recording the program s state after each statement is executed Involves keeping track of variables through various assignments Often times, variable values are printed to the screen to check their values Be sure to remove these print statements when debugging is complete 16
The assert keyword An assertion is a statement to Python that a particular condition should be true at this point in the program Useful in preventing logical errors Syntax: assert condition[, expression] e.g.: assert balance - withdrawal_amount >= 0 balance -= withdrawal_amount 17
The Python debugger pdb (Python DeBugger) is an interactive source code debugger for Python It support breakpoints and single stepping inspecting variable values source code listing executing arbitrary code in context 18
Using pdb 1. Add the following line of code at the beginning of your program import pdb 2. Place pdb.set_trace() At the location where you would like tracing to begin 3. Run your program from the command line PROMPT> python my_program.py 19
Using IDE debugger 1. Some IDEs come with their own debugger 2. May be more intuitive (e.g. GUI interface) 3. May not play well with pdb. Use one or the other. 20