目录
5 Automated Unit Testing with JUnit
6.1 Choosing Test Cases by Partitioning
6.2 Include Boundaries in the Partition
1 Software Testing
1.测试的目的
提高软件质量的重要手段
发现bugs, 确认是否达到可用级别(用户需求)
关注系统的某一侧面的质量特性
2.测试等级
单元测试
集成测试
系统测试
回归测试
3.动态测试和静态测试
静态测试:程序加载运行之前的测试。
动态测试:程序运行过程中对程序行为和代码的测试。
2 Test Case
测试用例:输入+执行条件+期望结果
测试用例的要求:最可能发现错误;不重复、不冗余;最有效;既不简单也不复杂
3 Test-First Programming
过程:
1.先写spec
2.再写符合spec的测试用例
3.写代码、执行测试、有问题再改、再执行测试用例,直到通过它
优势:
更早发现问题,越容易纠正。
更深入和更早的理解需求和设计
更早地暴露需求和设计的问题
可确保程序的可测试性
确保每个程序特征的测试都撰写完成
4 Unit Testing
针对软件的最小单元模型开展测试,隔离各个模块,容易定位错误和调试。
测试原则:
接口,测试输入输出
数据的一致性
所有语句均被执行到
边界条件测试
错误路径测试
5 Automated Unit Testing with JUnit
1.在每个测试方法前面使用@Test标注指明
2.调用assert类语句,例如assertEquals, assertTrue, and assertFalse
注意:
assertEquals参数的顺序:第一个参数是期望的结果,第二个参数是实际的结果
当一个测试遇到失败的时候,这个测试方法直接返回
每个@Test是独立运行的
assert:
assertArrayEquals("failure - byte arrays not same", expected, actual);
assertEquals("failure - strings are not equal", "text", "text");
assertFalse("failure - should be false", false);
assertNotNull("should not be null", new Object());
assertNotSame("should not be same Object", new Object(), new Object());
assertNull("should be null", null);
assertSame("should be same", aNumber, aNumber);
assertTrue("failure - should be true", true);
assertThat("albumen", both(containsString("a")).and(containsString("b")));
assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));
assertThat("good", allOf(equalTo("good"), startsWith("good")));
6 Black-box Testing
黑盒测试:用于检查代码的功能,不关心内部实现细节,检查程序是否符合规约.
6.1 Choosing Test Cases by Partitioning
基于等价类划分的测试:将被测函数的输入域划分为等价类,从等价类中导出测试用例。针对每个输入数据需要满足的约束条件,划分等价类。
例1:
例2:
BigInteger(a) × BigInteger(b) → BigInteger
6.2 Include Boundaries in the Partition
大量的错误发生在输入域的“边界”而非中央,边界值分析方法是对等价类划分方法的补充。
常见的边界:
– 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
笛卡尔积全覆盖
多个划分维度上的多个取值,要组合起来,每个组合都要有一个用例
7 White-box Testing
白盒测试要考虑内部实现细节。根据程序执行路径设计测试用例。
8 Coverage of Testing
代码覆盖度:已有的测试用例有多大程度覆盖了被测程序。
覆盖种类:
函数覆盖
语句覆盖
分支覆盖
条件覆盖
路径覆盖
JAVA使用EclEmma测试覆盖度。