Figure 1 Test Code Structure
Each test implementation could be divided into four layers, this structure could help us improve the codes reusability and maintainability, So, it will make us implement the tests quickly and easily.
Specification/Scenario layer:
This layer describes system’s behaviors and functionalities by the scenarios. For using JBehave, we can use the natural language describe the scenarios and just need to follow the JBehave ‘Given-When-Then’ rule.
Parser layer:
We don’t need to implement this layer , this layer has been implemented by JBehave. What exactly JBehave do is to relate the steps of the scenario to the methods of the test codes.
Step Logic Layer:
The layer implements test logics associating with every step of the scenarios. Every step are implemented by a Java method.
Action/Utils layer
This the very important layer to improve the reusability of our codes. This layer provides the utility methods to help you implement step logics. These utility methods usually involved the system state checking, mock requests sending and so on.
For example, we can provide the methods to check the data in database/file or check the state of the middleware, also so frameworks are very useful to implement the logic simulating the client browser’s requests.
Chao Cai
Working for Amazon.com
chaocai2001@yahoo.com.cn
1 TDD is design process; it let you design for testing, naturally
Write the test firstly, it does not only help you find the bugs; but the most important point is to let you design for test naturally.
Also you should keep in mind, tests not only help you find bugs, but also protect your codes; when some changes impact on your existing codes, the tests will be broken.
2 Keep the implementation simple
Keep your implementation simple, just let the test pass. The complex implementation may introduce the logics or codes not covered by the tests, even leads some codes not testable.
3 TDD in each scope.
You may get to know the concept ATDD (acceptance test driven development). TDD could be used in every phase of the development and by the different granularity.
To ATDD, you could consider on using some existing framework such as FIT, these frameworks will be bridge between business logic and implementation logic.
Recently, the concept BDD (behavior driven development) is introduced to the ATDD process, so the BDD frameworks such as JBehave is also the good choice.
Different TDD process could be nested and should be nested don’t let your step too large.
4 keep each step small enough
Always keep each step small to avoid introducing the untestable codes or logics and pass each test quickly.
6 Always refactor
This step is always overlooked in TDD process; however it is the very important step. Also, never forget refactor should involve all your tests.
Why can't write test firstly?
1.not think how to meature the codes
2. The current step maybe too large, should separate into small ones
3. The codes with ugly dependencies
http://blog.csdn.net/chaocai2004/archive/2011/01/09/6125479.aspx
Chao Cai (钄¤秴)
Sr. SDE
Amazon.com
浠ュ墠鍦ㄥ涔?fàn)绠楁硶鍜屾暟鎹l撴瀯鐨勬椂鍊欙紝瀵逛簬姣忕綆楁硶鐨勫鏉傚害閮芥槸姝昏鐨勫茍娌℃湁鐪熸鐨勫幓鐮旂┒浠栦滑鏄浣曡綆楀嚭鏉ワ紝鏈€榪戠獊鐒跺綆楁硶浜х敓浜嗗叴瓚o紝榪嬌鑷繁鐮旂┒浜嗕竴涓嬬畻娉曞鏉傚害鐨勮綆楁柟娉曘€?/span>
姒傚康
澶?/span>O琛ㄧず娉曡〃紺烘椂闂村鏉傛€э紝娉ㄦ剰瀹冩槸鏌愪竴涓畻娉曠殑鏃墮棿澶嶆潅鎬с€傚ぇO琛ㄧず鍙槸璇存湁涓婄晫錛岀敱瀹氫箟濡傛灉f(n)=O(n)錛岄偅鏄劇劧鎴愮珛f(n)=O(n^2)錛屽畠緇欎綘涓€涓笂鐣岋紝浣嗗茍涓嶆槸涓婄‘鐣岋紝浣嗕漢浠湪琛ㄧず鐨勬椂鍊欎竴鑸兘涔?fàn)鎯〃绀哄墠鑰?/span>銆?/span>
鍙﹀闄や簡榪欎釜瀹樻柟姒傚康錛屼釜浜鴻涓哄ぇO琛ㄧず鐨勬槸闂瑙勬ān鍜岀畻娉曚腑璇彞鎵ц嬈℃暟鐨勫叧緋匯€?/span>
浠ヤ簩鍒嗘煡鎵句負(fù)渚嬶紝鎴戜滑姹傝В瀹冪殑鏃墮棿澶嶆潅搴?/span>
1 璁捐妯′負(fù)n涓厓绱犳椂錛岃鎵цT(n)嬈?/span>
T(n)=T(n/2)+1
T(n)=[T(n/4)+1]+1
…
T(n)=T(n/2^m)+m
褰?/span>n=2^m
T(n)=T(1)+log2n
T(1)=1
鎵€浠ュ叾綆楁硶澶嶆潅搴︿負(fù)O錛坙og2n錛?/span>