Difference between revisions of "Python/C4/Testing-and-debugging/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with '{| border=1 !Visual Cue !Narration |- | 0:01 | Hello Friends and Welcome to the tutorial on 'Testing and Debugging'. |- | 0:05 | At the end of the tutorial, you will be able to…')
 
Line 538: Line 538:
 
|-
 
|-
 
| 13:32
 
| 13:32
| 5. Handle exception using <tt>try</tt> and <tt>except</tt>.
+
| 5. Handle exception using try and except  
  
 
|-
 
|-
 
| 13:35
 
| 13:35
| 6. Use <tt>percentage debug</tt> for debugging on ipython.
+
| 6. Use percentage debug for debugging on ipython.
  
 
|-
 
|-
Line 551: Line 551:
 
| 13:43
 
| 13:43
 
| 1. What is proper indentation for python code according to style guidelines?
 
| 1. What is proper indentation for python code according to style guidelines?
** two space identation
+
** two space indentation
** three space identation
+
** three space indentation
 
** four Space Indentation
 
** four Space Indentation
 
** no Indentation
 
** no Indentation
Line 583: Line 583:
 
|-
 
|-
 
| 14:25
 
| 14:25
| 1. <tt>if  underscore  underscore name underscore  underscore  == in single quotes underscore  underscore main underscore  underscore  colon</tt> is the idiom used for running python scripts in a standalone manner.
+
| 1.if  underscore  underscore name underscore  underscore  == in single quotes underscore  underscore main underscore  underscore  colon is the idiom used for running python scripts in a standalone manner.
  
 
|-
 
|-

Revision as of 16:16, 19 March 2013

Visual Cue Narration
0:01 Hello Friends and Welcome to the tutorial on 'Testing and Debugging'.
0:05 At the end of the tutorial, you will be able to,
  1. Understand what is software testing.
  2. Test simple functions for their functionality.
  3. Automate tests.
  4. Understand the need for coding style
  5. Learn some of the standards followed by the Python Community.
  6. Handle Errors and Exceptions.
0:21 Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with functions" and "Advanced Features of the Functions".
0:28 Now, what is software testing?
0:30 Software testing is an activity aimed at evaluating a program and determining that it meets its required results.
0:37 Lets first write an example simple function to calculate gcd of two numbers.
0:43 Open an editor and type the following code shown on the slide and save it as gcd.py
0:50 Save the file as gcd.py in slash home slash fossee slash path.
0:56 Now we need to evaluate this function.
0:58 That is, we have to check whether this function successfully gives us the gcd of two whole numbers.
1:04 Thus we need a set of inputs and the exact outputs that are expected for those input test cases.
1:10 Let our test case be 48 and 64 as a and b respectively.
1:15 For this test case we know that the GCD is 16.
1:19 So that is the expected output.
1:23 Let us include code for testing in our file gcd.py and add the remaining lines of code to the file.
1:33 That is f underscore underscore name underscore underscore == in quotes underscore underscore main underscore underscore colon

result = gcd within bracket 48 comma 64 if result exclamation= 16 colon print within double quotes Test failed print within double quotes Test Passed.

2:02 Let us now run the script and test our code
2:06 We run the code by providing the entire path where the file is located.
2:10 That is in the terminal we can type python gcd.py
2:17 We get the output as 'test passed' which means our code is correct.
2:20 Note that we have introduced a new semantic which uses two new magic names in Python underscore underscore name underscore underscore and underscore underscore main underscore underscore .
2:31 This is a very common idiom used in Python.
2:35 Every Python code in a file can be run in two ways: Either as an independent stand-alone script or as a Python module which can be imported by other Python scripts or modules.
2:48 When the idiom

if underscore underscore name underscore underscore == ' underscore underscore main underscore underscore ' is used, the code within this if block is executed first when we run the Python file as a stand-alone script.

