隨筆 - 3, 文章 - 152, 評論 - 17, 引用 - 0
          數據加載中……

          TDD(1)--轉自http://www.aygfsteel.com/yandazhi

          TDD是這樣一種設計風格

          Maintain an exhaustive suite of Programmer Tests

          維護一套程序員測試的框架

          No code goes into production unless it has associated tests

          除非它已結合測試,產品不寫入任何代碼

          Write the tests first

          先寫測試

          Tests determine what code you need to write

          測試決定你需要寫什么代碼

          public void testEmptyList() {
              MovieList emptyList 
          =newMovieList();
              assertEquals(
          "Empty list should have size of 0"0, emptyList.size());
          }



          要通過上面的測試,你必須創建一個類MovieList,和一個方法size();

          (eclipse的快速修復功能能幫你搞定哦。看來先寫測試還是很方便的*^^*)

          讓計算機來告訴你

          你需要增加類或者方法,編譯器會告訴你。(eclipse會向你抱怨有cannot be resolved 的)

          下面看看代碼的演進

          public void testRating() {
            assertEquals(
          "Bad average rating.",4,starWars.getAverageRating());
          }



          public void testRating() {
            starWars.addRating(
          3);
            starWars.addRating(
          5);
            assertEquals(
          "Bad average rating.",4,starWars.getAverageRating());
          }


          public void testRating() {
            Movie starWars 
          = new Movie("Star Wars");
            starWars.addRating(
          3);
            starWars.addRating(
          5);
            assertEquals(
          "Bad average rating.",4,starWars.getAverageRating());
          }



          非常有意思,和我們平時寫代碼的順序相反

          下面看看getAverageRating();

          public int getAverageRating() {
            
          return 4;
          }


          public int getAverageRating() {
            
          return (3 + 5/ 2;
          }



          private int totalRating = 0;


          public void addRating(int newRating) {
            totalRating 
          += newRating;
          }




          public int getAverageRating() {
            
          return totalRating / 2;
          }



          private int numberOfRatings = 0;

          public void addRating(int newRating) {
            totalRating 
          += newRating;
            numberOfRatings
          ++;
          }


          public int getAverageRating() {
            
          return totalRating / numberOfRatings;
          }



          實際上,每次變化之后都重新編譯和運行這個測試。

          Agile Modeling and TDD


          采用XP(極限編程)的項目都采用了TDD的輔助手段。建模(modeling)是XP當中很重要的的一個部分。XP開發者使用用戶敘述(user stories) ,用戶敘述是清晰的敏捷模型。

          創建敏捷模型能夠幫助我們TDD工作,這是因為,他能揭示我們需要的測試。一個敏捷模型草圖的背后總是隱含著這樣的思考“我怎樣來測試他”,這將導致一個新的測試案例。

          posted on 2005-07-25 12:26 閱讀(225) 評論(0)  編輯  收藏 所屬分類: Test-Driven Development

          主站蜘蛛池模板: 大化| 班戈县| 镇原县| 兰州市| 象州县| 尉氏县| 宜黄县| 江达县| 奉贤区| 陵水| 祁阳县| 同仁县| 新巴尔虎左旗| 侯马市| 托里县| 洛扎县| 彰化市| 新安县| 浦城县| 眉山市| 毕节市| 南投市| 三台县| 吴旗县| 墨脱县| 佛坪县| 安阳市| 景宁| 江口县| 宝应县| 河间市| 错那县| 兴国县| 喀喇沁旗| 离岛区| 沂水县| 鄯善县| 沂源县| 谢通门县| 鞍山市| 邵阳县|