Added questions and slides for testing and debugging
authorAmit Sethi
Fri, 12 Nov 2010 02:01:28 +0530
changeset 487 cb3974daced5
parent 486 591369704df0
child 488 85f049b5ec08
Added questions and slides for testing and debugging
testing-debugging/questions.rst
testing-debugging/script.rst
testing-debugging/slides.org
testing-debugging/slides.tex
--- a/testing-debugging/questions.rst	Thu Nov 11 23:44:59 2010 +0530
+++ b/testing-debugging/questions.rst	Fri Nov 12 02:01:28 2010 +0530
@@ -3,20 +3,49 @@
 
 .. A mininum of 8 questions here (along with answers)
 
-1. Question 1
+1. Why do we do Software Testing?
+   
+   Answer: To evaluate a program and determine that it meets required results.
+
+              
 
-   Answer: Answer 1
+
+2. What is proper indentation for python code according to style guidelines?
+
+   Answer: Four Space Indentation
    
-   OR
+3. What is the idiom used for running python scripts in a standalone manner?
    
-   Answer::
+   Answer: 
+   if __name__ == '__main__':
+     
+4. What constitutes a test case?
+
+   Answer: A set of inputs and expected result
+
+5. How do you start the debugger on ipython?
+
+   Answer: %debug
+
+6. What idiom do you use for catching and exception?
+
+   Answer: try,catch.
+
+7. What kind of exception is 0/0?
+
+   Answer Zero Division Error
+
+8. a = 12.68
+   b = 0.05
+   c = round(a/b)
+   d = c * b
+
+   What would you recommend to the programmer who wrote
+   above written piece of code.
+
    
-     answer code line 1
-     answer code line 2
-     answer code line 3
 
-2. Question 2
-3. Question 3
+
 
 
 Larger Questions
@@ -24,5 +53,8 @@
 
 .. A minimum of 2 questions here (along with answers)
 
-1. Question 1
-2. Question 2
+1. Write a program for checking if two numbers are coprime
+   Create test cases for it and automate it.
+2. Write a program that divides two numbers. Take the numbers from the user. Also make sure the code tell the user that input is invalid incase the divisor is 0.
+
+
--- a/testing-debugging/script.rst	Thu Nov 11 23:44:59 2010 +0530
+++ b/testing-debugging/script.rst	Fri Nov 12 02:01:28 2010 +0530
@@ -68,7 +68,7 @@
   if __name__ == '__main__':
       result = gcd(48, 64)
       if result != 16:
-          print "Test failed for the case a=48 and b=64. Expected 16. Obtained %d instead." % result
+          print "Test failed"
       print "Test Passed"
           
 Note that we have introduced a new semantic which uses two new magic names
@@ -173,15 +173,15 @@
      1.Four Space Indentation
      2.Limit to 79 characters a line, but readability should come first.
      3.Functions and methods should be separated with two blank lines. 
-       Class definitions with three blank lines. 
      4.No inline comments, comments should be above the line they comment.
      5.Use Docstring to explain units of code performing specific task like
      	functions.
      6.We should always have whitespace around operators and after punctuation. 
 
 %% %% Pause and do the following exercise
-%% %% Give meaningful names to the variables in the gcd code .     
-
+%% %% Give meaningful names to the variables in following
+code
+	c=a/b
 
 
 This will help enormously towards making our program more readable.
@@ -217,10 +217,10 @@
 
 Lets see why and how we can use Exception in our programs.
 
-{{{ Slide with code snippet }}}
+
 
 Type on your interpreter::
-     
+
      a = raw_input("Enter a number:")
      num = int(a) 
 
@@ -274,6 +274,9 @@
      import mymodule 
      mymodule.test()
 
+
+{{{ Slide with idb and total being accessed }}}
+
 Interpreter gives us an error because spam is not defined 
 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.
 
