What's TDD
Test-driven development ( TDD ) is a software development process that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new code to acceptable standards.
Test-driven development is related to the test-first programming concepts of extreme programming.
Requirements
Test-driven development requires developers to create automated unit tests that define code requirements( immediately ) before writing the code itself. The developers should pass the tests and refactor the code. Developers often use testing frameworks, such as xUnit, testNG to create and automatically run sets of test cases.
Test-driven development cycle
a. Add a test.
b. Run all tests and see if the new one fails
c. Write some code to make the test pass. ( Fix functional code )
d. Run the automated tests and see them succeed.
e. Refactor code ( Refactor code without breaking tests )
f. Repeat.
When do refactoring?
When there are some duplication codes.
When the code intention are not very clear.
When you feel the code quality is bad or tend to bad.
Refactoring method
extract class
extract interface
extract method
replace type code with subclasses
replace conditional with polymorphism form template method
introduce explaining variable
replace constructor with factory method
replace inheritance with delegation
Repeat:
Starting with another new test, the cycle is then repeated to push forward the functionality. The size of the steps should always be small, with as few as 1 to 10 edits between each test run. If new code does not rapidly satisfy a new test, or other tests fail unexpectedly, the programmer should undo or revert.
Benefits:
a. It enables developers to take small steps when writing software. When a test pass, it means this part of work is done. In traditional development, it is hard to know when the coding is finished.
b. The best and the function code is write by the same person.
c. Helps the developer understand the code generally and correctly. In traditional development it is hard to do so.
d. Avoid over-design.
e. The system can delivered with systematic tests. So it is easy for developers to edit and extend.
f. Everyone is making design.
g. In most of the times the code has a high quality. The results are always visible.
h. Redue the bugs which were caused by the differences between documents and codes.
i. Helps the developer to prioritize the designs. Which one should do immediately, and which one should do in next iteration.
j. Compare with traditional development, TDD can find more bugs.
Disadvantages
a. It's hard to predict what mock will be using in the Unit Test. The developer may have to modify the unit test after finished the functional code.
b. The tests themselves become part of the maintenance overhead of a project. Badly written tests is hard to maintain, and it may cause problems that real failures will be ignored. So the test code should also has a high quality.
c. Focus on test and requirements but not design at the beginning of the development cycle. Although developer can optimize the design druing the refactoring, but it'll add many works, sometimes you have to rewrite all of the codes.
Summary
The test code should be high quality, easy to maintain and easy to extend. Complex test code may increase the costs of maintenance.
TDD focus more on functions and features, less on design at the beginning of the development. So it's better to design the architecture before doing the development
2012年3月16日星期五
订阅:
博文 (Atom)
美国交通罚单解密
以下经验都基于本人在纽约市的经验而谈,如有出入,敬请谅解并另行考证。 在美国纽约生活了十年,开车到现在也差不多有7年的时间了。相信每个人的驾车史,特别是对于刚来美国的新移民,总难免受到交通罚单的困恼。 之前我也有提到过罚单的 博文 ,这次主要是结合自己的实际经验给大家分享...
-
以下经验都基于本人在纽约市的经验而谈,如有出入,敬请谅解并另行考证。 在美国纽约生活了十年,开车到现在也差不多有7年的时间了。相信每个人的驾车史,特别是对于刚来美国的新移民,总难免受到交通罚单的困恼。 之前我也有提到过罚单的 博文 ,这次主要是结合自己的实际经验给大家分享...
-
最近看了一本郭德纲的书,名字叫《过得刚好》,双关语,不算自传,就是一路走来曾经记录下的点滴。博文的最后是书中的一点摘录。 相声发源于清末,是一种贴近生活,幽默语言化的曲艺形式。从我记事起,我就是个相声迷,那时可没有网络或是有条件上剧场,顶多电视里面碰上春晚、或是什么相声小品大...
-
最近看了阮一峰的新书《未来世界的幸存者》, 在线阅读 ,很多观点都感同身受,里面谈到了作者对于未来世界的思考,为什么是幸存者呢?直接地表达了作者对于未来世界发展变化的担忧。未来的世界是什么样的世界?或许人类即将迎来又一次革命性的进步,第一次人类的大进步是蒸汽机,第二次是信息技术,...