CSC/ECE 517 Fall 2012/ch2a 2w11 aa: Difference between revisions
m (→Examples) |
No edit summary |
||
Line 3: | Line 3: | ||
=Motivation for TDD= | =Motivation for TDD= | ||
*Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part. | |||
*The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements | |||
=Steps= | =Steps= | ||
Annice | Annice |
Revision as of 19:47, 23 October 2012
Introduction
Test driven development (TDD) is a process that tries to create the minimal amount of code to meet customer's expectations. The idea is to test first, code second, then improve (or refactor) last. This process forces the software developers to focus on customer specifications and validation first. Since at each step of the way the programmer proves to himself that the code meets specifications, TDD gives the programmer confidence. The rest of this chapter gives the motivation for TDD, shows the steps for TDD, outlines the principles of TDD, and provides examples using TDD.
Motivation for TDD
- Testing is the one activity that improves the quality of code. In various design scenarios, like waterfall model, testing occurs towards the end of the project development activity. As shown in the figure 1, introducing testing as a latter phase increases the cost incurred for implementing the changes. From this we infer that there is a high chance of reducing the costs by moving the Test phase to the initial part.
- The client can also be well informed about the design and can suggest changes which can be incorporated well in advance. This approach also known as TDD helps achieve flexibility to achieve the client’s ever-changing requirements
Steps
Annice
Principles
Annice
Examples
Homework Grades Program
Setup
As a simple example, we are creating a program that keeps track of our homework grades. We envision that we would be able to get the average of these homework grades. Step one: write a test . Let's test an average function
myHomework = new Homework(); myHomework.grades = [100, 50]; assert(myHomework.average(myHomework.grades) == 75);
We will get multiple errors - this test won't even compile (but, that's ok for now). Let's take a look at what will generate error messages:
- class
Homework
not declared Homework
constructor not declared- field
grades
not declared - method
average
not declared
Now, we fix the first error:
class Homework { }
Second error:
Homework(void) { }
Third error:
int * grades;
Fourth error:
int average(int * grades) { return 0; // default return value }
Finally, the test compiles! The code now looks like this:
class Homework { int * grades; Homework(void) { } int average(int * grades) { return 0; // default return value } }
Red
Now, we run the test, and the familar red bar of failure greets us (remember the mantra red-green-refactor). The assert fails. The average function needs to actually average something (not just return 0). As we think about averaging the grades, we realize we need to know how many grades are in the int array grades
. So, we add to the code:
class Homework { int * grades; int numGrades; // new Homework(void) { } int average(int * grades) { int avg = 0; // new for(int i = 0; i < numGrades; i++) { // new avg += grades[i]; // new } // new return avg/numGrades; // new } }
Of course, we must remember to change the test to:
myHomework = new Homework(); myHomework.grades = [100, 50]; myHomework.numGrades = 2; assert(myHomework.average(myHomework.grades) == 75);
Green
Success! We have a green bar when we run it.
Refactor
The last step is refactoring. Perhaps we don't want a grade to be an int
? Should it be an unsigned int
? For this simple example, there isn't much refactoring to do, but in a larger example there may be multiple areas for improvement.
More Examples
See Test-Driven Development by Example by Kent Beck for more examples.
Conclusion
Danielle
References
- Beck, K. (2002). Test-driven development by example. Addison-Wesley.
- (n.d.). Test-driven development. website: http://en.wikipedia.org/wiki/Test_driven_development
- (2012, January 11). Test driven development. website: http://c2.com/cgi/wiki?TestDrivenDevelopment
- Ambler, S. W. (2002). Introduction to test driven development (tdd). Retrieved from Agile Data website: http://www.agiledata.org/essays/tdd.html
- http://www.testdriven.com/ ?