# 7. Testing, Debugging, Exceptions, and Assertions

**Channel:** MIT OpenCourseWare
**Source:** https://www.youtube.com/watch?v=9H6muyZjms0
**Transcript page:** https://www.withtranscript.ai/video/9H6muyZjms0

## Chapters

- 0:00 — Chapter 1 (0:00)
- 8:50 — Chapter 2 (8:50)
- 18:37 — Chapter 3 (18:37)
- 28:21 — Chapter 4 (28:21)
- 35:20 — Chapter 5 (35:20)

## Transcript

**[0:00]:** the following content is provided under a Creative Commons license your support will help MIT open courseware continue to offer highquality educational resources for free to make a donation or view additional materials from hundreds of MIT courses visit MIT opencourseware at

**[0:28]:** ocw.mit.edu all right everyone good afternoon let's get started so today's lecture will be on testing debugging and then exceptions and assertions so before we begin let's start with an analogy to sort of come back to real life for a second so I've made soup before perhaps you've made soup before let's say you're making soup in this big pot here and it turns out that bugs keep falling into your soup from the ceiling all right quick just question to the audience what do you do if you encounter this issue all right hands up one one at a time anyone have any idea yeah eat it eat it you want to eat it anyway okay all right we're going for an analogy here with computer programming I don't know what You' do if you have a buggy program I guess you just release it to the customer and they'd complain but okay what else yeah cover the

**[1:26]:** soup cover the soup that's a good suggestion yeah so you can cover the soup so put a lid on it sometimes you'd have to open uh take the lid off right to check to make sure it's done to taste it add things so bugs might fall in in between there but covering the soup is a good idea what else yeah debug it debug it I wish I had something for that answer all right that's a good answer yeah take all so take all the food out of your house so there's nothing for the bugs to eat that's sort of the equivalent of cleaning like doing a mass cleaning of your entire house that's a good that's a good one that's that's sort of eliminating the source of the bugs right what else yeah John decide it's high protein and declare it a feature decide it's high protein and declare and declare it a feature that's probably what a lot of a lot of people would do right all right cool so I wish computer debugging was as fun as taking bugs

**[2:34]:** out of your soup so what did we decide well we could check the soup for bugs keep the lid closed that was a good suggestion and cleaning your kitchen which someone suggested the equivalent of cleaning the kitchen was to just throw out all the food I would take a mop and clean the floor but yeah that works too so we can draw some Parallels for these um for this uh with for this analogy with computer programming so checking the soup is really equivalent to testing right you have you have a soup you think it has bugs in it test it make sure there's no bugs continue on keeping the lid closed it's sort of this idea of defensive programming so make sure that bugs don't fall in in the first place sometimes you have to open open the lid to make sure that the soup is it tastes good or whatever so that's equivalent to

**[3:21]:** defensive programming so try not to have bugs in the first place but they might show up anyway cleaning the kitchen is a limiting source of the bugs in the first place this is actually really really hard to do in programming but you can still try to do it okay so let's talk a little bit about programming so far in 61 or 600 so you expect really that you write a program you maybe do a little debugging and you run the program and it's perfect right you just nailed it but in reality you write this really complex piece of code and you go to run it and and it crashes right it's happened to me many times it's happened to you many times that's the reality okay so today's lecture we'll sort of go over some tips and tricks and debugging and how you can help make your life easier when you're writing programs so you

**[4:17]:** don't end up like this little girl here disappointed beyond belief all right so at the heart of it all is really starting with a defensive programming attitude okay and this comes back back to the idea of decomposition and abstraction that we talked about when we when we started when we did the lecture on functions right so try to start out with to modularize your code right if you write your code in different blocks documenting each different block you're more likely to understand what's happening in your code later on and you'll be able to test it and debug it a lot easier speaking of testing and debugging once you've written a program that's modular you still have to test it and the process of testing is really just coming up with inputs figuring out what outputs you expect and then running your program does the output that the program give match what you expected if it

