1.Testing
“Testing can never completely prove the absence of errors”
(1)Testing levels
Unit testing ——单元测试
Integration testing ——集成测试
System testing ——系统测试
Acceptance testing ——验收测试
(2)Static vs. Dynamic testing
Static testing : 没有实际执行程序
Dynamic testing:使用给定的测试用例执行编程代码,测试动态行为
(3)Testing vs. Debugging
Testing:发现是否存在错误
Debugging :识别错误根源,消除错误
(4)White-box vs. black-box testing
White-box testing:对程序内部代码结构的测试
Black-box testing:对程序外部表现出来的行为的测试(不知道内部程序细节)
(5)Our test
Not only “make it fail”, but also “fail fast”.
2.Test Case
(1)What is test case?
test case = {test inputs + execution conditions+ expected results}
(2)Characteristics of good test case
a.最可能发现错误
b.不重复、不冗余
c.最有效
d.既不简单也不复杂
3.Test-First Programming
Write the tests before you write the code.
(1)先写spec
(2)再写符合spec的测试用例
(3)写代码、执行测试、有问题再改、再执行测试用例,直到通过它
4.Unit Testing
Focusing verification effort on the smallest unit of software design!
5.Automated Unit Testing with JUnit
(1)JUnit是一个被广泛采用的Java单元测试框架
(2)JUnit单元测试被写在注释@Test之后
(3)Assertion methods: assertEquals, assertTrue, and assertFalse.
6.Black-box testing
![](https://i-blog.csdnimg.cn/blog_migrate/be96270c2d34038edf144f098e734362.png)
需求说明——产生——测试用例
被测程序——输出——测试结果
7.Choosing Test Cases by Partitioning
1.针对每个输入数据需要满足的约束条件,划分等价类
2.相似的输入将会展示相似的行为,故可从每个等价类中选一个代表作为测试用例即可,从而可以降低测试用例数量
3.Example:BigInteger.multiply()
BigInteger × BigInteger → BigInteger
(1)我们首先从正负对二维空间进行划分:
– a and b are both positive
– a and b are both negative
– a is positive, b is negative
– a is negative, b is positive
(2)其次需要考虑输入数据的特殊情况:
a or b : 0, 1, or -1
(3)考虑输入的上限:很大的数是否仍正确?
the absolute value of a or b is bigger than Long.MAX_VALUE
(4)综上我们选择 a、b:
– 0
– 1
– -1
– small positive integer
– small negative integer
– huge positive integer
– huge negative integer
(5)产生的测试集可以为:
– (a,b) = (-3, 25) to cover (small negative, small positive)
– (a,b) = (0, 30) to cover (0, small positive)
– (a,b) = (2^100, 1) to cover (large positive, 1)
– etc.
4.Example: max()
int × int → int
(1)From the specification, it makes sense to partition this function as:
• a < b
• a = b
• a > b
(2)Our test suite might then be:
• (a, b) = (1, 2) to cover a < b
• (a, b) = (9, 9) to cover a = b
• (a, b) = (-5, -6) to cover a > b
8.Include Boundaries in the Partition
A greater number of errors occur at the boundaries of the input domain rather than in the “center”.
1.Two Extremes for Covering the Partition
(1)笛卡尔积:全覆盖
多个划分维度上的多个取值,要组合起来,每个组合都要有一个用例
测试完备,但用例数量多,测试代价高
(2)覆盖每个取值:最少1次即可
每个维度的每个取值至少被1个测试用例覆盖一次即可
测试用例少,代价低,但测试覆盖度未必高
2.Example: max()
max : int × int → int.
(1)Relationship between a and b:
• a < b
• a = b
• a > b
(2)Value of a:
• a = 0
• a < 0
• a > 0
• a = minimum integer
• a = maximum integer
(3)Value of b:
• b = 0
• b < 0
• b > 0
• b = minimum integer
• b = maximum integer
9.White-box Testing
(1)根据程序执行路径设计测试用例
(2)独立/基本路径测试:
对程序所有执行路径进行等价类划分,找出有代表性的最简单的路径,设计测试用例使每一条基本路径被至少覆盖1次。
10.Coverage of Testing
(1)代码覆盖度:
已有的测试用例有多大程度覆盖了被测程序
代码覆盖度越低,测试越不充分,但要做到很高的代码覆盖度,需要更多的测试用例,测试代价高
(2)覆盖分类:
函数覆盖、语句覆盖、分支覆盖、条件覆盖、路径覆盖
测试效果:路径覆盖>分支覆盖>语句覆盖
测试难度:路径覆盖>分支覆 盖>语句覆盖