Testing and Test-First Programming
Testing levels
- Unit testing 單元測試
測試某一小部分代碼的正確性,尤其是測試某個函數(shù)。 - Integration testing 集成測試
The combined execution of 2 or more classes, packages, components, subsystems that have been created by multiple programmers or programming teams. - System testing 系統(tǒng)測試
測試一整個已經(jīng)集成好的系統(tǒng),看這個系統(tǒng)是否滿足需求,即在最終的一個配置下運行軟件。
Test vs Debug
Testing是為了發(fā)現(xiàn)是否存在錯誤
Debugging為了識別、定位已知錯誤的根源
White-box vs black-box testing
白盒測試是對程序內(nèi)部代碼進(jìn)行的測試。
黑盒測試僅針對陳旭外部表現(xiàn)出的行為進(jìn)行測試。
軟件測試的困難所在:窮舉、暴力測試是不現(xiàn)實的。
Test case
測試用例=輸入+執(zhí)行條件+期望結(jié)果
好的測試用例需要有一下屬性:
Most likely to catch the wrong
Not repetitive and not redundant
The most effective in a group of similar test cases
Neither too simple nor too complicated
Test-First programming
測試優(yōu)先的編程:在寫程序代碼之前先寫測試用例。
不要把測試留到最后。
測試用例要根據(jù)方法的規(guī)約(specification)來寫,同時,寫測試用例也是理解、修正、完善規(guī)約的過程。
(規(guī)約本身也有可能是錯誤的——不正確、不完整。)
測試優(yōu)先可以盡早的發(fā)現(xiàn)規(guī)約中的問題,避免浪費時間去做錯誤的事情。
越早發(fā)現(xiàn)錯誤就越容易去糾正錯誤。
Unit Testing
Unit testing focuses verification effort on the smallest unit of software design —— the software component or module.
單元測試需要考慮的東西:
接口,測試的輸入和輸出;數(shù)據(jù)的完整性;所有語句都要被執(zhí)行到過;所有的邊界都要測試到。
Test cases for the black-box testing are built around specification and requirements. 測試用例的編寫必須符合規(guī)約。
Choosing Test Case by Partitioning
按照等價類劃分設(shè)計測試用例
Equivalence partitioning is a testing method that divides the input domain of a program into classes of data from which test cases can be derived.
針對每個輸入數(shù)據(jù)需要滿足的約束條件,劃分等價類。
此方法可以將有限的測試資源最大化利用。
Guidelines
如果限定了輸入數(shù)據(jù)的范圍,則按不同范圍劃分;
如果指明了特定的值,則將幾個特定的值按內(nèi)在聯(lián)系劃分;
如果輸入數(shù)據(jù)是Y/N,將兩個都測一遍。
Example1
BigInteger.multiply():
BigInteger × BigInteger -> BigInteger
input: (a, b),從正負(fù)的角度對二維空間進(jìn)行等價類劃分:
a and b are both positive
a and b are both negative
a is positive, and b is negative
a is negative, and b is positive
需要考慮的特殊情況:0
考慮輸入的上限:面對很大的數(shù)是否仍然正確?
綜合以上的所有,劃分為49個等價類:
從每個正方形內(nèi)任意選取一組(a, b)
Example 2
public static int max(int a, int b)
int × int -> int
按如下等價類:
a < b
a = b
a > b
Boundary Value Analysis
A greater number of errors occurs at the boundaries of the input domain rather than in the center.
0 is a boundary between positive numbers and negative numbers.
Maximum and minimum values of numeric types, like int and double.
Emptiness (the empty string, empty list, empty array) for collection types.
The first and last element of a collection.
邊界值分析方法是對等價類劃分方法的補充。
重新設(shè)計max()的規(guī)約:
Relationship between a and b:
a < b
a = b
a > b
Value of a:(so is b)
a = 0
a < 0
a > 0
a = minimum integer
a = maximum integer
White-box testing
Whitebox testing (also called glass box testing) means choosing test cases with knowledge of how the function is actually implemented.
For example, if the implementation selects different algorithms depending on the input, then you should partition according to those domains.
If the implementation keeps an internal cache that remembers the answers to previous inputs, then you should test repeated inputs.
Other things
Running all your tests after every change is called regression testing.
Unit testing strategy is a complementary document of ADT’s design.
Aligning with the idea of test-first programming, it is recommended to write down the testing strategy (such as partitioning and boundary) according to which you design your test cases.
總結(jié)
以上是生活随笔為你收集整理的Testing and Test-First Programming的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件构造的视图与质量指标
- 下一篇: 程序人生 Hello‘s P2P