隨筆 - 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 閱讀(230) 評論(0)  編輯  收藏 所屬分類: Test-Driven Development

          主站蜘蛛池模板: 尼勒克县| 钦州市| 芒康县| 承德市| 金华市| 筠连县| 玛纳斯县| 高碑店市| 罗田县| 平顶山市| 略阳县| 东丰县| 微山县| 台江县| 阳东县| 增城市| 墨脱县| 宜州市| 年辖:市辖区| 万荣县| 迁安市| 长武县| 德保县| 五常市| 克拉玛依市| 库尔勒市| 格尔木市| 彭州市| 抚顺县| 自贡市| 墨江| 公安县| 迁西县| 通城县| 文化| 潜山县| 汝阳县| 涿州市| 庄河市| 永泰县| 秭归县|