--- a/testing-debugging/slides.org	Thu Nov 11 23:44:59 2010 +0530
+++ b/testing-debugging/slides.org	Fri Nov 12 02:01:28 2010 +0530
@@ -36,23 +36,18 @@
   -  Need for coding style and some of the standards followed by the Python Community.
   -  Handling Errors and Exceptions.
 
-
 * gcd function
   - Create gcd.py file with:
-#+begin_LaTeX
-\begin{lstlisting}[language=python]
+#+begin_src python
   def gcd(a, b):
         if a % b == 0: 
             return b
         return gcd(b, a%b)
-\end{lstlisting}
-#+end_LaTeX
+#+end_src python
 
 * Test for gcd.py
   - Edit gcd.py file
-#+begin_LaTeX
-\begin{lstlisting}[language=python]
-
+#+begin_src python 
   def gcd(a, b):
       if b == 0:
           return a
@@ -63,13 +58,11 @@
       if result != 16:
           print "Test failed"
       print "Test Passed"
-\end{lstlisting}
-#+end_LaTeX
+
+#+end_src
 
 * Automating tests
-#+begin_LaTeX
-\begin{lstlisting}[language=python]
-
+#+begin_src python
     if __name=__='__main__':
     for line in open('numbers.txt'):
         numbers = line.split()
@@ -79,8 +72,138 @@
         if gcd(x, y) != result:
             print "Failed gcd test
                           for", x, y
+#+end_src
+
+* Question 1
+  For the same inputs as gcd write automated tests for LCM.
+* Solution 1
+#+begin_src python
+  def gcd(a, b):
+        if a % b == 0: 
+            return b
+        return gcd(b, a%b)
+
+   def lcm(a, b):
+        return (a * b) / gcd(a, b)
+
+    if __name__ == '__main__':
+      for line in open('lcmtestcases.txt'):
+        numbers = line.split()
+        x = int(numbers[0])
+        y = int(numbers[1])
+        result = int(numbers[2])
+       	if lcm(x, y) != result:
+            print "Failed lcm test for", x, y
+
+#+end_src
+
+* Meaning full names
+#+begin_src python   
+   
+   amount = 12.68
+   denom = 0.05
+   nCoins = round(amount / denom)
+   rAmount = nCoins * denom
+
+#+end_src
+
+* Code style
+ - Four Space Indentation
+ - 79 character limit on a line
+ - Funtions should be seperated by 
+   blank line
+ - Use Docstring
+ - White space around operators 
+   - l = 32 % 4
+
+* Question 2
+   - Give meaningful names to the variables in following
+     code
+	
+     - c = a / b
+
+* Solution 2
+#+begin_src python
+
+  quotient = dividend / divisor
+
+#+end_src
+
+* Code Snippet
+#+begin_src python
+   
+  while True print 'Hello world'
+
+#+end_src
+
+* Error
+#+begin_latex
+\begin{lstlisting}
+ while True print 'Hello world'
+ \end{lstlisting}
+  \begin{lstlisting}
+  File "<stdin>", line 1, in ?
+    while True print 'Hello world'
+SyntaxError: invalid syntax
 \end{lstlisting}
-#+end_LaTeX
+#+end_latex
+
+* Code Snippet
+#+begin_src python
+  a = raw_input("Enter a number")
+  try:
+	num = int(a)
+   except:
+	print "Wrong input ..."
+
+#+end_src
+
+* Using idb
+#+begin_latex
+\small
+\begin{lstlisting}
+In []: import mymodule
+In []: mymodule.test()
+---------------------------------------------
+NameError   Traceback (most recent call last)
+<ipython console> in <module>()
+mymodule.py in test()
+      1 def test():
+      2     total=1+1
+----> 3     print spam
+NameError: global name 'spam' is not defined
+
+In []: %debug
+> mymodule.py(2)test()
+      0     print spam
+ipdb> total
+2
+\end{lstlisting}
+
+#+end_latex
 
 
 
