文章目录
Test Oracles
测试的三部分:
-
test input
-
expected output
-
testing environment
-
执行测试用例的正常流程是:
- 使用测试用例中的输入来执行程序,记录结果,
- 然后确定所获得的输出是否为失败。
-
谁或什么决定了程序产生的结果是否为失败?一种方式是由人工测试员查看输入并手动计算他们认为输出应该是什么。在这种情况下,人工测试员扮演着一个 test oracle 的角色。
-
一个 test oracle 是指某个人或某个事物判断程序是否通过了或未通过了该测试用例。当然,它也可以是另一个返回“yes”(表示实际结果不是失败)或“no”(表示实际结果为失败)的程序。
- test oracle 决定了根据当前的 test input, 系统给出的 output 是否 fail (充当了判官的角色)
- 理想情况下,Oracle 应该是自动化的(在运行时没有人为干预),因为这样我们可以执行更多的测试用例并获得程序更广泛的覆盖率,但实际上这通常并不容易。
Active and Passive Test Oracles
-
自动化预言可以分为两类:
-
主动预言(active oracle): 一个程序,给定一个待测试程序的输入,能够生成该输入的期望输出。
-
被动预言(passive oracle): 一个程序,给定一个待测试程序的输入和该待测试程序产生的实际输出,验证实际输出是否正确。
-
一般情况下更倾向于使用 被动预言。这有两个主要原因。
-
Easier Implementation 实现更简单
- Active oracles might require simulating the entire program-under-test. 主动 oracle 可能需要模拟整个被测程序。
- Passive oracles simply check the correctness of the output. 被动 oracle 只需检查输出是否正确。
- Example: Testing a sorting program is easier by verifying the output list is sorted rather than sorting the list itself. 例子:通过验证输出列表是否排序,而不是对列表本身进行排序,来测试排序程序更容易。
- Reduces chances of faults in the oracle itself. 减少了 oracle 自身出错的可能性。
-
Handling Non-determinism 处理非确定性问题
- Programs can return multiple outputs for a single input (non-determinism). 程序可以对于同一输入返回多个输出(非确定性)。
- Active oracles might produce different outputs than the program-under-test. 主动 oracle 可能会产生与被测程序不同的输出结果。
- Passive oracles don’t face this issue as they only verify the correctness of the output. 被动 oracle 不会面临这个问题,因为它们只验证输出是否正确。