接下來是《Testing Objected-Oriented Systems, Models, Patterns, and tools》(《面向對象系統測試 模型,視圖與工具》)這本大部頭的書,我手中的是清華大學出版社出版的影印本,其中沒有直接的對單元測試的定義,但在描述class scope testing時,有如下描述(P350):
“Class scope testing corresponds with the classical definition of unit testing: exercising a relatively small software component, usually via a driver, in isolation.”
我認為這段話也清楚地表明了作者認為針對類的測試是屬于單元測試的立場。
當然,還有一些書中的對單元測試的描述我也進行了引用,例如,在《The art of Software Testing (2nd)》一書中,單元測試被描述為:
“Module testing (or unit testing) is a process of testing the individual subprograms, subroutines, or procedures in a program. That is, rather than initially testing the program as a whole, testing is first focused on the smaller building blocks of the program.”
在《Pragmatic Unit Testing in C Sharp with NUnit》書中,單元測試描述如下:
“A unit test is a piece of code written by a developer that exercises a very small, specific area of functionality of the code being tested. Usually a unit test exercises some particular method in a particular context. For example, you might add a large value to a sorted list, then confirm that this value appears at the end of the list. Or you might delete a pattern of characters from a string and then confirm that they are gone.
Unit tests are performed to prove that a piece of code does what the developer thinks it should do.”
總之,從以上的內容來看,似乎沒有任何一本書直接支持《軟件測試方法和技術》中提到的這個觀點。
對于單元測試,我的觀點是:
單元測試是針對“單元”的測試,這是毫無疑問的。但何謂“單元”,則并沒有特別明確的區分,在不同的上下文語境中,單元可以是不同的含義。在RUP中,單元測試的對象可以是文件、DLL或是類等等。
從《Systematic Software Testing》提供的單元測試模板來看,在確定單元測試范圍的時候,單元測試范圍的內容可以是“Unit/Module/Component”,可見,在作者看來, 單元測試的范圍可以是Unit(代碼片斷)、Module(模組)或者是Component(構件)。
從目前的TDD和單元測試的發展趨勢來看,在使用面向對象方法開發的系統中,越來越多的人傾向于將單元測試理解為針對類的測試,或是針對幾個緊密聯系的類的測試,并不需要被測單元具有UI層面上的含義(例如,一個純粹的算法類)。
在實際的工作中,具體“單元”的選取應該是根據具體的情況來確定的,沒有一個非常統一的標準。例如,如果該系統大量使用外部的組件(component),單元測試的內容更多的是以組件來進行,但如果系統開發中要求對每個類進行測試,類就應該是單元測試的最好關注單位。
文章來源于領測軟件測試網 http://www.kjueaiud.com/