testing-debugging/script.rst
author Amit Sethi
Mon, 08 Nov 2010 01:54:34 +0530
changeset 401 abf092be95ef
child 487 cb3974daced5
permissions -rw-r--r--
Adding testing and debugging files
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
401
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
     1
.. Objectives
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
     2
.. ----------
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
     3
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
     4
.. Writing Simple Tests (Applying)
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
     5
.. Automating these tests
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
     6
.. Coding Style
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
     7
.. Errors and Exceptions 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
     8
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
     9
.. Prerequisites
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    10
.. -------------
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    11
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    12
..   1. Getting started with functions
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    13
..   2. Advanced Features of Functions   
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    14
     
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    15
.. Author              : Amit Sethi
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    16
   Internal Reviewer   : 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    17
   External Reviewer   :
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    18
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    19
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    20
Script
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    21
------
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    22
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    23
{{{ Show the slide containing title }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    24
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    25
Hello Friends. Welcome to this tutorial on Testing and Debugging.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    26
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    27
{{{ Show the outline slide }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    28
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    29
In this tutorial we will learn.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    30
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    31
1.What software Testing is? 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    32
2.Learn to test simple functions for their functionality.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    33
3.Learn how to automate tests. 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    34
4.Need for coding style and some of the standards followed by the Python Community.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    35
5.Handling Errors and Exceptions.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    36
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    37
Software testing is an activity aimed at evaluating a program 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    38
and determining that it meets its required results.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    39
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    40
{{{ Slide with the function }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    41
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    42
Lets first write a simple function to calculate gcd of two numbers.::
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    43
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    44
     def gcd(a, b):
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    45
      	 if b == 0:
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    46
            return a
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    47
      	 return gcd(b, a%b)
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    48
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    49
save this into a file "/home/fossee/gcd.py".
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    50
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    51
Now we need to evaluate this function. Thus we have to check whether 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    52
function successfully gives us the gcd of two whole numbers. Thus we need
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    53
a set of inputs and the exact outputs that are expected for those input 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    54
test cases.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    55
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    56
Let our test case be 48 and 64 as *a* and *b* respectively. For this test
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    57
case we know that the GCD is 16. So that is the expected output. 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    58
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    59
{{{ Slide with change in code }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    60
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    61
Let us include code for testing in our  **gcd.py** file ::
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    62
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    63
  def gcd(a, b):
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    64
      if b == 0:
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    65
          return a
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    66
      return gcd(b, a%b)
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    67
  
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    68
  if __name__ == '__main__':
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    69
      result = gcd(48, 64)
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    70
      if result != 16:
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    71
          print "Test failed for the case a=48 and b=64. Expected 16. Obtained %d instead." % result
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    72
      print "Test Passed"
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    73
          
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    74
Note that we have introduced a new semantic which uses two new magic names
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    75
in Python *__name__* and *__main__*. This is a very common idiom used in
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    76
Python. Every Python code in a file can be run in two ways: Either as an
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    77
independent stand-alone script or as a Python module which can be imported
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    78
by other Python scripts or modules. When the idiom::
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    79
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    80
  if __name__ == '__main__':
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    81
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    82
{{{ Slide with the idiom }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    83
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    84
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    85
is used, the code within this if block is executed first when we run the
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    86
Python file as a stand-alone script. In other words, when we run this
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    87
python file as a stand-alone script the control of the program first starts
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    88
from the code that is within this if block from which the control is
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    89
transferred to other parts of the program or to other modules from
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    90
here. This comes as an extremely handy feature especially when we want to
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    91
test our modules individually.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    92
      
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    93
But there can be a number of places where the gcd function might break would we
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    94
have to right a seperate test case for all of them. 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    95
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    96
Following is an (are) exercise(s) that you must do. 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    97
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    98
%% %% Write code for lcm and write tests for it  
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
    99
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   100
%% %% Answer is on the screen.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   101
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   102
Well thats where automating tests come in. We can run many tests to check where our
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   103
code can break. Lets see this with an example. Lets try and automate tests on the 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   104
gcd function. For this we will write a file with test cases and call the function
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   105
for all of them.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   106
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   107
{{{ Slide with the structure of file }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   108
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   109
The structure of the file will be the two parameters and the output result seperated 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   110
by space::
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   111
    
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   112
    |   12 |    28 |    4 |
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   113
    |   18 |    36 |   18 |
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   114
    | 4678 | 39763 | 2339 |
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   115
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   116
The file structure is shown in form a table here.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   117
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   118
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   119
{{{ Slide with code piece }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   120
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   121
We add the code piece to automate the test.::
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   122
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   123
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   124
   if __name__ == '__main__':
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   125
      for line in open('testcases.txt'):
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   126
        numbers = line.split()
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   127
        x = int(numbers[0])
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   128
        y = int(numbers[1])
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   129
        result = int(numbers[2])
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   130
       	if gcd(x, y) != result:
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   131
            print "Failed gcd test for", x, y
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   132
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   133
%% %% Pause the video do the following exercise 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   134
%% %% For the same inputs as gcd write automated tests for LCM.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   135
%% %% The solution is on the screen
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   136
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   137
For any program there can be innumerable test cases thus it is not
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   138
possible to test cases. However there are many ideas to reduce the set of
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   139
test cases by testing those cases that are more likely to show errors.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   140
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   141
Moving from testing lets talk a bit about coding style now.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   142
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   143
Apart from from being able to perform the required task, a property
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   144
of a good program is its readability. Code is read more often than it is
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   145
written. This is because that way other people can learn from it and extend 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   146
and improve it. There are certain pointers for readable code that I am going to discuss.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   147
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   148
First, Naming variables.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   149
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   150
{{{ Slide with code snippet }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   151
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   152
Choose name that by which people will most likely guess the usage.Lets look at this 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   153
with an example::
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   154
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   155
       amount = 12.68
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   156
       denom = 0.05
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   157
       nCoins = round(amount/denom)
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   158
       rAmount = nCoins * denom
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   159
 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   160
As we can see in the example it is very easy to make what the code is doing.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   161
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   162
One can almost read it as English sentences.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   163
Amount is 12.68
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   164
Denomination is .05
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   165
Number of coins is round of amount by denominations.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   166
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   167
Proper naming helps so much in understanding the code.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   168
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   169
{{{ Slide with code style points }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   170
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   171
Also one should use. ::
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   172
     
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   173
     1.Four Space Indentation
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   174
     2.Limit to 79 characters a line, but readability should come first.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   175
     3.Functions and methods should be separated with two blank lines. 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   176
       Class definitions with three blank lines. 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   177
     4.No inline comments, comments should be above the line they comment.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   178
     5.Use Docstring to explain units of code performing specific task like
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   179
     	functions.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   180
     6.We should always have whitespace around operators and after punctuation. 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   181
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   182
%% %% Pause and do the following exercise
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   183
%% %% Give meaningful names to the variables in the gcd code .     
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   184
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   185
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   186
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   187
This will help enormously towards making our program more readable.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   188
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   189
From coding style lets move on to handling errors and exceptions.  
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   190
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   191
{{{ Slide with code snippet }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   192
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   193
Lets try out the following piece of code::
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   194
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   195
     while True print 'Hello world'
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   196
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   197
{{{ Slide with Error }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   198
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   199
what happens when we do this on the interpreter. The interpreter 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   200
says that this is a syntax error. Syntax error are caused when we
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   201
do not follow the rules of the programming language.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   202
   
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   203
{{{ Slide with expression }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   204
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   205
However lets try an expression like ::
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   206
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   207
	1/0
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   208
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   209
{{{ Slide with Error }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   210
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   211
Although this expression follows the programming language rules,
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   212
however it is not possible to express the solution of this expression.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   213
Thus python throws an exception called ZeroDivisionError. Exception 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   214
is special kind of failure reported by the programming language.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   215
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   216
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   217
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   218
Lets see why and how we can use Exception in our programs.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   219
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   220
{{{ Slide with code snippet }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   221
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   222
Type on your interpreter::
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   223
     
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   224
     a = raw_input("Enter a number:")
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   225
     num = int(a) 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   226
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   227
{{{ Run this code on interpreter with a character input }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   228
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   229
You will notice that when you run this program and give and
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   230
non-numeric input it throws a 'ValueError' Exception. 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   231
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   232
So now we can 'catch' this exception and write code to 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   233
handle it.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   234
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   235
{{{ Slide with code snippet }}} 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   236
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   237
For this we have try and except clause in python. Lets change our 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   238
previous code slightly.::
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   239
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   240
	 a = raw_input("Enter a number")
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   241
	 try:
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   242
		num = int(a)
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   243
   	 except:
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   244
		print "Wrong input ..."
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   245
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   246
{{{ Run the code with character input }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   247
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   248
In this piece of code python tries to run the code inside the try
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   249
block but when if it fails it executes the code block in except.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   250
	  
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   251
In previous example we encountered a problem with running our conversion
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   252
to integer code. We found out what caused the error and then deviced a solution
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   253
for it this whole process is called debugging.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   254
 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   255
One can understand the debugging process using the figure.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   256
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   257
In debugging process we form a hypothesis of what causes the error.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   258
Test if it is correct by changing the code. And refine the hypothesis 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   259
on the basis of our result.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   260
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   261
{{{ Slide with code snippet }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   262
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   263
Lets see another example of debugging. Create a file mymodule.py and
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   264
add the following code::
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   265
    
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   266
    def test():
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   267
	total=1+1	
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   268
	print spam
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   269
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   270
{{{ Slide with code snippet }}} 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   271
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   272
Lets now try and run this code ::
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   273
     
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   274
     import mymodule 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   275
     mymodule.test()
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   276
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   277
Interpreter gives us an error because spam is not defined 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   278
but lets now do %debug on ipython interpreter. The prompt on the shell has changed to ipdb. This is debugger here you can access variables in that code block for example 'total'unlike the normal interpreter.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   279
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   280
%% %% Pause and do the following exercise
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   281
%% %% Do the gcd program which takes input from user which tells
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   282
%% %% the user wrong input if it is not valid and quits for 'q'.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   283
 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   284
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   285
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   286
This brings us to the end of this tutorial on testing and debugging. 
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   287
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   288
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   289
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   290
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   291
{{{ Show the summary slide }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   292
 	
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   293
In this tutorial we have learned to
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   294
1.Create simple tests for a function.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   295
2.Learn to Automate tests using many predefined test cases.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   296
3.Good coding standards.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   297
4.Difference between syntax error and exception.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   298
5.Handling exception using try and except.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   299
6.Using %debug for debugging on ipython.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   300
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   301
{{{ Show the "sponsored by FOSSEE" slide }}}
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   302
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   303
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   304
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   305
Hope you have enjoyed and found it useful.
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   306
Thank you!
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   307
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   308
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   309
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   310
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   311
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   312
abf092be95ef Adding testing and debugging files
Amit Sethi
parents:
diff changeset
   313