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