comp10001 foundations of computing testing and debugging · comp10001 foundations of computing week...

29
COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) COMP10001 Foundations of Computing Testing and Debugging Semester 1, 2017 Tim Baldwin & Egemen Tanin version: 1103, date: March 30, 2017 © 2017 The University of Melbourne

Upload: others

Post on 28-Mar-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

COMP10001 Foundations of Computing

Testing and Debugging

Semester 1, 2017Tim Baldwin & Egemen Tanin

— version: 1103, date: March 30, 2017 —

© 2017 The University of Melbourne

Page 2: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Reminders

• Grok Worksheets 8–11 due at the end of thisweek

• Advanced/revision lecture tomorrow; revisionlecture to be held in:

• Herbert Wilson Theatre, Doug McDonell (9–10)• Latham Theatre, Redmond Barry (3:15–4:15)

• Project 1 due on Friday of next week

Page 3: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Lecture Agenda

• Last lecture:• PEP8• Modules• File access

• This lecture:• Python tips/tricks• Testing and debugging• Commenting

Page 4: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Lecture Outline

1 How Far have We Come?

2 Python Tips/Tricks

3 Software Bugs and Debugging

4 Testing

Page 5: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Remember This?

Source(s): http://laughingsquid.com/hey-jude-flow-chart/

Page 6: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Lecture Outline

1 How Far have We Come?

2 Python Tips/Tricks

3 Software Bugs and Debugging

4 Testing

Page 7: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

defaultdict I

• When using dictionaries as “accumulators” youneed to initialise every value for new keys ...

def count_digits(num):

'''Count the digits in number num.

'''digit_count = {}

for digit in str(num):

if digit in digit_count:

digit_count[digit] += 1

else:

digit_count[digit] = 1

return digit_count

Page 8: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

defaultdict II

• Alternatively, simplify your life with defaultdict:

from collections import defaultdict

def count_digits(num):

''' Count the digits in a number '''digit_count = defaultdict(int)

for digit in str(num):

digit_count[digit] += 1

return digit_count

Page 9: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Python Tips: List Comprehensions

• We are often constructing lists in Python usingfor loops, e.g.:

mylist = []

for key , val in mydict.items ():

mylist.append(val , key)

• A “list comprehension” allows us to do this in asingle line:

[val , key for key , val in mydict.items ()]

• It also allows us to filter elements in a list:

[i for i in range (-9,10,2) if not (i%3)]

Page 10: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Python Tips: Assignment

• We often apply some operation to a variable andassign the result back to that variable:

i = i - 1

j = j * 2

mystr = mystr + letter

There is a family of convenient “shorthands” forthis, one for each binary operator:

i -= 1

j *= 2

mystr += letter

Page 11: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Lecture Outline

1 How Far have We Come?

2 Python Tips/Tricks

3 Software Bugs and Debugging

4 Testing

Page 12: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Bugs

• A (software) “bug” is an error/flaw in a piece ofcode that leads to a malfunction

• The first attested computer “bug” (GraceCooper, Harvard Mark II):

• So what’s the big deal?

Page 13: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

A Bug in Action: Mars Climate Orbiter

• Ideal: establish an orbit aroundMars, and study the weather,climate, etc of Mars in tandemwith the Mars Polar Lander

• Actuality: attempted to orbit toolow and crashed as a result

• Cause: metric vs. Imperialconversion in calculations

• Cost: US$165m

Page 14: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Other Famous Bugs

• Y2K

• HAL 9000 (2001: A Space Odyssey)

• Estimate that software bugs cost the USeconomy 0.6% of the GDP

• Over 50% of the development cost of software ison testing and debugging

• No general way of “proving” that a given pieceof software implements a given spec

Page 15: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Debugging

• Bugs are inevitable:• Fact: even the most carefully-engineered software will

include at least 5 errors/1000 lines of code• Fact: Windows 10 contained 50–60M lines of code ...

• Bug/error types:• syntax errors = incompatibility with the syntax of

the programming language• run-time errors = errors at run-time, causing the

code to crash• logic errors = design error, such that the code runs

but doesn’t do what it is supposed to do

• Debugging = the process of systematicallyfinding and fixing bugs

Page 16: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Class Exercise: Spot and Fix the Bugs

Spot and fix the bug(s) in the following code, andclassify each as a syntax, run-time or logic error:

1 def substrn(sup , sub)

2 sub_len = len(Sub)

3 for i in range(len(sup) - sub_len ):

4 if sup[i:i + sub_len] == sub:

5 n += 1

