ICT582 Topic 08
ICT582 Topic 08
www.codemasterinstitute.com
2
This Topic
§ Testing
§ Debugging
§ Catch exceptions
§ Raise exceptions
§ Assertions
§ Defensive programming
www.codemasterinstitute.com
3
Definitions
www.codemasterinstitute.com
4
Defensive Programming
Testing/Validation Debugging
§ Compare input/output § Study events leading to
pairs to specification an error
– "it's not working" – "Why is it not working?"
– "How can I break my – "How can I fix my
program?" program?"
www.codemasterinstitute.com 5
What You Should Do
www.codemasterinstitute.com
6
When Are You Ready to Test?
www.codemasterinstitute.com
7
Types of Tests
§ Unit testing
– validate each piece of program
– test each function separately
§ Regression testing
– add test for bugs as you find them
– catch reintroduced errors that were previously fixed
§ Integration testing
– does overall program work?
– Tend to rush to do this
www.codemasterinstitute.com
8
Testing Methods
www.codemasterinstitute.com
10
Black Box Testing
def sqrt(x, eps):
""" Assumes x, eps floats, x >= 0, eps > 0
Returns res such that x-eps <= res*res <= x+eps """
CASE x eps
boundary 0 0.0001
perfect square 25 0.0001
less than 1 0.05 0.0001
irrational square root 2 0.0001
negative number -4 0.0001
negative eps 35 -0.001
extremes 2 1.0/2.0**64.0
extremes 1.0/2.0**64.0 1.0/2.0**64.0
extremes 2.0**64.0 1.0/2.0**64.0
extremes 1.0/2.0**64.0 2.0**64.0
extremes 2.0**64.0 2.0**64.0
www.codemasterinstitute.com
11
Glass Box Testing
def abs(x):
""" Assumes x is an int
Returns x if x>=0 and –x otherwise """
if x < -1:
return –x
else:
return x
www.codemasterinstitute.com
13
Debugging
www.codemasterinstitute.com
14
Print Statement
www.codemasterinstitute.com
15
Debugging Steps
www.codemasterinstitute.com
16
Error Message - Easy
www.codemasterinstitute.com
18
Logic Errors - Hard
DON’T DO
• Write entire program • Write a function
• Test entire program • Test the function, debug the function
• Debug entire program • Write a function
• Test the function, debug the function
• *** Do integration testing ***
19
Exceptions
www.codemasterinstitute.com
21
Why Exception Handling?
www.codemasterinstitute.com
24
Why Exception Handling?
Note: the code in an except clause is executed only if an exception is raised in the
try block. If an exception is raised but it is not ValueError or
ZeroDivisionError, the code in the last except clause is executed.
www.codemasterinstitute.com
28
The else and finally Clause
try:
statements
except:
statements
else:
statements (executed only if there is no exception)
finally:
statements (always executed, used to clean-up
code that should be run no matter what else
happened, eg, close a file)
www.codemasterinstitute.com
29
What to Do with an Error?
www.codemasterinstitute.com
30
Raise an Exception
m a s ut
sa ng
or
ra r y
b
yw
es tri
ge
th lly al,
to ro
ke
nt f er
wi pica ion
ty opt
wa e o
m
a
na
§ Example:
raise ValueError("something is wrong")
www.codemasterinstitute.com
31
Example: Raise an Exception
def get_ratios(L1, L2):
""" Assumes: L1 and L2 are lists of equal length of numbers
Returns: a list containing L1[i]/L2[i] """
ratios = []
for i in range(len(L1)):
try:
ratios.append(L1[i]/L2[i])
except ZeroDivisionError:
ratios.append(float('nan')) # nan = not a number
except:
raise ValueError("get_ratios called with a bad arg")
return ratios
www.codemasterinstitute.com
32
Assertions
www.codemasterinstitute.com
34
Where to Use Assertions
§ Testing
§ Debugging
§ Catch exceptions
§ Raise exceptions
§ Assertions
§ Defensive programming
www.codemasterinstitute.com
36
Acknowledgement
§ geekforgeek
§ Python Tutor
§ MIT OCW
www.codemasterinstitute.com
37