Test DoublesLi-Wei Cheng
Outline•   Whatre test doubles ?•   Difference between each test double•   Examples•   Compare each test double
Whatre test doubles•   Test double 測試替身    o   A generic term for any case where you replace a        production object.  ...
Difference between each testdoubleTest Double                   Description  Dummy       最簡單、最原始的測試替身型別。Dummy 沒有實作,最常用於需要參...
Example          Validation depends on DAO          Validation depends on Hash
Compare each test doubleTest Double           Pros               Cons  Dummy       很容易建立            不太實用   Stub       容易建立...
Configure the test double
Example             /// pseudo code for CheckAuthentication             /// input: string id             /// input: string...
Example -- Dummy•   Dummy Object    o   Passed around but never actually used. Usually,        they are just used to fill ...
Example -- Stub•   Stub    o   Provide canned answers to calls made during the        test, usually not responding at all ...
Example -- Fake•   Fake    o   Actually have working implementations, but usually        take some shortcut which makes th...
Example -- Spy•   Spy    o   Stubs that also record some information based on        how they were called. One form of thi...
Example -- Mock•   Mock    o   pre-programmed with expectations which form a        specification of the calls they are ex...
Upcoming SlideShare
Loading in …5
×

Test doubles

371 views
290 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
371
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Test doubles

  1. 1. Test DoublesLi-Wei Cheng
  2. 2. Outline• Whatre test doubles ?• Difference between each test double• Examples• Compare each test double
  3. 3. Whatre test doubles• Test double 測試替身 o A generic term for any case where you replace a production object. Usually used in unit test
  4. 4. Difference between each testdoubleTest Double Description Dummy 最簡單、最原始的測試替身型別。Dummy 沒有實作,最常用於需要參數值但不 使用它的情況。Null 可視為是 Dummy,但真的 Dummy 是介面或基底類別的 衍生,且完全不包含實作。 Stub Dummy 的上一級,Stub 是介面或基底類別的最低限度實作。會傳回 Void 的方法通常完全不包含實作,而會傳回值的方法通常會傳回 Hard Code 的值 。 Spy 測試 Spy 類似 Stub,但除了提供用戶端可叫用成員的執行個體,Spy 還會 記錄叫用了哪些成員,好讓單元測試驗證所叫用的成員是否符合預期。 Fake Fake 包含更複雜的實作,通常涉及所繼承型別之不同成員之間的互動。雖然不 是完整的生產實作,但 Fake 與生產實作很相似,儘管它會採取一些捷徑。 Mock Mock 是由 Mock 程式庫動態建立 (其他通常是由測試開發人員使用程式碼來 產生)。測試開發人員永遠看不到實作介面或基底類別的實際程式碼,但是可以 設定 Mock 以提供傳回值、預期要叫用的特定成員...等等。視其中的設定而 定,Mock 的行為可能會像 Dummy、Stub 或 Spy。
  5. 5. Example Validation depends on DAO Validation depends on Hash
  6. 6. Compare each test doubleTest Double Pros Cons Dummy 很容易建立 不太實用 Stub 容易建立 彈性有限。從單元測試觀察比較難懂。無 法驗證是否有正確叫用成員 Spy 可以驗證是否有正確叫用成員 彈性有限。從單元測試觀察比較難懂 Fake 提供半成品的實作,適用於許多 較難建立。可能會因為本身過於複雜而需 不同的案例 要進行自己的單元測試 Mock 可有效建立測試替身。可以驗證 學習曲線較陡峭 是否有正確叫用成員。從單元測 試觀察比較易懂
  7. 7. Configure the test double
  8. 8. Example /// pseudo code for CheckAuthentication /// input: string id /// input: string password /// output: true --> if pass the checking /// false --> else fail the checking /// get the passwords hash result from DAO AccountDAO dao = new AccountDao(); String passwordHashFromDAO = mDao.getPasswordHash(id); mLog.writeLog(passwordHashFromDAO ); /// get the ids hash result from Hash Hash hash = new Hash(); String hashResult = mHash.getHashResult(password); mLog.writeLoghashResult (); /// check the result return passwordHashFromDAO.equals(hashResult);
  9. 9. Example -- Dummy• Dummy Object o Passed around but never actually used. Usually, they are just used to fill parameter list.
  10. 10. Example -- Stub• Stub o Provide canned answers to calls made during the test, usually not responding at all to anything outside whats programmed in for the test.
  11. 11. Example -- Fake• Fake o Actually have working implementations, but usually take some shortcut which makes them not suitable for production.
  12. 12. Example -- Spy• Spy o Stubs that also record some information based on how they were called. One form of this might be an email service that records how many messages it was sent.
  13. 13. Example -- Mock• Mock o pre-programmed with expectations which form a specification of the calls they are expected to receive. They can throw an exception if they receive a call they dont expect and are checked during verification to ensure they got all the calls they were expecting.

×