Presentation is loading. Please wait.

Presentation is loading. Please wait.

MOOR ERROR HANDLING Types of error How to test your code for errors How to detect errors How to recover from errors.

Similar presentations


Presentation on theme: "MOOR ERROR HANDLING Types of error How to test your code for errors How to detect errors How to recover from errors."— Presentation transcript:

1 MOOR ERROR HANDLING Types of error How to test your code for errors How to detect errors How to recover from errors

2 TYPES OF ERROR – THEORY   Recap: 3 main errors made by programmers (that is us!) 1. Syntax Errors – eg. silly typing errors 2. Run-time Errors – eg. it may run but crashes at some point 3. Logical Errors – eg. getting the code to run but it does something different  It is important to test a program to check if it is robust and not likely to break down.  A program is very unlikely to work perfectly the first time it is executed thus testing is carried out to try and make it fail or reveal the presence of errors.  Q. What are the consequences of software not being tested effectively? 1)The reputation of the company would be ruined 2)An accident could be caused if the code is used in a safety critical system eg. Aircraft/nuclear power station ------------------------------------------------------------------------------------------------------------------------ Debugging: the process of detecting and correcting errors during execution of a program.

3 DRY-RUN TESTING  Once you have an algorithm, pseudo-code or flow chart then a dry-run testing can be carried out.  This involves stepping through the algorithm one instruction at a time with purposely chosen data. A trace table is used by the programmer to track the test data.  Hopefully the table will help pinpoint where the problem is. Note, this form of testing is done on a small scale as it is hard going and very repetitive.  The main advantage of dry-run testing is that is enables programmers to spot errors even before they start writing code.

4

5 DRY-RUN TESTING Somebody on StackOverflow wrote: I am wondering? Why would the output not be: i1 j1 j2 i2 i3 and instead it is: i1 j1 j2 i2 j1 j2 i3 j1 j2 Why does the rightmost while loop keep repeating even though the value of j is already satisfied eventually by it. Answer: i= 1 while (i<=3): print("i",i) j=1 while (j<3): print("j",j) j=j+1 i=i+1 print("Done!") You are re-initializing your j to 1 in the outer while loop. So, every time your outer loop, starts, your inner loop will run two times. A trace table would be able to help find the source of the problem with all the variables and conditions along the top. Each time a variable/condition changes you go one row down and enter into the table. j

6 UNIT TESTING  This is a popular means of testing a program by writing additional programs that test a specific function of a main program.  This is useful for massive programs so you focus on a small piece of the code or a specific module.  Unit testing is an automatic tool that can uncover regressions. These are unwanted changes in previously working code which may have been introduced during the development.  Code coverage is a term that is used to describe the percentage of lines of code that are covered by unit tests. Eg. A def function or a specific looping structure.  Remember: we are looking for bugs which are the key annoyance to any programmer!

7 DEBUGGING  To debug code effectively 2 things are needed:  The ability to test each of the instructions provided by the program  The ability to retrieve the information about:  The results of the instructions  Any changes in the program when the tests were carried out  The error conditions  What the program was doing when the error occurred.  Thank goodness for our software tools in Eclipse to help in the debugging process.  The interpreter will detect syntax errors prior to running the program.  Problems will be flagged up:  Then the interpreter will detect run-time errors when you run the program.  You have to find logical errors yourself!  There are 3 specific feature to look at in our debugging work:  Breakpoints …. Steps … Watches …lets get debugging…

8 Copy the following code into Idle, save as “RPS”: import random,time def WhoWins(you,computer): if you == ("rock"): if computer == ("scissors"): return 1 # You win elif computer == ("rock"): return 2 # draw elif computer == ("paper"): return 3 # Computer wins elif you == ("paper"): if computer == ("rock"): return 1 # You win elif computer == ("paper"): return 2 # draw elif computer == ("scissors"): return 3 # Computer wins elif you == ("scissors"): if computer == ("paper"): return 1 # You win elif computer == ("scissors"): return 2 # draw elif computer == ("rock"): return 3 # Computer wins else: return 0 # invalid input option = ["rock", "paper","scissors"] wins = 0 loses = 0 draws = 0 while True: print("wins =", wins) print("loses =", loses) print("draws =", draws) you = input("rock,paper or scissors: ") computer = random.choice(option) time.sleep(1) print("you picked", you) time.sleep(1) print("computer picked", computer) time.sleep(1) winner=WhoWins(you,computer) if winner == 1: # you won print("you win") wins += 1 elif winner == 2: print("draw") draws += 1 elif winner == 3: print("you lose") loses += 1 else: print("invalid input")

9 Instructions: Put a breakpoint in “RPS”. Once the program is paused the debugger allows the tester to continue the execution of the program one line at a time; effectively stepping through the program. This allows a programmer to see exactly how many variables and objects are affected when a particular line is executed. Work through the step over, step into and step return. Note the variables being assigned in the debugger. Find the watch table (top right corner). Look at how the debugger sorts the variables. Consider the scope when inside a function. Make any other observations to see how the sub-routine (function) operates. Use the debugger next time you write a program to help detect errors

10


Download ppt "MOOR ERROR HANDLING Types of error How to test your code for errors How to detect errors How to recover from errors."

Similar presentations


Ads by Google