2014-12-18

[讀書筆記] Design and Testability

Design and Testability

1. Why should I care about testability in my design?

In a testable design, each logical piece of code (loops, ifs, switches, and so on) should be easy and quick to write a unit test against, one that demonstrates these properties:

FICC

  • Fast
    • Runs fast
  • Isolated
    • Is isolated, meaning it can run independently or as part of a group of tests, and can run before or after any other test
  • Configuration-free
    • Requires no external configuration
  • Consistent
    • Provides a consistent pass/fail result
If you were doing test-driven development, you’d have no choice but to write a testable system.

2. Design goals for testability

2.1 Make methods virtual by default

Java makes methods virtual by default, but in .NET, you need to explicitly set it as virtual so you can override it in a default class.

An alternative is to have the class invoke a custom delegate.

Using virtual methods is handy, but interface-based designs are also a good choice.

2.2 Use interface-based designs

Refer to chapter 3~5

2.3 Make classes nonsealed by default

If you can’t inherit from a class, you can’t override any virtual methods in it.

2.4 Avoid instantiating concrete classes inside methods with logic

Tests might need to control what instance is used in the class under test.

Solution:
(1) Use the external tools such as Mock tool
(2) Ways in the 3rd chapter such as Factory Method.

2.5 Avoid direct calls to static methods

Try to abstract any direct dependencies that would be hard to replace at runtime.

Solution:
(1) Ways in the 3rd chapter      
      Abstract a static method away using the Extract and Override refactoring.(2) Avoid using any static methods whatsoever
(3) Trying to minimize the number of singletons or static methods

2.6 Avoid constructors and static constructors that do logic

Things like configuration-based classes are often made static classes or singletons because so many parts of the application use them.

在Constructor建立多個邏輯或是static constructor 會因為很多地方有使用到導致難以替換

Solution:
(1) IoC Container

2.7 Separate singletons and singleton holders


SRP: Single Responsibility Principle

原則上就是將該做事的Class跟產生Singleton的Class權責分開
好處是 :
(1) SRP
(2) 可以替換

Sample:

原本的設計 Singleton

Singleton Holders抽出


3. Pros and cons of designing for testability


  • Amount of work
    • Let’s just say that more code, and work, is required when testability is involved, but that designing for testability makes you think about the user of your API more, which is a good thing.
    • 工作量增加是一定的 但是能多用User的角度來設計 這是好事
  • Complexity
    • Over design
    • 能用Tool來克服 (e.g. ReSharper)
  • Exposing sensitive IP
    • IP: Intellectual Property
    • Workaround : 
      • keeping things internal and using the [InternalsVisibleTo] attribute
    • Compromise
  • Sometimes you can’t
    • Political or other reasons


4. Alternatives to designing for testability

If a tool comes along that solves the testability problem for you, there will be no need to design specifically for testability.

5. Summary


Testable designs are what SOLID design principles have stood for. 
The end goal should not be testability, but good design instead.

Related Posts:

  • [心得文] 自動測試與TDD實務開發 Day1 [前情提要] [反省] 學習是要正面主動積極的 其實半年前我就想報名這堂課,不過可惜最後因為幾個負面思考而打消了念頭,當時在心中埋下了一顆悔恨的種子,告訴自己說,下次再有這個機會請不要錯過。 終於,今年盼到了第三梯的課程,雖然價格提高了4成 =.= ,不過還是自己能夠負擔的範圍啦,當然就報囉。 剛好今年也給了自己一些明確的目標: 1.     2015Q4 要在公司教授Unit Testing的… Read More
  • [讀書筆記][小故事][制度殺人] 為什麼要導入SCRUM [讀書筆記][小故事][制度殺人] 為什麼要導入SCRUM 在書上讀到了一則不有趣但是又發人深省的故事。 Milgram Experiment,米爾格倫實驗 米爾格倫實驗(Milgram experiment),又稱權力服從研究(Obedience to Authority Study)是一個針對社會心理學非常知名的科學實驗。 這個實驗室要求受測者擔任老師的角色,教導另一間房間的學生(演員),老師與學生分處不同房間,彼此看不見對方,… Read More
  • [讀書筆記] [Continuous Delivery] Build and Deployment ScriptingBuild and Deployment Scripting Introduction 簡單的專案可以用簡單的方法進行建置與部署 只要專案規模稍微複雜點 建置與部署就會出現以下問題 : 步驟會越來越繁複 所需時間也會越長 人工容易出錯 強烈建議使用Script來達成建置與部署的自動化 An Overview of Build Tools Make & SCons Ant NAnt and MSBuild Maven R… Read More
  • [心得文] 自動測試與TDD實務開發 Day2 Day2 的教學順序是先教我們怎麼使用Selenium做Web-Testing;中間則是介紹了FluentAutomation以及Page Objects;最後則是帶到了Refactoring。 而我的觀戰重點則是在於Refactoring。還記得以前我的Refactoring都是耗時費力,由上往下的設計模式。Refactor完也不敢保證有沒有side effect。Day2則是學到了如何有效攻克Refactoring。確實相當實用阿~ … Read More
  • [心得] Improvement Kata工作坊 - 讓員工自己變優秀的解藥(之一) 2015.05.21 Improvement Kata工作坊 - 讓員工自己變優秀的解藥(之一) 今晚參加Agile Meetup 2015 5月份聚會 今天的主題是講Improvement Kata 主要的重點在於 :  1. 了解現況 2. 確定目標 3. 持續改進 改進的重點在於套用kata的套路  持續練習固定的節奏與步驟來達成目標 … Read More

0 意見: