testing-debugging/slides.org
changeset 487 cb3974daced5
parent 403 9858ca9e3f93
equal deleted inserted replaced
486:591369704df0 487:cb3974daced5
    34   - Learn to test simple functions for their functionality.
    34   - Learn to test simple functions for their functionality.
    35   - Learn how to automate tests. 
    35   - Learn how to automate tests. 
    36   -  Need for coding style and some of the standards followed by the Python Community.
    36   -  Need for coding style and some of the standards followed by the Python Community.
    37   -  Handling Errors and Exceptions.
    37   -  Handling Errors and Exceptions.
    38 
    38 
    39 
       
    40 * gcd function
    39 * gcd function
    41   - Create gcd.py file with:
    40   - Create gcd.py file with:
    42 #+begin_LaTeX
    41 #+begin_src python
    43 \begin{lstlisting}[language=python]
       
    44   def gcd(a, b):
    42   def gcd(a, b):
    45         if a % b == 0: 
    43         if a % b == 0: 
    46             return b
    44             return b
    47         return gcd(b, a%b)
    45         return gcd(b, a%b)
    48 \end{lstlisting}
    46 #+end_src python
    49 #+end_LaTeX
       
    50 
    47 
    51 * Test for gcd.py
    48 * Test for gcd.py
    52   - Edit gcd.py file
    49   - Edit gcd.py file
    53 #+begin_LaTeX
    50 #+begin_src python 
    54 \begin{lstlisting}[language=python]
       
    55 
       
    56   def gcd(a, b):
    51   def gcd(a, b):
    57       if b == 0:
    52       if b == 0:
    58           return a
    53           return a
    59       return gcd(b, a%b)
    54       return gcd(b, a%b)
    60   
    55   
    61   if __name__=='__main__':
    56   if __name__=='__main__':
    62       result = gcd(48, 64)
    57       result = gcd(48, 64)
    63       if result != 16:
    58       if result != 16:
    64           print "Test failed"
    59           print "Test failed"
    65       print "Test Passed"
    60       print "Test Passed"
    66 \end{lstlisting}
    61 
    67 #+end_LaTeX
    62 #+end_src
    68 
    63 
    69 * Automating tests
    64 * Automating tests
    70 #+begin_LaTeX
    65 #+begin_src python
    71 \begin{lstlisting}[language=python]
       
    72 
       
    73     if __name=__='__main__':
    66     if __name=__='__main__':
    74     for line in open('numbers.txt'):
    67     for line in open('numbers.txt'):
    75         numbers = line.split()
    68         numbers = line.split()
    76         x = int(numbers[0])
    69         x = int(numbers[0])
    77         y = int(numbers[1])
    70         y = int(numbers[1])
    78         result = int(numbers[2])
    71         result = int(numbers[2])
    79         if gcd(x, y) != result:
    72         if gcd(x, y) != result:
    80             print "Failed gcd test
    73             print "Failed gcd test
    81                           for", x, y
    74                           for", x, y
       
    75 #+end_src
       
    76 
       
    77 * Question 1
       
    78   For the same inputs as gcd write automated tests for LCM.
       
    79 * Solution 1
       
    80 #+begin_src python
       
    81   def gcd(a, b):
       
    82         if a % b == 0: 
       
    83             return b
       
    84         return gcd(b, a%b)
       
    85 
       
    86    def lcm(a, b):
       
    87         return (a * b) / gcd(a, b)
       
    88 
       
    89     if __name__ == '__main__':
       
    90       for line in open('lcmtestcases.txt'):
       
    91         numbers = line.split()
       
    92         x = int(numbers[0])
       
    93         y = int(numbers[1])
       
    94         result = int(numbers[2])
       
    95        	if lcm(x, y) != result:
       
    96             print "Failed lcm test for", x, y
       
    97 
       
    98 #+end_src
       
    99 
       
   100 * Meaning full names
       
   101 #+begin_src python   
       
   102    
       
   103    amount = 12.68
       
   104    denom = 0.05
       
   105    nCoins = round(amount / denom)
       
   106    rAmount = nCoins * denom
       
   107 
       
   108 #+end_src
       
   109 
       
   110 * Code style
       
   111  - Four Space Indentation
       
   112  - 79 character limit on a line
       
   113  - Funtions should be seperated by 
       
   114    blank line
       
   115  - Use Docstring
       
   116  - White space around operators 
       
   117    - l = 32 % 4
       
   118 
       
   119 * Question 2
       
   120    - Give meaningful names to the variables in following
       
   121      code
       
   122 	
       
   123      - c = a / b
       
   124 
       
   125 * Solution 2
       
   126 #+begin_src python
       
   127 
       
   128   quotient = dividend / divisor
       
   129 
       
   130 #+end_src
       
   131 
       
   132 * Code Snippet
       
   133 #+begin_src python
       
   134    
       
   135   while True print 'Hello world'
       
   136 
       
   137 #+end_src
       
   138 
       
   139 * Error
       
   140 #+begin_latex
       
   141 \begin{lstlisting}
       
   142  while True print 'Hello world'
       
   143  \end{lstlisting}
       
   144   \begin{lstlisting}
       
   145   File "<stdin>", line 1, in ?
       
   146     while True print 'Hello world'
       
   147 SyntaxError: invalid syntax
    82 \end{lstlisting}
   148 \end{lstlisting}
    83 #+end_LaTeX
   149 #+end_latex
    84 
   150 
    85 
   151 * Code Snippet
    86 
   152 #+begin_src python
       
   153   a = raw_input("Enter a number")
       
   154   try:
       
   155 	num = int(a)
       
   156    except:
       
   157 	print "Wrong input ..."
       
   158 
       
   159 #+end_src
       
   160 
       
   161 * Using idb
       
   162 #+begin_latex
       
   163 \small
       
   164 \begin{lstlisting}
       
   165 In []: import mymodule
       
   166 In []: mymodule.test()
       
   167 ---------------------------------------------
       
   168 NameError   Traceback (most recent call last)
       
   169 <ipython console> in <module>()
       
   170 mymodule.py in test()
       
   171       1 def test():
       
   172       2     total=1+1
       
   173 ----> 3     print spam
       
   174 NameError: global name 'spam' is not defined
       
   175 
       
   176 In []: %debug
       
   177 > mymodule.py(2)test()
       
   178       0     print spam
       
   179 ipdb> total
       
   180 2
       
   181 \end{lstlisting}
       
   182 
       
   183 #+end_latex
       
   184 
       
   185 
       
   186 
       
   187 * Summary
       
   188  - Create simple tests for a function.
       
   189  - Learn to Automate tests using many predefined test cases.
       
   190  - Good coding standards.
       
   191  - Difference between syntax error and exception.
       
   192  - Handling exception using try and except.
       
   193  - Using %debug for debugging on ipython.
       
   194 
       
   195 * Thank you!
       
   196 #+begin_latex
       
   197   \begin{block}{}
       
   198   \begin{center}
       
   199   This spoken tutorial has been produced by the
       
   200   \textcolor{blue}{FOSSEE} team, which is funded by the 
       
   201   \end{center}
       
   202   \begin{center}
       
   203     \textcolor{blue}{National Mission on Education through \\
       
   204       Information \& Communication Technology \\ 
       
   205       MHRD, Govt. of India}.
       
   206   \end{center}  
       
   207   \end{block}
       
   208 #+end_latex
       
   209