diff options
author | Amit Sethi | 2010-11-12 02:01:28 +0530 |
---|---|---|
committer | Amit Sethi | 2010-11-12 02:01:28 +0530 |
commit | 2258ff6f30478efdaa4dce0f1347b170f90783d1 (patch) | |
tree | c70c0562d4f23a7bad716dc80963f92937f79bc6 | |
parent | 671859df1a8221a546e640bc88f4d9445ee7505e (diff) | |
download | st-scripts-2258ff6f30478efdaa4dce0f1347b170f90783d1.tar.gz st-scripts-2258ff6f30478efdaa4dce0f1347b170f90783d1.tar.bz2 st-scripts-2258ff6f30478efdaa4dce0f1347b170f90783d1.zip |
Added questions and slides for testing and debugging
-rw-r--r-- | testing-debugging/script.rst | 15 | ||||
-rw-r--r-- | testing-debugging/slides.org | 153 | ||||
-rw-r--r-- | testing-debugging/slides.tex | 226 |
3 files changed, 339 insertions, 55 deletions
diff --git a/testing-debugging/script.rst b/testing-debugging/script.rst index b30beb5..94549cf 100644 --- a/testing-debugging/script.rst +++ b/testing-debugging/script.rst @@ -68,7 +68,7 @@ Let us include code for testing in our **gcd.py** file :: 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 @@ Also one should use. :: 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 @@ is special kind of failure reported by the programming language. 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 @@ Lets now try and run this code :: 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. diff --git a/testing-debugging/slides.org b/testing-debugging/slides.org index 0e56fe3..23dc2f4 100644 --- a/testing-debugging/slides.org +++ b/testing-debugging/slides.org @@ -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 -* Automating tests -#+begin_LaTeX -\begin{lstlisting}[language=python] +#+end_src +* Automating tests +#+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{lstlisting} -#+end_LaTeX +#+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 + +* 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 diff --git a/testing-debugging/slides.tex b/testing-debugging/slides.tex index e7dd7d7..d83387e 100644 --- a/testing-debugging/slides.tex +++ b/testing-debugging/slides.tex @@ -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 @@ showstringspaces=false, keywordstyle=\color{blue}\bfseries} \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 @@ showstringspaces=false, keywordstyle=\color{blue}\bfseries} \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] - - 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 -\end{lstlisting} +\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} -$^{1}$ FOOTNOTE DEFINITION NOT FOUND: 0 +\begin{verbatim} -$^{2}$ FOOTNOTE DEFINITION NOT FOUND: 1 +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 + +In []: %debug +> mymodule.py(2)test() + 0 print spam +ipdb> total +2 +\end{lstlisting} +\end{frame} +\begin{frame} +\frametitle{Summary} +\label{sec-15} + +\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} |