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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 9: Line 9:
 
|-
 
|-
 
|  00:05
 
|  00:05
| At the end of the tutorial, you will be able to,
+
| At the end of the tutorial, you will be able to, Understand what is software testing.
 +
 +
|-
 +
|  00:09
 +
|Test simple functions for their functionality.
  
# Understand what is software testing.
+
|-
# Test simple functions for their functionality.
+
|  00:11
# Automate tests.
+
|Automate tests.Understand the need for coding style
# Understand the need for coding style
+
 
# Learn some of the standards followed by the Python Community.
+
|-
# Handle Errors and Exceptions.
+
|  00:14
 +
|Learn some of the standards followed by the Python Community.
 +
 
 +
|-
 +
|  00:18
 +
| Handle Errors and Exceptions.
  
 
|-
 
|-
Line 72: Line 81:
 
|-
 
|-
 
|01:33
 
|01:33
|That is f  underscore  underscore name underscore   underscore  == in quotes underscore   underscore main underscore   underscore  colon
+
|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.  
result = gcd within bracket 48 comma  64 if result exclamation= 16 colon
+
print  within double quotes Test failed
+
print  within double quotes Test Passed.  
+
  
 
|-
 
|-
Line 107: Line 113:
 
|-
 
|-
 
| 02:48
 
| 02:48
| When the idiom
+
| 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.  
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.  
+
  
 
|-
 
|-
Line 386: Line 390:
 
|-
 
|-
 
|09:26
 
|09:26
|So type i python
+
|So type i python a = raw underscore input within bracket  within double quotes Enter a number colon
a = raw underscore input within bracket  within double quotes Enter a number colon
+
  
 
|-
 
|-
Line 415: Line 418:
 
|-
 
|-
 
|10:33
 
|10:33
|So type a = raw underscore input then
+
|So type a = raw underscore input then Enter a decimal number, try colon, num = int a, except ,   print within double quotes Wrong input ...
Enter a decimal number
+
 
+
try colon
+
  num = int a
+
  except  
+
   print within double quotes Wrong input ...
+
  
 
|-
 
|-
Line 469: Line 466:
 
|-
 
|-
 
|12:07
 
|12:07
|def test() colon
+
|def test() colon total=1+1  print Spam
total=1+1
+
  print Spam
+
  
 
|-
 
|-
Line 551: Line 546:
 
|-
 
|-
 
| 13:43
 
| 13:43
| 1. What is proper indentation for python code according to style guidelines?
+
|What is proper indentation for python code according to style guidelines?
** two space indentation
+
two space indentation
** three space indentation
+
three space indentation
** four Space Indentation
+
four Space Indentation
** no Indentation
+
no Indentation
  
 
|-
 
|-
 
| 13:53
 
| 13:53
| 2. '''How do you start the debugger on ipython?'''
+
|'''How do you start the debugger on ipython?''' debug, Modula debugModula debugger,start debugger
**
+
*** debug
+
*** Modula debug
+
*** Modula debugger
+
*** start debugger
+
  
 
|-
 
|-
 
| 14:01
 
| 14:01
| 1. What is the idiom used for running python scripts in a standalone manner?
+
|What is the idiom used for running python scripts in a standalone manner?
  
 
|-
 
|-
Line 576: Line 566:
 
|-
 
|-
 
| 14:11
 
| 14:11
| 1.Four Space Indentation is required for writing a python code according to style guidelines.
+
|Four Space Indentation is required for writing a python code according to style guidelines.
  
 
|-
 
|-
 
| 14:19
 
| 14:19
| 2. We start the debugger on i python by saying, Modula debug
+
|We start the debugger on i python by saying, Modula debug
  
 
|-
 
|-
 
| 14:25
 
| 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.
+
|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.
  
 
|-
 
|-

Latest revision as of 16:07, 20 February 2017