**[5:17]:** does great you're done but if it doesn't you have to go to this debugging step and this debugging step is the hardest part and it's really just figuring out why the program crashed or why the program didn't give you the answer that you expected it to give okay so as I mentioned the most important thing is to do defensive programming and to that end you want to set yourself up for easy testing and debugging which really comes down to making sure that the code you write is modular so write as many functions as you can document what the functions do document their constraints and it'll make your life a little bit easier later on when you have to debug it when when do you want to start testing well first you have to make sure your program runs so eliminate syntax errors and static semantic errors which by the way python can easily catch for

**[6:09]:** you once you've ensured that a piece of code runs then you want to come up with some test cases so this is pairs of input and output for um for what you expect the program to do once you have your test cases and a piece of code that runs you can start doing tests so there there's three General classes of tests that you can do the first is called unit testing and if you written functions unit testing testing just makes sure that for example each function runs according to the specifications so you do this multiple times as you're testing each function you might find a bug at that point you do regression testing come up with a test case that found that bug and run all of the different pieces of the of your code again to make sure that when you fix the bug you don't reintroduce new bugs into um pieces of the

**[7:05]:** of the code that had already run so you do this this a bunch of times you do a little bit of unit testing a little bit of regression testing and keep doing that at some point you're ready to do integration testing which means test your program as a whole okay does the overall program work so this is the part where you take all of the individual pieces put them together and integration testing tests to make sure that the interactions between all the all of the different pieces works as expected if it does great you're done but if it doesn't then you'll have to go back to unit testing and regression testing and so on so it's really a a um a cycle of testing okay so what are some testing approaches the first and this is probably most common with programs that involve numbers is figuring out some natural boundaries for the numbers for the

**[7:54]:** program sorry so for example if I have a function is bigger and it Compares if x is is bigger than y then some natural boundary given this specification is if x is less than y x is greater than y x is equal to Y maybe throw in less than or equal to or greater or equal to and so on so that's just sort of an intuition about the problem it's possible you have some problems for which there are no natural partitions in which case you might do some random testing and then the more random testing you do the greater the likely that your program is correct but there's actually two more rigorous um ways to do testing and one is blackbox testing and the other one is glass box testing in blackbox testing you're assuming you have the specifications to a function so that's the dock string all you're looking at is the dock string

**[8:50]:** and coming up with some test cases based on that in glassbox testing you have the code itself and you're trying to come up with some test cases that hit upon on all of the possible paths through through the code all right let's look at an example for blackbox testing I'm finding the square root of x to some close enough value given by this Epsilon and the idea here notice I don't actually give you how this function is implemented the idea is that you're just figuring out test cases based on this specification and the great thing about blackbox testing is that whoever implements this function can implement it in whatever way they wish they can use approximation method they can use bisection method it doesn't matter the test cases that you come up with for this function are going to be exactly the same right no matter what the implementation so for this particular function here's

**[9:47]:** a sample set we check the boundary we check perfect squares we can check some number that's less than one we can check maybe irrationals and then you do extreme tests so when either Epsilon is really large or Epsilon is really small or X is really large or X is really small and all the com possible combinations of those okay so the important thing about blackbox testing is that you are doing you are creating the test cases based on the specifications only glassbox testing you're using the code itself to guide your test cases so if you have a piece of code and you come up with a test case that goes through every single possible combination of uh input of uh every single possible path through the code then that test set is called path complete the problem with this is when you encounter Loops for example every single possible path through a loop is maybe

**[10:51]:** the code not going through the loop at all going through once going through twice going through three times four times five times and so on right which could be a very very big test set so instead there are actually some guidelines for when you're dealing with loops and and things like that so for branches when you're doing glassbox testing it's important you should just exercise all of the parts of the conditional so make sure you have a test case that goes through each part of the conditional for four Loops make sure you have a test case where the loop is not entered at all where the loop is entered one time and when the loop is entered just some number more than once for while Loops similar to for Loops except that make sure you have test cases that cover all of the possible ways to break out of the Y Loop so if if

**[11:34]:** the if the Y Loop condition becomes false or if maybe there's a break inside the inside the Y Loop and so on so in this in this example we have the absolute value of x this is a specification and this is the implementation that um that someone decided to to do for this uh function so a path complete test set means that you want to have a a test that goes through each one of these branches so if x is less than minus one well minus two is less than minus one so that's good and otherwise which means pick a number greater than minus one so two so two and minus two are path complete yield to um yield path complete yields a path complete test Suite but notice that while we've hit upon every possible path through this code we've actually missed a test case minus one so this code incorrectly classifies minus one

**[12:36]:** um as returning minus one right which is wrong so in add so for glass box testing in addition to making sure you're going through all the possible paths through the code you also want to make sure you hit upon any boundary condition so in this case for branches minus one is a boundary condition okay so you youve created a test Suite you've tested your program chances are you found a bug what do you do now all right quick sort of detour into a little bit of History uh the history of debugging so 1947 this computer was built and it was a computer that was very impressive for its day it could do things like addition in 0.1 seconds things like multiplication in 7 seconds and take the log of something in 5 seconds okay so faster than a human possibly but pretty slow for today's standards okay and a group of Engineers were working on

**[13:45]:** on uh running a program that found that was supposed to find a trigonometric function and among them being uh this one of the first female scientists Grace Hopper and they found that their program was not working correctly so they went through all of the panels and all of the relays in the computer and they isolated a program in panel F relay 70 where they found this moth just just sitting in there I think it was dead probably electrocuted but it was a moth that was impeding the calculation and don't know if you can read this but this part right here they made a note in their in a log book that says first actual casee of bug being found which I think is really cute so so they were literally doing debugging in this uh in this computer right all right so you won't be doing that sort of debugging you'll be doing a virtual

**[14:44]:** kind of debugging in your programs which again is not that fun but you still have to do it so debugging as you might have noticed so far in your problem sets has a a bit of a steep learning curve right and obviously your goal goal is to have a bug free program and in order to achieve that you have to do the debugging there are some tools which some of you have been using there are some tools built into anaconda or whatever uh ID you've been using um to do to do debugging I know some of you have been using the python tutor which is awesome the print statement can also be a good debugging tool but over above everything else it's really important to just be systematic as you're TR to debug your program I want to talk a little bit about print statements and how you can use them to debug because I

**[15:36]:** I think python tutor you know if you don't have the internet you might not be able to use it um if you don't know how to use the debugger you know you don't need to learn but print statements you'll always have them right and you can always put them in your program and they're really good ways to test hypotheses so good places to put print statements are inside functions inside Loops for example what are the loop parameters what are the loop values what function what functions return what values so you can make sure that values are being passed the correct values are being passed between parts of your code I will mention that you can use the bisection method when you're debugging which is interesting so if you take a print statement find approximately the halfway point in your code print out what values you you uh print out some relevant values all of the

**[16:32]:** possible some some print out some values at that point in your code if everything is as you expect it to be at that point in your code then you're good that means the code so far is bug free that means that however that means that the code Beyond it has a bug right so since you've put a print statement halfway in your code and you think that's and that's that gave good results then put a print statement 3/4 of the way in a code and see if the values are as you expect at that point and if they are great then put a print statement um further down so in this way you could use the bisection method to sort of pinpoint a line or a set of lines or maybe a um a function that that's giving you the bad uh the bad results so the general debugging steps is to study the program

**[17:26]:** code don't ask what is wrong because that's actually part of the testing so your test cases would have figured out what's wrong the debugging process is figuring out how that how the result took place and since programming is or programing and debugging is sort of is a science you can use a scientific method as well so look at all the data that's your test cases figure out a hypothesis maybe say oh maybe I'm indexing from one instead of zero in lists for example come up with an experiment that you can repeat and then pick a simple test case that you can test your hypothesis with so as you're debugging you will encounter error messages and these error messages are actually pretty easy to figure out and they're really easy to fix in your code so for example accessing things beyond the limits of a list give you index errors trying to convert in this case a list to an integer gives you type errors accessing variables that you've haven't created before gives you name errors and so on and so on and syntax errors