3:04 In other words, when we run this python file as a stand-alone script, the control of the program first starts from the code that is within this if block after which the control is transferred to other parts of the program or to other modules from here.
3:21 This comes as an extremely handy feature especially when we want to test our modules individually.
3:27 But there can be a number of places where the gcd function might break.
3:33 Would we have to write a separate test case for all of them.
3:38 Pause the video here, try out the following exercise and resume the video.
3:43 Write code for gcd and write tests for it
3:48 Well thats where automating tests come in.
3:52 We can run many tests to check where our code can break.
3:57 Lets see this with an example.
3:59 First lets try and automate tests on the gcd function.
4:05 For this we will write a file with test cases and call the function for all of them.
4:13 The file structure is shown in form a table here.
4:19 The structure of the file will be the two parameters and the output result separated by space.
4:28 We have separated the elements by a space.
4:32 We add this code piece to automate the test.
4:41 So let us now test this code.
4:46 Open the file gcd.py which we have created before and add this piece of code appropriately.
5:00 Now, we run it as, python gcd.py.
5:12 We see that our code has passed the test.
5:15 Pause the video here, try out the following exercise and resume the video.
5:21 For the same inputs as gcd write automated tests for LCM.
5:26 We shall make use of the same automated test code which we had used for GCD with minor changes.
5:32 Use the data from the file lcmtestcases.txt .
5:36 The solution is on your screen.
5:46 This is the complete solution for your problem.
5:49 You can test this code by running it on your terminal as we had done for gcd.py
5:56 Thus, for any program there can be innumerable test cases.
6:01 Hence practically, it is not possible to test cases.
6:04 However there are many ideas to reduce the set of test cases by testing those cases that are more likely to show errors.
6:10 Moving from testing lets talk a bit about coding style now.
6:15 Apart from being able to perform the required task, a property of a good program is its readability.
6:22 Code is read more often than it is written.
6:25 This is because, that way, other people can learn from it and extend and improve it.
6:30 There are certain pointers for readable code that I am going to discuss.
6:34 First, Naming variables.
6:39 We choose a name so that it becomes easier to understand it's usage.
6:44 Lets look at this with an example
6:47 amount = 12.68 denom = 0.05 nCoins = round amount comma slash denom rAmount = nCoins star denom
7:01 As we can see in this example it is very easy to make what the code is doing.
7:07 One can almost read it as English sentences.
7:10 Amount is 12.68
7:12 Denomination is 0.05
7:16 Number of coins is round of amount by denominations.
7:20 Proper naming helps so much in understanding the code.
7:26 Also one should keep in mind the following things while writing a code.
7:30 1. Four Space Indentation
7:33 2. Limit to 79 characters a line, but readability should come first.
7:38 3. Functions and methods should be separated with two blank lines.
7:41 4. No inline comments, comments should be above the line they comment.
7:50 5. Use Docstring to explain units of code performing specific task like functions.
7:56 6. We should always have whitespace around operators and other punctuation.
8:00 Pause the video here,and try out the following exercise and resume the video.
8:05 Give meaningful names to the variables in the following code
8:08 c=a slash b
8:12 The solution is on your screen.
8:15 As you saw, this will help enormously towards making our program more readable.
8:24 Now move on the handling errors and exceptions.
8:28 Lets try out the following piece of code
8:30 So type ipython

while True print in quotes Hello world

8:46 So what happens when we do this on the interpreter.
8:49 The interpreter says that this is a syntax error.
8:52 Syntax error are caused when we do not follow the rules of the programming language.
8:58 However lets try an expression like
9:02 Typing 1 slash 0
9:06 Although this expression follows the programming language rules, however it is not possible to express the solution of this expression.
9:13 Thus python throws an exception called ZeroDivisionError.
9:17 Exception is special kind of failure reported by the programming language.
9:21 Lets see why and how we can use Exception in our programs.
9:26 So type ipython

a = raw underscore input within bracket within double quotes Enter a number colon

9:55 Then type a non-numeric input
10:01 Then type num = int a.
10:10 You will notice that when you run this program and give and non-numeric input it throws a 'ValueError' Exception.
10:21 So now we can 'catch' this exception and write code to handle it.
10:25 For this we have try and except clause in python.
10:29 Lets change our previous code slightly.
10:33 So type a = raw underscore input then

Enter a decimal number

try colon

  num = int a
except 
 print within double quotes Wrong input ...
11:13 In this piece of code, python tries to run the code inside the try block but when if it fails it executes the code block in except.
11:23 In previous example we encountered a problem with running our conversion to integer code.
11:31 We found out what caused the error and then deviced a solution for it.
11:36 This whole process is called debugging.
11:38 One can understand the debugging process using the figure.
11:42 In debugging process, we form a hypothesis of what causes the error.
11:47 Test if it is correct by changing the code.
11:50 And refine the hypothesis on the basis of our result.
11:54 Lets see another example of debugging.
11:57 Create a file mymodule.py and add the following code
12:02 So type and create the file
12:07 def test() colon
total=1+1
print spam
12:16 Lets now try and run this code on the ipython interpreter
12:19 So we have to import the file, type import mymodule
12:27 Then type mymodule.test()
12:36 Interpreter gives us an error because spam is not defined.
12:40 lets now do modula debug on ipython interpreter.
12:45 modula debug and hit enter.
12:48 The prompt on the shell has changed to ipdb.
12:55 This is debugger here you can access variables in that code block for example 'total'unlike the normal interpreter.
13:06 So you can type,total and check.
13:11 We get the correct output.
13:13 To exit from the ipdb prompt, press q
13:18 This brings us to the end of this tutorial.
13:21 In this tutorial,we have learnt to, 1. Create simple tests for a function.
13:24 2. Automate tests using many predefined test cases.
13:27 3. Use the python coding standards.
13:30 4. Differentiate between syntax error and exception.
13:32 5. Handle exception using try and except
13:35 6. Use percentage debug for debugging on ipython.
13:40 Here are some self assessment questions for you to solve
13:43 1. What is proper indentation for python code according to style guidelines?
    • two space indentation
    • three space indentation
    • four Space Indentation
    • no Indentation
13:53 2. How do you start the debugger on ipython?
      • debug
      • Modula debug
      • Modula debugger
      • start debugger
14:01 1. What is the idiom used for running python scripts in a standalone manner?
14:08 So now we look at the answers,
14:11 1.Four Space Indentation is required for writing a python code according to style guidelines.
14:19 2. We start the debugger on ipython by saying, modula debug
14:25 1.if underscore underscore name underscore underscore == in single quotes underscore underscore main underscore underscore colon is the idiom used for running python scripts in a standalone manner.
14:40 So we hope you have enjoyed this tutorial and found it useful.
14:46 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha