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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{| border=1
 
{| border=1
!Visual Cue
+
|'''Time'''
!Narration
+
|'''Narration'''
 +
 
 
|-
 
|-
| 0:01
+
| 00:01
 
| Hello Friends and Welcome to the tutorial on 'Testing and Debugging'.
 
| Hello Friends and Welcome to the tutorial on 'Testing and Debugging'.
  
 
|-
 
|-
0: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.
  
 
|-
 
|-
0:21
+
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".
 
| 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
+
| 00:28
 
| Now, what is software testing?  
 
| Now, what is software testing?  
  
 
|-
 
|-
| 0:30
+
| 00:30
 
| Software testing is an activity aimed at evaluating a program and determining that it meets its required results.
 
| Software testing is an activity aimed at evaluating a program and determining that it meets its required results.
  
 
|-
 
|-
| 0:37
+
| 00:37
 
| Lets first write an example  simple function to calculate gcd of two numbers.  
 
| Lets first write an example  simple function to calculate gcd of two numbers.  
  
 
|-
 
|-
| 0:43
+
| 00:43
 
| Open an editor and type the following code shown on the slide and save it as gcd.py
 
| Open an editor and type the following code shown on the slide and save it as gcd.py
  
 
|-
 
|-
| 0:50
+
|00:50
 
| Save the file as gcd.py in  slash home slash fossee slash  path.
 
| Save the file as gcd.py in  slash home slash fossee slash  path.
  
 
|-
 
|-
| 0:56
+
| 00:56
 
| Now we need to evaluate this function.  
 
| Now we need to evaluate this function.  
  
 
|-
 
|-
| 0:58
+
| 00:58
 
| That is, we have to check whether this function successfully gives us the gcd of two whole numbers.  
 
| That is, we have to check whether this function successfully gives us the gcd of two whole numbers.  
  
 
|-
 
|-
| 1:04
+
| 01:04
 
| Thus we need a set of inputs and the exact outputs that are expected for those input test cases.
 
| Thus we need a set of inputs and the exact outputs that are expected for those input test cases.
  
 
|-
 
|-
| 1:10
+
| 01:10
 
| Let our test case be 48 and 64 as ''a'' and ''b'' respectively.  
 
| Let our test case be 48 and 64 as ''a'' and ''b'' respectively.  
  
 
|-
 
|-
| 1:15
+
| 01:15
 
| For this test case we know that the GCD is 16.  
 
| For this test case we know that the GCD is 16.  
  
 
|-
 
|-
| 1:19
+
| 01:19
 
| So that is the expected output.
 
| So that is the expected output.
  
 
|-
 
|-
| 1:23
+
|01:23
 
| Let us include code for testing in our file gcd.py and add the remaining lines of code to the file.
 
| Let us include code for testing in our file gcd.py and add the remaining lines of code to the file.
  
 
|-
 
|-
|1: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.  
+
  
 
|-
 
|-
| 2:02
+
| 02:02
 
| Let us now run the script and test our code  
 
| Let us now run the script and test our code  
  
 
|-
 
|-
| 2:06
+
| 02:06
 
| We run the code by providing the entire path where the file is located.
 
| We run the code by providing the entire path where the file is located.
  
 
|-
 
|-
|2:10
+
|02:10
 
|That is in the terminal we can type python gcd.py
 
|That is in the terminal we can type python gcd.py
  
 
|-
 
|-
| 2:17
+
| 02:17
 
| We get the output as 'test passed' which means our code is correct.  
 
| We get the output as 'test passed' which means our code is correct.  
  
 
|-
 
|-
| 2:20
+
| 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 ''.  
 
| 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
+
| 02:31
 
| This is a very common idiom used in Python.  
 
| This is a very common idiom used in Python.  
  
 
|-
 
|-
| 2:35
+
| 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.
 
| 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
+
| 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.  
+
  
 
|-
 
|-
| 3:04
+
| 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.  
 
| 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
+
| 03:21
 
| This comes as an extremely handy feature especially when we want to test our modules individually.
 
| This comes as an extremely handy feature especially when we want to test our modules individually.
  
 
|-
 
|-
| 3:27
+
| 03:27
 
| But there can be a number of places where the gcd function might break.  
 
| But there can be a number of places where the gcd function might break.  
  
 
|-
 
|-
| 3:33
+
| 03:33
 
| Would we have to write a separate test case for all of them.
 
| Would we have to write a separate test case for all of them.
  
 
|-
 
|-
| 3:38
+
| 03:38
 
| Pause the video here, try out the following exercise and resume the video.
 
| Pause the video here, try out the following exercise and resume the video.
  
 
|-
 
|-
| 3:43
+
| 03:43
 
| Write code for gcd and write tests for it
 
| Write code for gcd and write tests for it
  
 
|-
 
|-
| 3:48
+
| 03:48
 
| Well thats where automating tests come in.  
 
| Well thats where automating tests come in.  
  
 
|-
 
|-
| 3:52
+
| 03:52
 
| We can run many tests to check where our code can break.  
 
| We can run many tests to check where our code can break.  
  
 
|-
 
|-
| 3:57
+
| 03:57
 
| Lets see this with an example.  
 
| Lets see this with an example.  
  
 
|-
 
|-
| 3:59
+
| 03:59
 
| First lets try and automate tests on the gcd function.  
 
| First lets try and automate tests on the gcd function.  
  
 
|-
 
|-
| 4:05
+
| 04:05
 
| For this we will write a file with test cases and call the function for all of them.
 
| For this we will write a file with test cases and call the function for all of them.
  
 
|-
 
|-
| 4:13
+
| 04:13
 
| The file structure is shown in form a table here.
 
| The file structure is shown in form a table here.
  
 
|-
 
|-
| 4:19
+
| 04:19
 
| The structure of the file will be the two parameters and the output result separated by space.
 
| The structure of the file will be the two parameters and the output result separated by space.
  
 
|-
 
|-
| 4:28
+
|04:28
 
| We have separated the elements by a space.
 
| We have separated the elements by a space.
  
 
|-
 
|-
| 4:32
+
| 04:32
 
| We add this code piece to automate the test.
 
| We add this code piece to automate the test.
  
 
|-
 
|-
|4:41
+
|04:41
 
|So let us now test this code.
 
|So let us now test this code.
  
 
|-
 
|-
| 4:46
+
| 04:46
 
| Open the file gcd.py which we have created before and add this piece of code appropriately.
 
| Open the file gcd.py which we have created before and add this piece of code appropriately.
  
 
|-
 
|-
| 5:00
+
| 05:00
 
| Now, we run it as, python gcd.py.
 
| Now, we run it as, python gcd.py.
  
 
|-
 
|-
| 5:12
+
| 05:12
 
| We see that our code has passed the test.
 
| We see that our code has passed the test.
  
 
|-
 
|-
| 5:15
+
| 05:15
 
| Pause the video here, try out the following exercise and resume the video.
 
| Pause the video here, try out the following exercise and resume the video.
  
 
|-
 
|-
| 5:21
+
| 05:21
 
| For the same inputs as gcd write automated tests for LCM.
 
| For the same inputs as gcd write automated tests for LCM.
  
 
|-
 
|-
| 5:26
+
| 05:26
 
| We shall make use of the same automated test code which we had used for GCD with minor changes.  
 
| We shall make use of the same automated test code which we had used for GCD with minor changes.  
  
 
|-
 
|-
| 5:32
+
| 05:32
 
| Use the data from the file lcmtestcases.txt .  
 
| Use the data from the file lcmtestcases.txt .  
  
 
|-
 
|-
| 5:36
+
| 05:36
 
| The solution is on your screen.
 
| The solution is on your screen.
  
 
|-
 
|-
| 5:46
+
| 05:46
 
| This is the complete solution for your problem.  
 
| This is the complete solution for your problem.  
  
 
|-
 
|-
| 5:49
+
| 05:49
 
| You can test this code by running it on your terminal as we had done for gcd.py
 
| You can test this code by running it on your terminal as we had done for gcd.py
  
 
|-
 
|-
| 5:56
+
| 05:56
 
| Thus, for any program there can be innumerable test cases.  
 
| Thus, for any program there can be innumerable test cases.  
  
 
|-
 
|-
| 6:01
+
| 06:01
 
| Hence practically, it is not possible to test cases.  
 
| Hence practically, it is not possible to test cases.  
  
 
|-
 
|-
| 6:04
+
| 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.
 
| 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
+
| 06:10
 
| Moving from testing lets talk a bit about coding style now.
 
| Moving from testing lets talk a bit about coding style now.
  
 
|-
 
|-
| 6:15
+
| 06:15
 
| Apart from being able to perform the required task, a property of a good program is its readability.  
 
| Apart from being able to perform the required task, a property of a good program is its readability.  
  
 
|-
 
|-
| 6:22
+
| 06:22
 
| Code is read more often than it is written.  
 
| Code is read more often than it is written.  
  
 
|-
 
|-
| 6:25
+
| 06:25
 
| This is because, that way, other people can learn from it and extend and improve it.  
 
| This is because, that way, other people can learn from it and extend and improve it.  
  
 
|-
 
|-
| 6:30
+
| 06:30
 
| There are certain pointers for readable code that I am going to discuss.
 
| There are certain pointers for readable code that I am going to discuss.
  
 
|-
 
|-
| 6:34
+
| 06:34
 
| First, Naming variables.
 
| First, Naming variables.
  
 
|-
 
|-
| 6:39
+
| 06:39
 
| We choose a name so that it becomes easier to understand it's usage.
 
| We choose a name so that it becomes easier to understand it's usage.
  
 
|-
 
|-
| 6:44
+
| 06:44
 
|  Lets look at this with an example
 
|  Lets look at this with an example
  
 
|-
 
|-
| 6:47
+
| 06:47
 
| amount = 12.68 denom = 0.05 nCoins = round amount comma slash denom rAmount = nCoins star denom
 
| amount = 12.68 denom = 0.05 nCoins = round amount comma slash denom rAmount = nCoins star denom
  
 
|-
 
|-
| 7:01
+
| 07:01
 
| As we can see in this example it is very easy to make what the code is doing.
 
| As we can see in this example it is very easy to make what the code is doing.
  
 
|-
 
|-
| 7:07
+
| 07:07
 
| One can almost read it as English sentences.  
 
| One can almost read it as English sentences.  
  
 
|-
 
|-
| 7:10
+
| 07:10
 
| Amount is 12.68  
 
| Amount is 12.68  
  
 
|-
 
|-
| 7:12
+
| 07:12
 
| Denomination is 0.05
 
| Denomination is 0.05
  
 
|-
 
|-
| 7:16
+
| 07:16
 
| Number of coins is round of amount by denominations.
 
| Number of coins is round of amount by denominations.
  
 
|-
 
|-
| 7:20
+
| 07:20
 
| Proper naming helps so much in understanding the code.
 
| Proper naming helps so much in understanding the code.
  
 
|-
 
|-
| 7:26
+
| 07:26
 
| Also one should keep in mind the following things while writing a code.
 
| Also one should keep in mind the following things while writing a code.
  
 
|-
 
|-
| 7:30
+
| 07:30
 
| 1. Four Space Indentation
 
| 1. Four Space Indentation
  
 
|-
 
|-
| 7:33
+
| 07:33
 
| 2. Limit to 79 characters a line, but readability should come first.
 
| 2. Limit to 79 characters a line, but readability should come first.
  
 
|-
 
|-
| 7:38
+
| 07:38
 
| 3. Functions and methods should be separated with two blank lines.
 
| 3. Functions and methods should be separated with two blank lines.
  
 
|-
 
|-
| 7:41
+
| 07:41
 
| 4. No inline comments, comments should be above the line they comment.
 
| 4. No inline comments, comments should be above the line they comment.
  
 
|-
 
|-
| 7:50
+
| 07:50
 
| 5. Use Docstring to explain units of code performing specific task like functions.
 
| 5. Use Docstring to explain units of code performing specific task like functions.
  
 
|-
 
|-
| 7:56
+
| 07:56
 
| 6. We should always have whitespace around operators and other punctuation.
 
| 6. We should always have whitespace around operators and other punctuation.
  
 
|-
 
|-
| 8:00
+
| 08:00
 
| Pause the video here,and try out the following exercise and resume the video.
 
| Pause the video here,and try out the following exercise and resume the video.
  
 
|-
 
|-
| 8:05
+
| 08:05
 
| '''Give meaningful names to the variables in the following code'''
 
| '''Give meaningful names to the variables in the following code'''
  
 
|-
 
|-
| 8:08
+
| 08:08
 
| c=a slash b
 
| c=a slash b
  
 
|-
 
|-
| 8:12
+
| 08:12
 
| The solution is on your screen.
 
| The solution is on your screen.
  
 
|-
 
|-
| 8:15
+
| 08:15
 
| As you saw, this will help enormously towards making our program more readable.
 
| As you saw, this will help enormously towards making our program more readable.
  
 
|-
 
|-
| 8:24
+
| 08:24
 
| Now move on the handling errors and exceptions.  
 
| Now move on the handling errors and exceptions.  
  
 
|-
 
|-
| 8:28
+
| 08:28
 
| Lets try out the following piece of code
 
| Lets try out the following piece of code
  
 
|-
 
|-
|8:30
+
|08:30
|So type ipython
+
|So type i python
 
while True print in quotes Hello world
 
while True print in quotes Hello world
  
 
|-
 
|-
8:46
+
08:46
 
| So what happens when we do this on the interpreter.  
 
| So what happens when we do this on the interpreter.  
  
 
|-
 
|-
| 8:49
+
| 08:49
 
| The interpreter says that this is a syntax error.  
 
| The interpreter says that this is a syntax error.  
  
 
|-
 
|-
| 8:52
+
| 08:52
 
| Syntax error are caused when we do not follow the rules of the programming language.
 
| Syntax error are caused when we do not follow the rules of the programming language.
  
 
|-
 
|-
| 8:58
+
| 08:58
 
| However lets try an expression like
 
| However lets try an expression like
  
 
|-
 
|-
|9:02
+
|09:02
 
|Typing 1 slash 0
 
|Typing 1 slash 0
  
 
|-
 
|-
| 9:06
+
| 09:06
 
| Although this expression follows the programming language rules, however it is not possible to express the solution of this expression.
 
| Although this expression follows the programming language rules, however it is not possible to express the solution of this expression.
  
 
|-
 
|-
| 9:13
+
| 09:13
|  Thus python throws an exception called <tt>ZeroDivisionError</tt>.
+
|  Thus python throws an exception called Zero Division Error .
  
 
|-
 
|-
| 9:17
+
| 09:17
 
|  Exception is special kind of failure reported by the programming language.
 
|  Exception is special kind of failure reported by the programming language.
  
 
|-
 
|-
| 9:21
+
| 09:21
 
| Lets see why and how we can use Exception in our programs.
 
| Lets see why and how we can use Exception in our programs.
  
 
|-
 
|-
|9:26
+
|09:26
|So type ipython
+
|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
+
  
 
|-
 
|-
|9:55
+
|09:55
 
|Then type a non-numeric input
 
|Then type a non-numeric input
  
Line 398: Line 402:
 
|-
 
|-
 
| 10:10
 
| 10:10
| You will notice that when you run this program and give and non-numeric input it throws a 'ValueError' Exception.
+
| You will notice that when you run this program and give and non-numeric input it throws a 'Value Error' Exception.
  
 
|-
 
|-
Line 414: 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 ...
+
  
 
|-
 
|-
 
| 11:13
 
| 11:13
| In this piece of code, python tries to run the code inside the <tt>try</tt> block but when if it fails it executes the code block in <tt>except</tt>.  
+
| 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 .  
  
 
|-
 
|-
Line 432: Line 430:
 
|-
 
|-
 
| 11:31
 
| 11:31
| We found out what caused the error and then deviced a solution for it.  
+
| We found out what caused the error and then devoiced a solution for it.  
  
 
|-
 
|-
Line 460: Line 458:
 
|-
 
|-
 
| 11:57
 
| 11:57
| Create a file mymodule.py and add the following code
+
| Create a file my module.py and add the following code
  
 
|-
 
|-
Line 468: Line 466:
 
|-
 
|-
 
|12:07
 
|12:07
|def test() colon
+
|def test() colon total=1+1  print Spam
total=1+1
+
  print spam
+
  
 
|-
 
|-
 
|12:16
 
|12:16
| Lets now try and run this code on the ipython interpreter
+
| Lets now try and run this code on the i python interpreter
  
 
|-
 
|-
 
|12:19
 
|12:19
|So we have to import the file, type import mymodule
+
|So we have to import the file, type import my module
  
 
|-
 
|-
 
|12:27
 
|12:27
|Then type mymodule.test()
+
|Then type my module.test()
  
 
|-
 
|-
 
| 12:36
 
| 12:36
| Interpreter gives us an error because spam is not defined.
+
| Interpreter gives us an error because Spam is not defined.
  
 
|-
 
|-
 
| 12:40
 
| 12:40
| lets now do modula debug on ipython interpreter.
+
| lets now do Modula debug on i python interpreter.
  
 
|-
 
|-
 
|12:45
 
|12:45
|modula debug and hit enter.
+
|Modula debug and hit enter.
  
 
|-
 
|-
Line 502: Line 498:
 
|-
 
|-
 
| 12:55
 
| 12:55
| This is debugger here you can access variables in that code block for example 'total'unlike the normal interpreter.  
+
| This is debugger here you can access variables in that code block for example 'total' unlike the normal interpreter.  
  
 
|-
 
|-
Line 522: Line 518:
 
|-
 
|-
 
| 13:21
 
| 13:21
| In this tutorial,we have learnt to, 1. Create simple tests for a function.
+
| In this tutorial,we have learn't to, 1. Create simple tests for a function.
  
 
|-
 
|-
Line 542: Line 538:
 
|-
 
|-
 
| 13:35
 
| 13:35
| 6. Use percentage debug  for debugging on ipython.
+
| 6. Use percentage debug  for debugging on i python.
  
 
|-
 
|-
Line 550: 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 575: 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 ipython 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