Compartir a través de


Code and Design - Part 2 - TDD

TDD - Test Driven Development

Why TDD? First we need to know why Unit Test.

Unit Test intend to bring us a assumption and we need and only need to implement all the functionalities which defined in the Test. And the test will cover all kinds of possible calls from outside caller. The Test treats the function as a black-box and just make the guess to call it.

It is a very good and accurate technology to test the robust of the code and reduce the bug. But we are human being, once we developed the function, it is more easier for us to write the "Success assumption" Test instead of "Failure assumption" one.

TDD is the best practice to force us develop the functions from perspective of Tester and function caller. It will prevent to develop the functions that never ever will be called. Also it could avoid to waste our time to develop the never been called functions and codes.

Recommend book: Test Driven Development - Example - Kent Beck.

Here is the best example from that book to explain what is TDD:

1. Write the Test

AssertEquals( functionName(1,2), 3);

2. Develop the function

function functionName(int a, int b)
{
return a + b;
}

3. Enhance the Test

AssertEquals( functionName(-1, -2), 3);

4. Enhance the function

function functionName(int a, int b)

{

return Math.abs(a + b);

}

5. Repeat steps 3,4 until all the features of this function satisfied you :)

 

Best Practice
=================================
1. Write the Test, then write the code
2. Assume Fails, not success - the way of thinking
3. Code should satisfy your Unit Test, not yourself
4. Do not forget good code standard and comments, XP is extreme programming, term extreme forces on the methodology, programming forces on the standards
5. Do not conflict with the Design, if something wrong, review your design and think about that
6. Pay attention to your Test code, refactor it if necessary.
7. Refactor the code after all functions have been implemented