6 print("n")

Page 17: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Comments

Job much easier if there was a description of what thefunction should do.

1 def substrn(sup ,sub)

2 '''3 Return the number of times `sub `4 occurs in `sup `5 '''6 sub_len = len(Sub)

7 for i in range(len(sup)-sub_len ):

8 if sup[i:i+sub_len] == sub:

9 n += 1

10 print("n")

Page 18: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Functions and Docstring-styleCommenting I

• A docstring is a string literal that occurs as thefirst statement in a module, function, class, ormethod definition. Such a docstring becomes the__doc__ special attribute of that object.

def Celcius2Fahrenheit(n):

""" Return the (float)

Fahrenheit equivalent

of a temperature in Celcius """

return 9.0*n/5 + 32

Page 19: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Functions and Docstring-styleCommenting II

• It is possible to access the __doc__ for a functionvia help, e.g. given:

def seconds_in_year(days =365):

""" Calculate seconds in a year """

return days *24*60*60

>>> help(seconds_in_year)

Help on function seconds_in_year in module __main__:

seconds_in_year(days =365)

Calculate seconds in a year

Page 20: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Comments

7 Wrong:

def f(x):

'''This is a function of parameter x

that calculates the length of x

squared.'''return len(x)**2

Don’t describe Python syntax; the reader knowsPython:3 Right:

def len_sqr(x):

'''Return the square of the length of x.'''return len(x)**2

Page 21: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Comments

7 Wrong:

c = 0 # a variable to count

for i in word: # a loop

if i in 'aeiou ': # is i in aeiou

c += 1 # add one to count

Succint description of logic for a block in English.Meaningful variable names help readability.3 Right:

# set `count ` to no. of vowels in `word `count = 0

for character in word:

if character in 'aeiou ':count += 1

Page 22: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Comments

# set `count ` to no. of vowels in `word `count = 0

for character in word:

if character in 'aeiou ':count += 1

Use functions.

def count_vowels(word):

'''return the no. of vowels in `word `'''count = 0

for character in word:

if character in 'aeiou ':count += 1

return count

Page 23: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Prevention Rather than Cure:Defensive Programming

• Build up your code bit by bit, using functionscopiously, testing as you go against knowninputs/outputs

• “Log” each step of your progress

• Use comments to remind you about anyassumptions made by the code/corners cut alongthe way

• Always be on the lookout for common gotchas

• Above all, remember that the program code mustcommunicate with humans, not just machines

Page 24: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Common Python Gotchas

• Equality (==) vs. assignment (=)

• Printing vs. returning from functions

• Correct use of types (e.g. False vs. "False")

• Incorrect use of function/method (e.g.return list.sort())

• Spelling and capitalisation

• Loops and incrementing

• Conditionals and indentation

• Namespace problems

Page 25: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

A General Approach to Debugging

• Reproduce the bug

• Determine exactly what the problem is

• Eliminate “obvious” causes (e.g. Is it pluggedin? )

• Divide the process, separating out the parts thatwork from the part(s) that don’t (“isolate” theproblem)

• When you reach a dead end, reassess yourinformation; then step through the process again

• As you proceed, make predictions about whatshould happen and verify the outcome

Page 26: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Lecture Outline

1 How Far have We Come?

2 Python Tips/Tricks

3 Software Bugs and Debugging

4 Testing

Page 27: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

The Actuality of Software Testing

• Execution-based verification:• generate and execute test cases, and check the

correctness of the output• generally impossible to enumerate all possible

inputs/use cases, so instead focus on developing a setof “representative” inputs to test the code over

• in addition to “integration” testing (between systemcomponents), “unit” test the components of thesystem

• Non-execution-based verification• detect bugs by eyeballing the code directly• e.g., code review or pair programming

Page 28: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Test Cases

• Test cases should be designed independently ofthe software implementation, and (ideally) bedesigned to:

• test one thing each (e.g. one use case per input type)• test over the spectrum of use cases for the software

(often based on the “boundaries” of inputs)• (in part) identify and test “corner case” inputs

Page 29: COMP10001 Foundations of Computing Testing and Debugging · COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017) The Actuality of Software Testing • Execution-based

COMP10001 Foundations of Computing Week 5, Lecture 2 (30/3/2017)

Lecture Summary

• What is defaultdict and why is it useful?

• What are list comprehensions and why are theyuseful?

• What do += and *= mean?

• What are docstrings?

• What is a bug/debugging?

• Why do bugs occur?

• What different types of bugs are there?

• What is the basic procedure for identifying bugs?