Time Narration
00:01 Hello Friends and Welcome to the tutorial on 'Testing and Debugging'.
00:05 At the end of the tutorial, you will be able to, Understand what is software testing.
00:09 Test simple functions for their functionality.
00:11 Automate tests.Understand the need for coding style
00:14 Learn some of the standards followed by the Python Community.
00:18 Handle Errors and Exceptions.
00:21 Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with functions" and "Advanced Features of the Functions".
00:28 Now, what is software testing?
00:30 Software testing is an activity aimed at evaluating a program and determining that it meets its required results.
00:37 Lets first write an example simple function to calculate gcd of two numbers.
00:43 Open an editor and type the following code shown on the slide and save it as gcd.py
00:50 Save the file as gcd.py in slash home slash fossee slash path.
00:56 Now we need to evaluate this function.
00:58 That is, we have to check whether this function successfully gives us the gcd of two whole numbers.
01:04 Thus we need a set of inputs and the exact outputs that are expected for those input test cases.
01:10 Let our test case be 48 and 64 as a and b respectively.
01:15 For this test case we know that the GCD is 16.
01:19 So that is the expected output.
01:23 Let us include code for testing in our file gcd.py and add the remaining lines of code to the file.
01: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.
02:02 Let us now run the script and test our code
02:06 We run the code by providing the entire path where the file is located.
02:10 That is in the terminal we can type python gcd.py
02:17 We get the output as 'test passed' which means our code is correct.
02: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 .
02:31 This is a very common idiom used in Python.
02: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.
02: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.
03: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.
03:21 This comes as an extremely handy feature especially when we want to test our modules individually.
03:27 But there can be a number of places where the gcd function might break.
03:33 Would we have to write a separate test case for all of them.
03:38 Pause the video here, try out the following exercise and resume the video.
03:43 Write code for gcd and write tests for it
03:48 Well thats where automating tests come in.
03:52 We can run many tests to check where our code can break.
03:57 Lets see this with an example.
03:59 First lets try and automate tests on the gcd function.
04:05 For this we will write a file with test cases and call the function for all of them.
04:13 The file structure is shown in form a table here.
04:19 The structure of the file will be the two parameters and the output result separated by space.
04:28 We have separated the elements by a space.
04:32 We add this code piece to automate the test.
04:41 So let us now test this code.
04:46 Open the file gcd.py which we have created before and add this piece of code appropriately.
05:00 Now, we run it as, python gcd.py.
05:12 We see that our code has passed the test.
05:15 Pause the video here, try out the following exercise and resume the video.
05:21 For the same inputs as gcd write automated tests for LCM.
05:26 We shall make use of the same automated test code which we had used for GCD with minor changes.
05:32 Use the data from the file lcmtestcases.txt .
05:36 The solution is on your screen.
05:46 This is the complete solution for your problem.
05:49 You can test this code by running it on your terminal as we had done for gcd.py
05:56 Thus, for any program there can be innumerable test cases.
06:01 Hence practically, it is not possible to test cases.
06:04 However there are many ideas to reduce the set of test cases by testing those cases that are more likely to show errors.
06:10 Moving from testing lets talk a bit about coding style now.
06:15 Apart from being able to perform the required task, a property of a good program is its readability.
06:22 Code is read more often than it is written.
06:25 This is because, that way, other people can learn from it and extend and improve it.
06:30 There are certain pointers for readable code that I am going to discuss.
06:34 First, Naming variables.
06:39 We choose a name so that it becomes easier to understand it's usage.
06:44 Lets look at this with an example
06:47 amount = 12.68 denom = 0.05 nCoins = round amount comma slash denom rAmount = nCoins star denom
07:01 As we can see in this example it is very easy to make what the code is doing.
07:07 One can almost read it as English sentences.
07:10 Amount is 12.68
07:12 Denomination is 0.05
07:16 Number of coins is round of amount by denominations.
07:20 Proper naming helps so much in understanding the code.
07:26 Also one should keep in mind the following things while writing a code.
07:30 1. Four Space Indentation
07:33 2. Limit to 79 characters a line, but readability should come first.
07:38 3. Functions and methods should be separated with two blank lines.
07:41 4. No inline comments, comments should be above the line they comment.
07:50 5. Use Docstring to explain units of code performing specific task like functions.
07:56 6. We should always have whitespace around operators and other punctuation.
08:00 Pause the video here,and try out the following exercise and resume the video.
08:05 Give meaningful names to the variables in the following code
08:08 c=a slash b
08:12 The solution is on your screen.
08:15 As you saw, this will help enormously towards making our program more readable.
08:24 Now move on the handling errors and exceptions.
08:28 Lets try out the following piece of code
08:30 So type i python

while True print in quotes Hello world

08:46 So what happens when we do this on the interpreter.
08:49 The interpreter says that this is a syntax error.
08:52 Syntax error are caused when we do not follow the rules of the programming language.
08:58 However lets try an expression like
09:02 Typing 1 slash 0
09:06 Although this expression follows the programming language rules, however it is not possible to express the solution of this expression.
09:13 Thus python throws an exception called Zero Division Error .
09:17 Exception is special kind of failure reported by the programming language.
09:21 Lets see why and how we can use Exception in our programs.
09:26 So type i python a = raw underscore input within bracket within double quotes Enter a number colon
09: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 'Value Error' 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 devoiced 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 my module.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 i python interpreter
12:19 So we have to import the file, type import my module
12:27 Then type my module.test()
12:36 Interpreter gives us an error because Spam is not defined.
12:40 lets now do Modula debug on i python 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 learn't 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 i python.
13:40 Here are some self assessment questions for you to solve
13:43 What is proper indentation for python code according to style guidelines?

two space indentation three space indentation four Space Indentation no Indentation

13:53 How do you start the debugger on ipython? debug, Modula debugModula debugger,start debugger
14:01 What is the idiom used for running python scripts in a standalone manner?
14:08 So now we look at the answers,
14:11 Four Space Indentation is required for writing a python code according to style guidelines.
14:19 We start the debugger on i python by saying, Modula debug
14:25 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