I was recently told about the unit testing in Test Driven Development (TDD) environment. It is not new, but it seems that the practice of TDD is very rare in the development area and very poor documentation or less documents available for the CppUnit testing. I received many emails from people asking what exactly the TDD is and it seems like many people do not understand the usefulness of the TDD. Therefore, I decided to put this article up on the blog.
What is Test-Driven Development?
Since the computers were invented, the programmers faced the software bugs while implementing for new programs. Some will even claim that there is no software that is free from software bugs. It might be possible since our human are imperfect. This phenomenon will be lasting forever as long as systems are getting complex. If you have written any program in any programming languages, I believe you have faced the problem of software bugs that made you scratched your head and hated so much about programming. This is the main motivation of Test-Driven Development (TDD). The word "TESTING" become a part of software development life cycle (SDLC). Testing will enable you to ensure whether your program is working correctly.
Most of the time, we wrote a program from beginning to end. When the program codes are completed, then we try to compile and run the program and check if the program has any errors or bugs. This way is efficient if your program is like printing a "hello world" message. But what if your program is about implementing some complex systems. In this case, we wrote the program by a numbers of functions or modules. In Unit Testing, you will do the testing on function by function or module by module. Basically, You will ensure the correctness of the function before you will continue with next function.
Advantages of Test-Driven Development
Have you encountered case like that? You wrote the program and run it successfully on your computer. But when it deployed at the customer site, your customers are mad and complained that your program have lots of bugs. That is really bad - if you found the problem at final stage of development, you have to spent lot of times to analyze the program from line 1. If you found the problem at customer, then you will lose the business and that is the worst. Why is this thing happened? One of the possibilities is that you missed/skipped some test cases. Another possibilities is you have little time and you do not have enough time for testing. Another fact that I won't deny is that some programmers are lazy to do the testings including myself. Bugs multiply as you wrote the programs. If you have found the bug and solved it in early stage will not only save you more time but also make your customer happier. It is like construction of building. You got to lay strong foundation of building before you step up another level of building. Otherwise, your building will be either swaying to one side or worst collapsed when it's completed.
Test Twice, Write Once
Now you know about Test-Driven Development and its advantages over the conventional programming style. But how? It requires you to do more testings than your coding. Basically, it will change your concept of writing programming. I will give you an example of a function called "addition(a, b)" that will add the input 'a' and 'b' regardless of data types (int or float).
Step 1: Instead of writing your program codes, you will write testing codes with possible inputs. In this case, the possible inputs can be both 'a' and 'b' are integers, floats, or double. Remember that your addition function is still empty with no codes yet.
In your main program:
|
In your testing program:
|
addition(a,b){ return a+b; }
| testAddition(a,b){ CPP_ASSERT(addition(1,2)==3); //integer CPP_ASSERT(addition(1.0,2.0)==3.0); //float . . . }
|
With this sample above, when you run your testing program, the return is FALSE since the main program function is empty with no coding.
Step 2: You will now write your code in your main program for addition function.
Step 3: You will run the testing program again. If your main program is correctly written for addition function, the testing program will return TRUE. If you are still getting the result of FALSE, which mean your main program has not correctly written the function for addition and you will modify the function until you can receive the result TRUE in your test program.
In this way, you perform the test twice, and code it once. You will ensure the function of addition before you will continue with other functions such as multiplication(a,b) or division(a,b). This is how the TDD is supposed to be developed.