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

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

          TDD是這樣一種設(shè)計(jì)風(fēng)格

          Maintain an exhaustive suite of Programmer Tests

          維護(hù)一套程序員測試的框架

          No code goes into production unless it has associated tests

          除非它已結(jié)合測試,產(chǎn)品不寫入任何代碼

          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());
          }



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

          (eclipse的快速修復(fù)功能能幫你搞定哦??磥硐葘憸y試還是很方便的*^^*)

          讓計(jì)算機(jī)來告訴你

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

          下面看看代碼的演進(jìn)

          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;
          }



          實(shí)際上,每次變化之后都重新編譯和運(yùn)行這個測試。

          Agile Modeling and TDD


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

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

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

          主站蜘蛛池模板: 西乌| 大港区| 理塘县| 利辛县| 古田县| 闽清县| 永仁县| 辽宁省| 伊宁市| 延吉市| 尖扎县| 和平区| 永嘉县| 延津县| 宁强县| 陆良县| 朝阳县| 宁城县| 新巴尔虎左旗| 融水| 舟曲县| 察哈| 乌苏市| 盈江县| 呈贡县| 利川市| 德惠市| 临潭县| 昌江| 深圳市| 高雄县| 慈利县| 嘉荫县| 竹北市| 亳州市| 陆丰市| 炉霍县| 新竹市| 朝阳区| 杭锦后旗| 安陆市|