**[18:37]:** are things for things like if you forget a parenthesis or forget a colon or something like that okay so error messages are really easy to spot right the python interpreter spits these out for you and then you can pinpoint the exact line logic errors are actually the hard part okay and logic errors are the ones that you will be uh spending the most time on for which I would recommend always trying to take a break take a nap go eat something sometimes you'd have to start all over right so throw out the code you have and just sit down with a piece of paper try to figure out how you want to solve the problem and if you look up the term rubber ducky a lot of heads went up on that one rubber ducky debugging um that is an actual term uh in Wikipedia and it's when a programmer explains their code to rubber

**[19:31]:** ducky that's me on the left explaining code to my rubber ducky you should always you should you should go buy one or code to anyone else preferably someone who doesn't really understand anything because that'll force you to explain everything really really closely okay and as you're doing that you'll figure out your problem and I figured out my problem in both of these cases um so just go back to the basics all right quick summary of dos and don'ts of debugging and testing so don't write the entire program test the entire program and debug the entire program I know this is really tempting to do and I do it all the time but don't do it okay because you're going to introduce a lot of bugs and it's going to be hard to isolate which bugs are affecting other ones and it'll lead to a lot more stress than you need instead do um do unit

**[20:28]:** testing so write one function test the function debug the function you know make sure it works write the other function and so on and so on do a little regression testing a little more unit testing a little integration testing and um it's a lot more systematic way to write the program and it'll cut down on your debugging time immensely if you're changing your code and inevitably you'll be changing your code as you're doing your problem sets remember to back up your code so if you have a version that almost works don't just modify that maybe save a copy SP you've got terabytes of of of memory on your computer it won't hurt to just make a quick copy of it document maybe what worked and what didn't in that copy and then um and then make another copy and then you can modify your code okay so that's sort of a highle introduction to testing

**[21:27]:** and debugging the rest of the class will be on um on on the error messages or or on errors that you'll get in your programs so when your functions when when you run functions or when you run your program at some point the program execution is going to stop right maybe it encountered an error because of some unexpected condition and when that happens you get an exception so the error is called an exception and it's called an exception because it was an exception to what was expected okay to what the program expected so all of these errors that I've talked about in the previous slides are actually examples of exceptions

**[22:12]:** okay and there are actually many other types of exceptions which you'll see um as you go on in this course and also in 602 so how do we deal with these exceptions okay in Python you you can actually have handlers for exceptions so if you know that a piece of code might give you an error for example here I'm dealing with inputs from users and users are really unpredictable right you tell them to give you a number they might give you their name nothing you can do about that or is there yes there is so in your program you can actually put any lines of code that you think might be problematic that might give you an error an exception in this Tri block so you say TR colon and you put in any lines of code that you think might give you an error if if none of these lines of code actually produce an error then great python doesn't do anything else it treats them as just part as just if they were part of a regular program but if an error

**[23:30]:** does come up for example if if someone doesn't put in a number but puts their name in that's going to raise an error specifically a value error and at that point Python's going to say is there an accept statement and if so this accept statement is going to handle the error and it's going to say okay an error came up but I know how to handle it I'm going to print out this user this message to the user so if we look at code this is the same code as in the slides and there's no try except block around it so if I run it and I say three and four it's going to it's going to run fine but if I run it and I say a a it's going to give a value error okay now if I run the same piece of code with try with a try accept block I run

**[24:36]:** it if I give it a regular numbers it's fine but if I'm being a cheeky user and I say three automatically this would have raised the value error in the previous uh version of the of the program but in this version of the program the programmer handled the exception or caught the exception and printed out this nicer looking message so bug and user input is nicer than this this whole lot here right a lot easier to

**[25:19]:** read okay so any problematic uh lines of code you can put in a tri block and then handle um handle whatever errors might come up in this accept block this accept block is going to catch any error that comes up okay and you can actually get a little bit more specific and catch specific types of errors in this case I'm saying if a value error comes up for example if the user inputs a string instead of an integer do this which is going to print this this message if the user in inputs a number for B such that we're dividing a divide we're doing a divid by B so that would give a zero division error in that case we're going to catch this other error here this zero division error and we're going to print this other message can't divide by zero okay so each so you can think of these different except blocks

