Testing and Test-First Programming

Testing levels

  • Unit testing 单元测试
    测试某一小部分代码的正确性,尤其是测试某个函数。
  • 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 系统测试
    测试一整个已经集成好的系统,看这个系统是否满足需求,即在最终的一个配置下运行软件。

Test vs Debug

Testing是为了发现是否存在错误
Debugging为了识别、定位已知错误的根源

White-box vs black-box testing

白盒测试是对程序内部代码进行的测试。
黑盒测试仅针对陈旭外部表现出的行为进行测试。
软件测试的困难所在:穷举、暴力测试是不现实的。

Test case

测试用例=输入+执行条件+期望结果
好的测试用例需要有一下属性:
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

测试优先的编程:在写程序代码之前先写测试用例。
不要把测试留到最后。
测试用例要根据方法的规约(specification)来写,同时,写测试用例也是理解、修正、完善规约的过程。
(规约本身也有可能是错误的——不正确、不完整。)
测试优先可以尽早的发现规约中的问题,避免浪费时间去做错误的事情。
越早发现错误就越容易去纠正错误。

Unit Testing

Unit testing focuses verification effort on the smallest unit of software design —— the software component or module.
单元测试需要考虑的东西:
接口,测试的输入和输出;数据的完整性;所有语句都要被执行到过;所有的边界都要测试到。
Test cases for the black-box testing are built around specification and requirements. 测试用例的编写必须符合规约。

Choosing Test Case by Partitioning

按照等价类划分设计测试用例
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.
针对每个输入数据需要满足的约束条件,划分等价类。
此方法可以将有限的测试资源最大化利用。

Guidelines

如果限定了输入数据的范围,则按不同范围划分;
如果指明了特定的值,则将几个特定的值按内在联系划分;
如果输入数据是Y/N,将两个都测一遍。

Example1

BigInteger.multiply():
BigInteger × BigInteger -> BigInteger
input: (a, b),从正负的角度对二维空间进行等价类划分:
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
考虑输入的上限:面对很大的数是否仍然正确?
综合以上的所有,划分为49个等价类:

从每个正方形内任意选取一组(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.

边界值分析方法是对等价类划分方法的补充。

重新设计max()的规约:
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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值