White-box testing
把测试对象看作一个打开的透明盒子。测试时,利用程序内部的逻辑结构及有关信息,通过在不同点检查程序状态,检验程序中的每条通路是否都能按预定要求进行正确工作。
流程图的构造
Control Flow Graph (CFG)
不同测试的流程图原理基本是相同的,只是测试用例表不同。
If-Then-Else
Case / Switch
Pre-test Loop
Post-test Loop
相关应用
Represented as a regular expression
- Concatenation
.
: represen a sequence of nodes in the graph
- Selection
+
: represents a decision in the graph
- Iteration
*
: represents a repetition in the graph.
Calculate the Number of Paths
- Replacing node numbers and nulls by the value 1 .
- Evaluate the expression as a mathematical formula.
- The selection becomes addition.
- The concatenation becomes multiplication.
例子
Loops
Simplifying:
0
: a null iteration
Identify all paths
Null else
There is an
if
, but without else
, the expression (n + 0) is used.- The node n represents the true decision.
- The node 0 represents the null else decision.
Switch
语句覆盖
Statement Coverage
Make sure that every line of code has been executed during testing.
- Each node on the CFG is a test case
Test Cases
Test Data
分支覆盖
Branch Coverage
Make sure that all branches in the source code have been checked during testing.
- Each branch in the source code is a test case.
Test Case
Test Data
条件覆盖
Condition Coverage
Every condition in every decision to take on the values
true
and false
.Test Case
Test Data
Multiple Condition Coverage
(An example for Line 6: the compound condition only)
Test Case
Test Data
Condition Decision Coverage
Decisions containing multiple conditions. It supplement to the Condition Coverage.
Test Case
Identify the Sequences of Decisions
Can we have two Sequences, i.e.,
{CDC-1, CDC-3, CDC-5} and {CDC-2, CDC-4, CDC-6}, instead of three❓
- No. The Condition CDC-4 and CDC-6 can NOT be satisfied at the same time.
(短路求值)&&:当前一个已经被判断 false,则不会继续判断下一个条件
Test Data
Modified Condition Decision Coverage
Test Case
In addition to Decision Condition Coverage, a Modified Condition Decision Coverage examines the independent
effect of each condition from Condition Decision Coverage on the output.
Test Data
路径覆盖
Path Coverage
Path testing causes every possible path from entry to exit of the program to be exercised during testing. Each unique path from start to finish is a test case.
Test Case
Test Data
数据流覆盖
变异测试
Mutation Testing: a form of “fault-insertion” testing
In Mutation Testing faults are inserted into “mutations” of the source code, and the tests (developed using one of the previous techniques) are run against these.
- A mutation operator is a rule to derive mutants from a software under testing.