summaryrefslogtreecommitdiff
path: root/TDD/getting_started_with_tdd/tdd1_script.rst
blob: ebe50360d1ba8d05a9d1c94b0b2f239cdb67168c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
------
Script
------

.. L1

{{{ Show the "Title" slide }}}

.. R1

Hello friends and welcome to the tutorial on, 
'Getting started with TDD'

.. L2

{{{ Show "Objectives" slide }}} 

.. R2

At the end of this tutorial, you will be able to,

 1. Understand basics of Test Driven Development.
 #. Understand the use test cases.
 #. Write simple tests for a function.

.. L3

{{{ Switch the "Pre-requisite" slide }}}

.. R3

Before beginning this tutorial, we would suggest you to complete the 
tutorial on "Getting started with functions".

.. L4

{{{ show "What is TDD?" slide }}}

.. R4

Test-driven development is a software development process that relies on the
repetition of a very short development cycle.

The basics of Test Driven Development are,

1. Decide the new feature you want to implement & the methodology to test it.
#. Write tests for the new feature decided upon.
#. Just write enough code so that the test can run, but, it fails.


.. L5

{{{ show "What is TDD?.." slide }}}

.. R5

#. Modify the code so that all the current tests & the previous tests pass.
#. Run tests to see if all the tests pass successfully.
#. Refactor the code - optimize the algorithm, remove duplication & 
   documentation, etc.
#. And finally run the tests again to see if all the tests pass.


.. L6

{{{ show "First test - fibonacci" slide }}}

.. R6

To illustrate TDD, lets take a simple program. Consider a function "fibonacci",
that takes one argument and returns the nth number of fibonacci series.
As shown in the example, c will contain the 3rd digit of the fibonacci series
that starts counting from 0(zero).


.. L7

{{{ show "Test Cases" slide }}}
	
.. R7 

To test any function it is important to have enough test cases & their expected
outputs before you start writing the test cases. Test cases are expected
outputs for a given set of inputs. So, to test fibonacci function,
we need test cases. As shown in this slide, our test cases are, when 'n=3', '2'
is the expected output from the fibonnaci series. Similarly, when 'n=4', '3'
is the expected output.
Test cases can either be true or false depending on their actual behaviour
against the expected behaviour.


.. L8
 
{{{ show "Test cases-Code" slide }}}

.. R8

The sample code for test cases is shown here. Observe that if any "if"
statement is executed, test aborts after printing the error message.


.. L9

{{{ show the "Stubs" slide }}}

.. R9

Now, the fibonacci function is written just enough so that the tests can run.
But, obviously the tests are going to fail.


.. L10

{{{ show "fibonacci.py" slide }}}

.. R10

We will now combine the fibonnaci stub function we just wrote & the test cases 
together in "fibonacci.py" file. Note that the test cases should be added
after "name=main" idiom.

.. L11 

{{{ show the "first run" slide }}}
{{{ Run the fibonacci.py in terminal and show the error output & switch back to
slide }}}
>>> python fibonacci.py
       
.. R11

Now, let us run the fibonacci.py file. To do this, switch to the terminal & 
type "python fibonacci.py".


The tests fail as we just have a stub "fibonacci" function and no meaningful
code in it. Our next step is to write just minimum code to pass our tests.


.. L12

{{{ show "Fibonnaci" slide }}}
{{{ switch to terminal }}}
>>> python fibonacci.py


.. R12

Modify the fibonacci stub function with given code & save the file. Now, switch
to terminal and run it again as "python fibonacci.py".
 
{{{ pause }}}
Observe that, there will be no errors, as the test passes successfully.


.. L13

{{{ Show "Fibonacci - Recursive" slide }}}
   
.. R13

Finally, the same "fibonacci" function is modified to make it more readable
and easy to understand using recursion.
Pause this video here. Replace the "fibonacci" function with recursive one.
Run the modified "fibonacci.py" file. The test should pass again without any
errors. After successfully achieving this result, you can resume the video.

.. L14

{{{ show "Summary" slide }}}

.. R14

This brings us to the end of the tutorial. In this tutorial,  we have learnt,
 
 1. The basic steps involved in Test driven development.
 #. How to use test cases.
 #. How to write simple tests for a function.


.. L15

{{{ show "Evaluation" slide }}}

.. R15

Here are some self assessment questions for you to solve,

 1. Design a TDD approach for a factorial function.
 2. Design a TDD approach for an armstrong function. 

Try out the excercises for yourself & resume the video for solutions.


.. L16

{{{ show "Factorial" slide}}}

.. R16

Given here is a TDD approach to a factorial function.

.. L17

{{{ show "Armstrong" slide}}}

.. R17

Given here is a TDD approach to an armstrong function.


.. L18

{{{ Show the SDES & FOSSEE slide }}}

.. R18

Software Development techniques for Engineers and Scientists - SDES, is an 
initiative by FOSSEE. For more information, please visit the given link.

Free and Open-source Software for Science and Engineering Education - FOSSEE, is
based at IIT Bombay which is funded by MHRD as part of National Mission on 
Education through ICT.

.. L19

{{{ Show the ``About the Spoken Tutorial Project'' slide }}}

.. R19

Watch the video available at the following link. It summarises the Spoken 
Tutorial project.If you do not have good bandwidth, you can download and 
watch it. 

.. L20

{{{ Show the `` Spoken Tutorial Workshops'' slide }}}

.. R20

The Spoken Tutorial Project Team conducts workshops using spoken tutorials,
gives certificates to those who pass an online test.

For more details, contact contact@spoken-tutorial.org

.. L21

{{{ Show the Acknowledgements slide }}}

.. R21

Spoken Tutorial Project is a part of the "Talk to a Teacher" project.
It is supported by the National Mission on Education through ICT, MHRD, 
Government of India. More information on this mission is available at the 
given link.

.. L22

{{{ Show the Thankyou slide }}}

.. R22

Hope you have enjoyed this tutorial and found it useful.
Thank you!