**[26:18]:** as sort of if you know if else if statements except for exceptions okay so we're going to try this but if there's a value error do this uh otherwise if there's a division error do this and otherwise do this so this last accept is actually going to be for any other error that comes up so if it's not a value error not a nor a division error then we're going to print something went very wrong I don't I couldn't even try to create I couldn't even try to make the program come up with uh any other error besides those two okay so a lot of the time you're just going to use try accept blocks but there's other blocks that you can add to exceptions and these are more rarely used but I'll talk about them anyway so you can have an else block and an else block is going to get executed when the

**[27:14]:** code in the tri block finished without raising an error and you can also have a finally block which is always executed okay if the code in the tri block finished without without an error if you raised an exception if you raised a different kind of exception if you went through the else in any of these cases the whatever is in the finally block is always going to get executed okay and it's usually used to clean up um cleanup code like if you want to print oh the program finished or if you want to close a file or something like that so we've encountered errors we've caught what else can we do with ER with exceptions three other things so one is if we've we've caught an error we can just fail silently okay what this means is you've caught an error and you just substitute whatever erroneous value the user gave you for some

**[28:21]:** other value that's not actually a very good idea that's a bad idea because suddenly L the user thinks that they entered something and they think everything's great your program accepts it but then they get some weird value as an output Which is far from what they expected right so it's not really a good idea to just replace users values with anything okay in the context so this is in the context of a function in the context of a function what else can we do well if you have a function that fails for example let's say you're trying to do um you're trying to get the square root of an even number okay and let's say the user gives you uh sorry you're trying to find the square root of a positive number and let's say the user gives you a negative

**[29:13]:** number well if the user gives you a negative number if your your function could return an error value which means well if the number input is input it is is less than zero then return zero or minus one or minus 100 just pick any value to return um which represents some error value this is actually not a good idea either because later on in your program if you're using this function now you have to do a check and the check is well if the return from this function is minus one or minus 100 do this otherwise do this so you're you're you're complicating your code because now you always have to have always have to have this check for this error value right which makes the code really messy the other thing we can do is we can signal an airor condition so this is how you um how you um create control flow in

**[30:06]:** your programs with exceptions so in Python signaling an error condition means raising your own exception so so far we've just seen the programs crashing which means they raise an exception and then you deal with them but in this last case you're raising your own exception as a way to use that exception later on in the in the code okay so in Python you raise your own exception using this raise keyword and then an exception and then some sort of description like user entered a negative number or something like

**[30:46]:** that okay a lot of the time we're going to raise a value error okay so if the number is less than zero then raise a value error so something is wrong okay the keyword the name of the error and then some sort of descriptive

**[31:07]:** string okay so let's see an example of how we raise an an exception I have this function here called get ratios it takes in two lists L1 and L2 and it's going to create a new list that's going to contain the ratio of each element in L1 divided by each element in L2 so I have a four Loop here for index in range length L1 so I'm going through every single element in L1 I'm going to try oops here I'm going to try to do this line so I think that this line might give me an error so I'm going to put it in a tri block the error I think I'm going to get is a zero division error because what happens when an element in L2 is zero

**[32:00]:** and when an element in L2 is zero I'm going to append this not a number as a float okay so Nan as a string you can convert it to a float and it stands for not a number okay so then I can continue populating the um the list with these not a numbers if an element in L2 is zero and otherwise if there's no zero division error but there's another kind of error then I'm going to raise my own error and say for any other kind of error just raise a value error which says get ratios was called with a bad argument okay so here I'm sort of consolidating all errors into my one value error so later on in my program I can catch this value error and do something with it here's another example of exceptions so let's say we're given a class list we have a list of lists where we have the name of a student first name and last name and their grades in the class so

