testing_and_debugging/slides.org
changeset 522 d33698326409
equal deleted inserted replaced
521:88a01948450d 522:d33698326409
       
     1 #+LaTeX_CLASS: beamer
       
     2 #+LaTeX_CLASS_OPTIONS: [presentation]
       
     3 #+BEAMER_FRAME_LEVEL: 1
       
     4 
       
     5 #+BEAMER_HEADER_EXTRA: \usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent}
       
     6 #+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_envargs(Env Args) %4BEAMER_col(Col) %8BEAMER_extra(Extra)
       
     7 #+PROPERTY: BEAMER_col_ALL 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC
       
     8 
       
     9 #+LaTeX_CLASS: beamer
       
    10 #+LaTeX_CLASS_OPTIONS: [presentation]
       
    11 
       
    12 #+LaTeX_HEADER: \usepackage[english]{babel} \usepackage{ae,aecompl}
       
    13 #+LaTeX_HEADER: \usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet}
       
    14 
       
    15 #+LaTeX_HEADER: \usepackage{listings}
       
    16 
       
    17 #+LaTeX_HEADER:\lstset{language=Python, basicstyle=\ttfamily\bfseries,
       
    18 #+LaTeX_HEADER:  commentstyle=\color{red}\itshape, stringstyle=\color{red},
       
    19 #+LaTeX_HEADER:  showstringspaces=false, keywordstyle=\color{blue}\bfseries}
       
    20 
       
    21 #+TITLE:     Testing and debugging
       
    22 #+AUTHOR:    FOSSEE
       
    23 #+EMAIL:     
       
    24 #+DATE:    
       
    25 
       
    26 #+DESCRIPTION: 
       
    27 #+KEYWORDS: 
       
    28 #+LANGUAGE:  en
       
    29 #+OPTIONS:   H:3 num:nil toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t
       
    30 #+OPTIONS:   TeX:t LaTeX:nil skip:nil d:nil todo:nil pri:nil tags:not-in-toc
       
    31 
       
    32 * Outline 
       
    33   - What software Testing is? 
       
    34   - Learn to test simple functions for their functionality.
       
    35   - Learn how to automate tests. 
       
    36   -  Need for coding style and some of the standards followed by the Python Community.
       
    37   -  Handling Errors and Exceptions.
       
    38 
       
    39 * gcd function
       
    40   - Create gcd.py file with:
       
    41 #+begin_src python
       
    42   def gcd(a, b):
       
    43         if a % b == 0: 
       
    44             return b
       
    45         return gcd(b, a%b)
       
    46 #+end_src python
       
    47 
       
    48 * Test for gcd.py
       
    49   - Edit gcd.py file
       
    50 #+begin_src python 
       
    51   def gcd(a, b):
       
    52       if b == 0:
       
    53           return a
       
    54       return gcd(b, a%b)
       
    55   
       
    56   if __name__=='__main__':
       
    57       result = gcd(48, 64)
       
    58       if result != 16:
       
    59           print "Test failed"
       
    60       print "Test Passed"
       
    61 
       
    62 #+end_src
       
    63 
       
    64 * Automating tests
       
    65 #+begin_src python
       
    66     if __name=__='__main__':
       
    67     for line in open('numbers.txt'):
       
    68         numbers = line.split()
       
    69         x = int(numbers[0])
       
    70         y = int(numbers[1])
       
    71         result = int(numbers[2])
       
    72         if gcd(x, y) != result:
       
    73             print "Failed gcd test
       
    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
       
   148 \end{lstlisting}
       
   149 #+end_latex
       
   150 
       
   151 * Code Snippet
       
   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