Mini-Lecture 12 Debugging
Testing last_name_first(n) # test procedure Call function def test_last_name_first(): on test input """Test procedure for last_name_first(n)""" result = name.last_name_first('Walker White') Compare to expected output introcs.assert_equals('White, Walker', result) result = name.last_name_first('Walker White') introcs.assert_equals('White, Walker', result) Call test procedure # Script code to activate the test test_last_name_first() print('Module name is working correctly') 9/21/18 Debugging 2
Types of Testing Black Box Testing White Box Testing • Function is “opaque” • Function is “transparent” § Test looks at what it does § Tests/debugging takes place inside of function § Fruitful : what it returns § Focuses on where error is § Procedure : what changes • Example : Use of print • Example : Unit tests • Problems : • Problems : § Much harder to do § Are the tests everything? § Must remove when done § What caused the error? 9/21/18 Debugging 3
Finding the Error • Unit tests cannot find the source of an error • Idea: “Visualize” the program with print statements def last_name_first(n): """Returns: copy of <n> in form <last>, <first>""" end_first = n.find(' ') Print variable after print(end_first) each assignment first = n[:end_first] print('first is '+str(first)) Optional : Annotate last = n[end_first+1:] value to make it easier to identify print('last is '+str(last)) return last+', '+first 9/21/18 Debugging 4
Conditionals and Debugging # Put max of x, y in z • Must understand which branch caused the error print('before if') § Unit test produces error if x > y: § Visualization tools show print('if x>y') the current flow for error z = x • Visualization tools? else : § print statements print('else x<=y') § Advanced tools in IDEs z = y (Integrated Dev. Environ.) print('after if') 9/21/18 Debugging 5
Conditionals and Debugging # Put max of x, y in z • Call these tools traces print('before if') • No requirements on how if x > y: to implement your traces print('if x>y') § Less print statements ok z = x § Do not need to word them Traces exactly like we do else : § Do what ever is easiest print('else x<=y') for you to see the flow z = y • Example : flow.py print('after if') 9/21/18 Debugging 6
Watches vs. Traces Watch Trace • Visualization tool • Visualization tool (e.g. print statement) (e.g. print statement) • Looks at variable value • Looks at program flow • Often after an assignment • Before/after any point where flow can change • What you did in lab 9/21/18 Debugging 7
Traces and Functions print('before if') Example : flow.py if x > y: print('if x>y') z = y print(z) Watches Traces else : print('else x<=y') z = y print(z) print('after if') 9/21/18 Debugging 8
Recommend
More recommend