Try   HackMD

Testing 學習筆記

tags: Testing 學習紀錄 測試

優點

參考網址

促使開發者思考結構

  • 為了先寫出測試,開發者必須先去思考如何進行測試。
  • 同時若嚴格實行 TDD(Test-Driven Development 測試驅動開發),每個循環的步驟1~2都只編寫最少量的程式碼,程式們會趨向被編寫成小而獨立的區塊,而增加了維護性與再利用性。
  • 因此會帶來幾個特點:
    • 寫出可重複測試的程式 (廢話)
    • 減少程式之間的耦合
    • 函式小而簡潔
    • 而如果開發的最後才寫測試,則容易發現函式之間互相依賴,導致難以編寫個別函式的單元測試。

易於重構 (Refactor)

  • 所謂重構,指的是維持程式已完成的功能,但是整理程式碼以增加可維護性,像是把修改成更有意義的變數名稱、將過大的函式拆開等等。
  • 利用 TDD 開發的過程中,幾乎所有的程式都維持在已編寫了單元測試的狀態,也許只要幾秒就能運行完一次所有的測試,這代表了一點,也是我認為最重要的一點。
    不怕把程式改到爆掉!
    不怕把程式改到爆掉!
    不怕把程式改到爆掉!
  • 當我們面對複雜又大量的程式碼時 (極端例子: 一個1000行的函式),會害怕我們一但修改了其中一行,導致整個程式崩潰,這時我們也跟著程式崩潰了,因為 Debug 地獄既煩人又耗時。
  • 於是開發者會傾向不去觸碰以前寫好、目前運作上正常的程式碼,日積月累,最終演變成幾乎沒有可讀性。
  • 但擁有測試就能夠一定程度確保,沒有把原本寫好的程式破壞掉,若有,測試通常會第一時間印出哪一行的單元測試有錯誤,這也是說 TDD 的每個循環都做了一次 regression test (回歸測試)。
  • 讓開發者更勇於對程式碼進行重構,因此也容易保持簡潔。

減少 Debug 的時間

  • 相當多 Bug 會在執行測試時被檢測出來,Debug 的時間當然也減少了。
  • 同時每一個函式小而獨立,要檢測出 Bug 出在哪一個函式也就相對容易。

高測試覆蓋率

  • 覆蓋率是指,程式碼有被測試執行過的比例,測試的工具套件能夠提供。
  • 這一點算是使用 TDD 開發附帶的好處,因為我們在開發的同時,一邊也在寫著測試,程式的測試覆蓋率會非常高,這會讓我們對自己寫出來的程式很有信心。
  • 開發後一次編寫所有測試,較容易忽略一些可能發生的正常執行情況,另一方面一次花費大量的編寫時間也可能會讓開發者想偷懶。

照做過程

  • 文章
  • 在 feature/ 快速建立一個測試類別
    • php artisan make:test IndexTest
    • Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
    • 此時可下php artisan test
    • 如果是剛建立的laravel專案,應該會出現錯誤訊息,因為在初始頁面找不到TDD_Blog
    • Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
    • 如上圖修改後,才可通過測試

資料庫

  • php artisan make:test PostTest
    • Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
    • 直接在路由設定回傳'All Posts:'
    • Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
  • php artisan make:model Post -mf
    • -m 參數會同時創造等等用來 Migrate 的檔案。
    • -f 參數就是代表同時也建立相對應的 Factory 函式的檔案。
  • 先手動新增測試用的資料
    • INSERT INTO posts (post_text) VALUES ("Hello, I'm Louis.");
  • 在PostTest新增public function testInsertTestPost
    • Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
  • 在原本testAllPost中增加一行測試
    • $response->assertSee("Hello, I'm Louis."); //目前的文章
    • Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
    • 本來應該要有',但是在windows中似乎有編碼問題。後選擇圖中所示測試
    • Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
  • Laravel 關聯式資料庫
    • 123

UI測試

使用紀錄