Professional Documents
Culture Documents
Dr. Raj Singh
Dr. Raj Singh
Dr. Raj Singh
Raj Singh
Test Driven Development
Best practices
Example
2
A software development process.
First write an initially failing automated test case that defines a desired improvement or new function
Related to XP
3
TDD is a design (and Unit Test
testing) approach
Code
involving short, rapid
Re-factor
iterations of
4
Software development With TDD
Should be faster Good programmers are more effective
As economical as it could be Testing can close the gap
Good quality product Quality improvement
5
pass fail No
Write
Run fail
Re/write actual code All
test test to pass the done
test pass
Yes
repeat
6
"keep it simple stupid" (KISS)
7
For TDD, a unit is most commonly
defined as a class or group of related
functions often called a module.
8
Advanced practices of TDD can lead to
Acceptance Test-driven development
(ATDD) .
Focused development.
9
A commonly applied setup, execution,
structure for test validation, and
cases has: cleanup
11
more productive
A study shows that programmer using TDD Rarely debug
are: Effective and efficient system design
No unnecessary code
Focus on task
12
More time needed initially
Requirements change
Code maintenance
13
Unit tests focus on a unit
14
When code under
development relies on a an interface should be defined
that describes the access
database, a web service, or available
any other external process
15
§ The Fibonacci numbers or series or sequence are the numbers in the following
integer sequence:
0, 1, 1, ,2 ,3, 5, 8, 13, 21, 34, …
§ The sequence Fn of Fibonacci numbers is defined by the recurrence relation
Fn = Fn-1 + Fn-2
16
§ The second test shows that fib(1) = 1.
int fib(int n) {
return 0;
}
17
§ The first test shows that fib(0) = 0. The implementation returns a constant.
int fib(int n) {
if (n == 0) return 0;
return 1;
}
18
§ We can factor out the common structure of the assertions by driving the test from a
table of input and expected values.
19
§ Now adding the next case requires 6 keystrokes and no additional lines:
§ The test works. It just so happens that our constant “1” is right for this case as well.
20
§ On to the next test:
§ Hooray, it fails. Applying the same strategy as before (treating smaller inputs as
special cases), we write:
21
int fib(int n) {
if (n == 0) return 0;
if (n <= 2) return 1;
return 2;
}
§ Now we are ready to generalize. We wrote “2”, but we don’t really mean “2”, we mean “1 + 1”.
int fib(int n) {
if (n == 0) return 0;
if (n <= 2) return 1;
return 1 + 1;
}
22
§ That first “1” is an example of fib(n-1):
int fib(int n) {
if (n == 0) return 0;
if (n <= 2) return 1;
return fib(n-1) + 1;
}
int fib(int n) {
if (n == 0) return 0;
if (n <= 2) return 1;
return fib(n-1) + fib(n-2);
}
23
§ Cleaning up now, the same structure should work for fib(2), so we can tighten up
the second condition:
int fib(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n-1) + fib(n-2);
}
24
Review class notes. Additional reading: Start a discussion on Google
Examples of UML diagrams Groups to clarify your doubts.
25