+* Summary
+ - Create simple tests for a function.
+ - Learn to Automate tests using many predefined test cases.
+ - Good coding standards.
+ - Difference between syntax error and exception.
+ - Handling exception using try and except.
+ - Using %debug for debugging on ipython.
+
+* Thank you!
+#+begin_latex
+  \begin{block}{}
+  \begin{center}
+  This spoken tutorial has been produced by the
+  \textcolor{blue}{FOSSEE} team, which is funded by the 
+  \end{center}
+  \begin{center}
+    \textcolor{blue}{National Mission on Education through \\
+      Information \& Communication Technology \\ 
+      MHRD, Govt. of India}.
+  \end{center}  
+  \end{block}
+#+end_latex
+
--- a/testing-debugging/slides.tex	Thu Nov 11 23:44:59 2010 +0530
+++ b/testing-debugging/slides.tex	Fri Nov 12 02:01:28 2010 +0530
@@ -1,4 +1,4 @@
-% Created 2010-11-07 Sun 18:57
+% Created 2010-11-12 Fri 02:00
 \documentclass[presentation]{beamer}
 \usepackage[latin1]{inputenc}
 \usepackage[T1]{fontenc}
@@ -61,12 +61,12 @@
 \item Create gcd.py file with:
 \end{itemize}
 
-\begin{lstlisting}[language=python]
-  def gcd(a, b):
-        if a % b == 0: 
-            return b
-        return gcd(b, a%b)
-\end{lstlisting}
+\begin{verbatim}
+def gcd(a, b):
+      if a % b == 0: 
+          return b
+      return gcd(b, a%b)
+\end{verbatim}
 \end{frame}
 \begin{frame}[fragile]
 \frametitle{Test for gcd.py}
@@ -76,45 +76,203 @@
 \item Edit gcd.py file
 \end{itemize}
 
-\begin{lstlisting}[language=python]
+\begin{verbatim}
+def gcd(a, b):
+    if b == 0:
+        return a
+    return gcd(b, a%b)
 
-  def gcd(a, b):
-      if b == 0:
-          return a
-      return gcd(b, a%b)
-  
-  if __name__=='__main__':
-      result = gcd(48, 64)
-      if result != 16:
-          print "Test failed"
-      print "Test Passed"
-\end{lstlisting}
+if __name__=='__main__':
+    result = gcd(48, 64)
+    if result != 16:
+        print "Test failed"
+    print "Test Passed"
+\end{verbatim}
 \end{frame}
 \begin{frame}[fragile]
 \frametitle{Automating tests}
 \label{sec-4}
 
-\begin{lstlisting}[language=python]
+\begin{verbatim}
+if __name=__='__main__':
+for line in open('numbers.txt'):
+    numbers = line.split()
+    x = int(numbers[0])
+    y = int(numbers[1])
+    result = int(numbers[2])
+    if gcd(x, y) != result:
+        print "Failed gcd test
+                      for", x, y
+\end{verbatim}
+\end{frame}
+\begin{frame}
+\frametitle{Question 1}
+\label{sec-5}
+
+  For the same inputs as gcd write automated tests for LCM.
+\end{frame}
+\begin{frame}[fragile]
+\frametitle{Solution 1}
+\label{sec-6}
+
+\begin{verbatim}
+def gcd(a, b):
+      if a % b == 0: 
+          return b
+      return gcd(b, a%b)
+
+ def lcm(a, b):
+      return (a * b) / gcd(a, b)
+
+  if __name__ == '__main__':
+    for line in open('lcmtestcases.txt'):
+      numbers = line.split()
+      x = int(numbers[0])
+      y = int(numbers[1])
+      result = int(numbers[2])
+      if lcm(x, y) != result:
+          print "Failed lcm test for", x, y
+\end{verbatim}
+\end{frame}
+\begin{frame}[fragile]
+\frametitle{Meaning full names}
+\label{sec-7}
+
+\begin{verbatim}
+
+amount = 12.68
+denom = 0.05
+nCoins = round(amount / denom)
+rAmount = nCoins * denom
+\end{verbatim}
+\end{frame}
+\begin{frame}
+\frametitle{Code style}
+\label{sec-8}
+
+\begin{itemize}
+\item Four Space Indentation
+\item 79 character limit on a line
+\item Funtions should be seperated by 
+   blank line
+\item Use Docstring
+\item White space around operators
+
+\begin{itemize}
+\item l = 32 \% 4
+\end{itemize}
+
+\end{itemize}
+\end{frame}
+\begin{frame}
+\frametitle{Question 2}
+\label{sec-9}
+
+\begin{itemize}
+\item Give meaningful names to the variables in following
+     code
+
+\begin{itemize}
+\item c = a / b
+\end{itemize}
+
+\end{itemize}
+\end{frame}
+\begin{frame}[fragile]
+\frametitle{Solution 2}
+\label{sec-10}
 
