最近讀到一篇深得我心的文章 Design for Testability 核心一句話概括就是:
Optimize your code for testability
為了可測試性而優(yōu)化你的代碼.
這樣不僅可以提升測試的質(zhì)量, 也可以提升代碼的質(zhì)量, 文中提出幾個(gè)收益:
A preference for pure functions over immutable data
會(huì)為了好寫測試而偏重于寫一些 pure function
Small modules with well-defined interfaces
模塊化也會(huì)做的好.
A separation of IO and computation
IO 相關(guān)邏輯與計(jì)算邏輯會(huì)分開
Explicit declaration of dependencies
會(huì)有明確的依賴的聲明, 便于測試
后續(xù)作者還補(bǔ)充了一篇 How I Test, 也值得一讀. 不過這事兒還是要靠實(shí)踐, 對吧.
擴(kuò)展一下, 還可以讀讀 Microservice Testing: Introduction, 提出要 Build a Testing Culture
, 也蠻有道理的.
總之一句話, 不寫測試的代碼都是垃圾.