Testing and Test-First Programming
Testing levels
- Unit testing 單元測(cè)試
測(cè)試某一小部分代碼的正確性,尤其是測(cè)試某個(gè)函數(shù)。 - Integration testing 集成測(cè)試
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)測(cè)試
測(cè)試一整個(gè)已經(jīng)集成好的系統(tǒng),看這個(gè)系統(tǒng)是否滿足需求,即在最終的一個(gè)配置下運(yùn)行軟件。
Test vs Debug
Testing是為了發(fā)現(xiàn)是否存在錯(cuò)誤
Debugging為了識(shí)別、定位已知錯(cuò)誤的根源
White-box vs black-box testing
白盒測(cè)試是對(duì)程序內(nèi)部代碼進(jìn)行的測(cè)試。
黑盒測(cè)試僅針對(duì)陳旭外部表現(xiàn)出的行為進(jìn)行測(cè)試。
軟件測(cè)試的困難所在:窮舉、暴力測(cè)試是不現(xiàn)實(shí)的。
Test case
測(cè)試用例=輸入+執(zhí)行條件+期望結(jié)果
好的測(cè)試用例需要有一下屬性:
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
測(cè)試優(yōu)先的編程:在寫(xiě)程序代碼之前先寫(xiě)測(cè)試用例。
不要把測(cè)試留到最后。
測(cè)試用例要根據(jù)方法的規(guī)約(specification)來(lái)寫(xiě),同時(shí),寫(xiě)測(cè)試用例也是理解、修正、完善規(guī)約的過(guò)程。
(規(guī)約本身也有可能是錯(cuò)誤的——不正確、不完整。)
測(cè)試優(yōu)先可以盡早的發(fā)現(xiàn)規(guī)約中的問(wèn)題,避免浪費(fèi)時(shí)間去做錯(cuò)誤的事情。
越早發(fā)現(xiàn)錯(cuò)誤就越容易去糾正錯(cuò)誤。
Unit Testing
Unit testing focuses verification effort on the smallest unit of software design —— the software component or module.
單元測(cè)試需要考慮的東西:
接口,測(cè)試的輸入和輸出;數(shù)據(jù)的完整性;所有語(yǔ)句都要被執(zhí)行到過(guò);所有的邊界都要測(cè)試到。
Test cases for the black-box testing are built around specification and requirements. 測(cè)試用例的編寫(xiě)必須符合規(guī)約。
Choosing Test Case by Partitioning
按照等價(jià)類(lèi)劃分設(shè)計(jì)測(cè)試用例
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.
針對(duì)每個(gè)輸入數(shù)據(jù)需要滿足的約束條件,劃分等價(jià)類(lèi)。
此方法可以將有限的測(cè)試資源最大化利用。
Guidelines
如果限定了輸入數(shù)據(jù)的范圍,則按不同范圍劃分;
如果指明了特定的值,則將幾個(gè)特定的值按內(nèi)在聯(lián)系劃分;
如果輸入數(shù)據(jù)是Y/N,將兩個(gè)都測(cè)一遍。
Example1
BigInteger.multiply():
BigInteger × BigInteger -> BigInteger
input: (a, b),從正負(fù)的角度對(duì)二維空間進(jìn)行等價(jià)類(lèi)劃分:
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
考慮輸入的上限:面對(duì)很大的數(shù)是否仍然正確?
綜合以上的所有,劃分為49個(gè)等價(jià)類(lèi):
從每個(gè)正方形內(nèi)任意選取一組(a, b)
Example 2
public static int max(int a, int b)
int × int -> int
按如下等價(jià)類(lèi):
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.
邊界值分析方法是對(duì)等價(jià)類(lèi)劃分方法的補(bǔ)充。
重新設(shè)計(jì)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的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 软件构造的视图与质量指标
- 下一篇: 程序人生 Hello‘s P2P