別把單測玩出“壞味道”

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#006400","name":"user"}}],"text":"前言","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"實際上,我從未想過,也從未打算過去寫一篇關於單元測試(下述簡稱“單測”)的文章。早期我主要all in在電商領域,由於業務增速較快,我不可能要求開發人員花大量的美國時間去編寫單測代碼,以及滿足單測覆蓋率等硬性指標,換句話說,","attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#000080","name":"user"}}],"text":"站在特定的業務角度而言,單測並非是強訴求,只要測試團隊資源足夠充裕(空間換時間),那麼寫不寫單測最終達成的效果其實是一致的,這是事實","attrs":{}},{"type":"text","text":"。在我加入支付寶之後,面對支付業務的特殊性和複雜性,以及測試團隊資源的嚴重匱乏,註定了單測的必要性和強制性。從理論上來說,85%的bug可以在單測階段被發現,被修復,從整個迭代交付週期來看,單側的覆蓋率越高,項目整體的交付時間和缺陷數就越少。當然,論證單測的必要性似乎並無太大意義,最終還是要結合具體的業務場景而定,沒有絕對的對與錯,但把單測過度複雜化,陷於固定思維去思考問題和嘗試說服別人,就一定是不對的,容易導致出現“跪久了,站不起來;站久了,跪不下去”的局面。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假設你所在的企業對單測有強訴求,那麼單測應該由誰來寫?怎麼寫?這是接下來本文討論的重點。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#006400","name":"user"}}],"text":"單測的本質","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"單測毋庸置疑應該由開發人員來負責編寫,你的邏輯,你最熟悉,而不是交給測試同學,我認爲,","attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#000080","name":"user"}}],"text":"測試團隊的核心關注點應該是集中在功能測試階段發力","attrs":{}},{"type":"text","text":"。那麼如何定義單測?簡單來說,單測代碼就是在特定的條件下對目標代碼執行正確性驗證的一種特殊代碼,其中關於“單元”的定義,我認爲並不需要過於糾結,它的範圍可以是一個目標函數,但同樣也允許是一個類。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"實際上,我並不是專業的測試人員,對於單元測試、增量測試、全量測試、迴歸測試、集成測試,以及冒煙測試等不勝枚舉的測試方法及手段常常是望洋興嘆,甚至無法準確歸類。實際上,我比較贊成和認同google的做法,對於測試,簡單分爲3類,沒那麼多花樣,如圖1所示。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/03/0325382162684fa88331c74881ddf46a.png","alt":null,"title":"圖1 google軟件測試分類","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"boxShadow"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其中小型測試僅關注目標函數的邏輯性驗證,其它部分全部mock,強調的是效率;而中型測試關注的是多模塊之間的交互性驗證;最後大型測試則顯得比較重和全面,我們可以理解爲集成測試。那麼我們的單元測試究竟應該如何歸類呢?顯而易見,單元測試歸類在小型測試中是最適合不過的。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#006400","name":"user"}}],"text":"單測究竟應該怎麼寫","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#000080","name":"user"}}],"text":"單測的編寫,最重要的就是要寫得輕薄,強調的是效率,那就必須採取小步快跑模式","attrs":{}},{"type":"text","text":",如果單測代碼還需要考慮連接數據庫去驗證SQL語法以及語義層面的正確性、訪問各類中間件,甚至是觸發RPC調用,那麼這就不叫做單測,而是集成測試。","attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#000080","name":"user"}}],"text":"實際上,筆者所在的團隊,甚至大量的互聯網企業對於單測的定位均不清晰,其中最主要的問題我認爲還是把單測當做了產品質量保證的銀彈","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"部分開發者認爲,單測環節是整個測試環節中最底層,同時也是最重要的一個測試環節,全面的測試驗證可以大幅縮短集成測試和迭代交付的時間窗口,後續放心重構,以及降低bug滯後的修復成本等,因此把大量的測試驗證工作都前置在單測環節進行。而我認爲,","attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#000080","name":"user"}}],"text":"基礎測試環節做的過重,只會弊多於利,反而會延長交付週期,增加單測代碼的開發成本、維護成本,甚至導致開發人員走形式,走過場去編寫單測代碼","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在單測環節,開發人員僅需關注邏輯單元的正確性,其它部分,該mock的就mock,想要跑得快,那就必須丟包袱,在合適的環節去做正確的事,不要總是想着集中在某一個點能解決所有問題。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":"center","origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"====== END ======","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"至此,本文內容全部結束。如果在閱讀過程中有任何疑問,歡迎在評論區留言參與討論。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"推薦文章:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/afb69dbad5329096215ffe9e1","title":"","type":null},"content":[{"type":"text","text":"源碼系列 | 阿里 JVM-Sandbox 核心源碼剖析","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/8304c894c4e38318d38ceb116","title":"","type":null},"content":[{"type":"text","text":"實操 | 剖析 Java16 新語法特性","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/6ad9f5278c0981de6971c70f4","title":"","type":null},"content":[{"type":"text","text":"專訪 | 我與畢玄大師的對話","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/ae514bc05af1f16b151a351f8","title":"","type":null},"content":[{"type":"text","text":"算力 | 手寫紅黑樹","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/d367c19896e4cef6fbb661cf7","title":"","type":null},"content":[{"type":"text","text":"硬核系列 | 深入剖析字節碼增強","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/cef6d2931a54f85142d863db7","title":"","type":null},"content":[{"type":"text","text":"硬核系列 | 深入剖析 Java 協程","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/61868b3f66de36d32a5f1434f","title":"","type":null},"content":[{"type":"text","text":"白玉試毒 | 灰度架構設計方案","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/655943e5f85e6f79ffbd03047","title":"","type":null},"content":[{"type":"text","text":"新時代背景下的 Java 語法特性","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/92ba88c7926b5f5c6fbc11830","title":"","type":null},"content":[{"type":"text","text":"剖析 Java15 新語法特性","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/4d571787a3280ef3094338f9b","title":"","type":null},"content":[{"type":"text","text":"看門狗 | 分佈式鎖架構設計方案 -01","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/545a3accd173d6517ebd0ad59","title":"","type":null},"content":[{"type":"text","text":"看門狗 | 分佈式鎖架構設計方案 -02","attrs":{}}]}]}]}],"attrs":{}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章