**[33:09]:** we currently have two students and what I want to do is create a new list which is the same the same things the same inputs here but I'm adding an extra I'm appending an extra value at the end of the list for each student which is the average of all of their grades or all of their yeah all of their grades so let's look at the code this is a function that takes the class list which is this whole list here I'm creating a new uh list inside it initially empty and then I'm going for every element in the class list I'm appending element at zero which is going to be this first list here so it's going to be the first name and the last name element at one which is the grades and then the last thing I'm appending is a function call the function call being called with element one which is all of the grades

**[34:22]:** and this is my function call we're going to see three different function calls okay this is the first one it simply takes the sum of the grades and divides it by the length of the

**[34:35]:** grades if if these students are responsible and they've taken all of the tests right then there's no problem because length of grades is going to be something greater than zero but what if we have a student in the class who didn't show up for any

**[34:56]:** tests then we have no record of any of their tests right no record of grades or anything like that so they're going to have an empty list so if we run the same this function averages on their data we're actually going to get a zero division error because we're trying to divide by length of grades which is going to be um

**[35:20]:** zero so what can we do two things two options here one is we can just flag the error and print the message so here there's a new average function an improved one that's going to try to do the exact same line as the previous one and it's going to and it's going to catch the zero division error and when it catches it it's going to print this warning and when we run it we're going to get warning no grades data which is fine and we're going to get this none here for the grades okay so everyone else's grades was calculated correctly and for this last one we got a none that's because when we entered this accept statement right if this is a function remember functions return something this function in this particular accept statement didn't return anything right so it returns a none so for the averages for this particular function the average is

**[36:27]:** going to be a none um for this person who didn't have any grades associated with them okay and yeah so that's basically what I said so that's our first option is to just flag the flag the error and print a message the other option is to actually change the policy so this is where you replace the data with some sort of default value and if you do something like this then this should be document it inside the function so when you write the doc string for the function you would say if if the um uh if there's no if the if the if the if the list is is empty then it'll Return To Zero so this is the exact same thing as before we have a try and an accept for the zero division error we also print a warning no grades data and then we return a zero so we still flag the

**[37:30]:** error and now instead of a none we get a zero because we've returned 0.0 here as opposed to just leaving it blank okay all right so those are um exceptions last thing we're going to talk about today are these things called assertions and assertions are good example of defensive programming in that you have assert statements at the beginning of functions typically or at the end of functions and assert statements are used to make sure that the assumptions on computations are exactly what the function expects them to be okay so if we have a function that says it's supposed to take in an integer greater than zero then the assert statement will assert that the function takes in an integer that's greater than zero okay here's an example this is the same average function we've seen before here instead of using exceptions we're going to use an assert statement and the assert statement we're putting right at the at the front uh at the beginning of the function sorry and the keyword is assert the next next part of

**[38:57]:** the assert is what uh the function expects so we expect that the length of grades is not equal to zero so it has to be greater than zero and then we have a string here which represents what do you print out if the assertion does not hold so if you run the function and you give it a list that is empty this becomes false so the assert is false and we're going to print out an assertion error no grades data okay if the assert is false the function does not continue it stops right there why does it behave this way well assertions are great to make sure that preconditions and postconditions on functions are exactly as you expect okay so as soon as an assert becomes false the function is going to immediately terminate this is useful because it'll prevent the program from propagating bad values so as soon as a precondition isn't true for

**[40:12]:** example as you enter a function then that means something went wrong in your program and the program is going to stop right there so instead of propagating a a bad value throughout the program and then you getting an output that you didn't expect and then you having to trace back to the function that was that gave this bad value you'll get this bad value you'll get this assert being false a lot earlier so it'll be a lot so you it'll be a lot easier to figure out where the bug came from and you won't have to trace back so many steps okay so this is basically what I said you really want to spot the bugs as soon as they're introduced okay and exceptions are good if you want to raise them when the user supplies bad data input but assertions are used to make sure that the types um and other the types of

**[41:05]:** of inputs to functions maybe um other conditions on inputs to functions are are being held as the values are being passed in okay so the key word here is making sure that the invariance on data structures are met and that's it great thanks