-    if __name=__='__main__':
-    for line in open('numbers.txt'):
-        numbers = line.split()
-        x = int(numbers[1])
-        y = int(numbers[2])
-        result = int(numbers[3])
-        if gcd(x, y) != result:
-            print "Failed gcd test
-                          for", x, y
+\begin{verbatim}
+
+quotient = dividend / divisor
+\end{verbatim}
+\end{frame}
+\begin{frame}[fragile]
+\frametitle{Code Snippet}
+\label{sec-11}
+
+\begin{verbatim}
+
+while True print 'Hello world'
+\end{verbatim}
+\end{frame}
+\begin{frame}[fragile]
+\frametitle{Error}
+\label{sec-12}
+
+\begin{lstlisting}
+ while True print 'Hello world'
+ \end{lstlisting}
+  \begin{lstlisting}
+  File "<stdin>", line 1, in ?
+    while True print 'Hello world'
+SyntaxError: invalid syntax
 \end{lstlisting}
-
+\end{frame}
+\begin{frame}[fragile]
+\frametitle{Code Snippet}
+\label{sec-13}
 
+\begin{verbatim}
+a = raw_input("Enter a number")
+try:
+      num = int(a)
+ except:
+      print "Wrong input ..."
+\end{verbatim}
+\end{frame}
+\begin{frame}[fragile]
+\frametitle{Using idb}
+\label{sec-14}
 
+\small
+\begin{lstlisting}
+In []: import mymodule
+In []: mymodule.test()
+---------------------------------------------
+NameError   Traceback (most recent call last)
+<ipython console> in <module>()
+mymodule.py in test()
+      1 def test():
+      2     total=1+1
+----> 3     print spam
+NameError: global name 'spam' is not defined
 
-$^{1}$ FOOTNOTE DEFINITION NOT FOUND: 0
+In []: %debug
+> mymodule.py(2)test()
+      0     print spam
+ipdb> total
+2
+\end{lstlisting}
+\end{frame}
+\begin{frame}
+\frametitle{Summary}
+\label{sec-15}
 
-$^{2}$ FOOTNOTE DEFINITION NOT FOUND: 1
+\begin{itemize}
+\item Create simple tests for a function.
+\item Learn to Automate tests using many predefined test cases.
+\item Good coding standards.
+\item Difference between syntax error and exception.
+\item Handling exception using try and except.
+\item Using \%debug for debugging on ipython.
+\end{itemize}
+\end{frame}
+\begin{frame}
+\frametitle{Thank you!}
+\label{sec-16}
 
-$^{3}$ FOOTNOTE DEFINITION NOT FOUND: 2
+  \begin{block}{}
+  \begin{center}
+  This spoken tutorial has been produced by the
+  \textcolor{blue}{FOSSEE} team, which is funded by the 
+  \end{center}
+  \begin{center}
+    \textcolor{blue}{National Mission on Education through \\
+      Information \& Communication Technology \\ 
+      MHRD, Govt. of India}.
+  \end{center}  
+  \end{block}
 \end{frame}
 
 \end{document}