Some questions and answers about unit testing
I have had an interesting discussion with some project managers about the benefit of unit testing in last Friday. Here are just some common questions raised in the meeting and my answers, I think it is also be interesting for all ones who want to apply unit test in software development but still have concern about the benefit of unit test.
Question 1: How to write unit test efficiently?
Answer 1: Some examples of unit test try to give just 2+2=4 equation for writing unit test, so simple method such as store data in database, retrieve data and compare whether it is correct. Follow these stupid examples can lead people have the wrong think about unit test. In my opinion, writing unit test effectively is not a simple task. The first principle is that we must maintain the test code is very simple, just feed the input, get the output and compare with expected result. Each test method should only have 10-15 lines and it has no extra effort. However, to write the unit test effectively you must make your design is testable. A design is called testable if it is designed well enough followed object oriented design principles. Just care the quality of architecture besides the writing unit test.
Question 2: The effort of unit test is very high, it can cost 40-50% effort of writing code. Applying unit test can cause the effort of development is increased, is it correct?
Answer 2: Each developer has responsibility of making sure his/her works gain good quality. Due to iterative software process that cause almost source codes are written in many times. Applying unit test by tool can help us automatically define the defects indirectly caused by modification. At the first time, writing unit test seems take more effort just than manual but in later it save efforts of defining defects, fix defects etc In overall, having unit test tends to reduce the effort of development.
Question 3: Team would like to apply unit test, do we need to inform customer that we apply unit test and suggest them extend the schedule due to apply unit test will increase the quality but it takes time
Answer 3: The first thing the customer care it is not what you did in development but you must give them a quality product. You can use XP, RUP almost client does not care but just artifacts are defined in SOW. So release a quality product is your responsibility not client. As the Q/A 2, applying unit test will reduce the development effort but increase product quality. So you should apply to your product and it is not involved to client at all.
Question 4: I have a big product and complex architecture. How can I evaluate the effort to make unit test effectively?
Answer 4: All kind of project should apply unit test. As Q/A 1, writing unit test is not simple. It is more complex if you have a existing legacy system. In almost case, you must spend effort for refractoring the current architecture before writing unit test for that legacy application.
Question 5: continue with Q/A 4, you means in that case I must spend time for refractoring and unit test, it seems to take us a lot effort for unit test?
Answer 5: The benefit you gain here is quality. Refractoring will help you make the design simple, that leads the quality of architecture is increased and effort for maintenance is decreased. Unit test also has the same benefit. In case, you just maintain the product in 2 months and we have a lot of works must be completed, you should not refractoring/unit test legacy system but if you continue working with it more than 1-2 years, you should think about the refractoring and unit test. The solution is depended on your circumstance though in almost case, refractoring and unit test is the wise solution.