这是用户在 2024-9-30 20:28 为 https://edstem.org/au/courses/17312/lessons/55753/slides/377529 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?

7.2.3 Testing heuristics 7.2.3 测试启发式

There are typically many tests that we can perform on programs that we write, and it is important that we do the testing efficiently. Testing should be planned strategically so that we use the minimum number of tests possible to comprehensively and thoroughly test our programs. We should design both positive and negative tests, but there are other considerations. Here we will outline three testing heuristics that must be taken into account when testing a program. Testing heuristics are 'rules-of-thumb' or simple strategies to help guide testing.
通常,我们可以对自己编写的程序执行许多测试,因此我们必须有效地进行测试。应战略性地规划测试,以便我们使用尽可能少的测试次数来全面彻底地测试我们的程序。我们应该同时设计阳性和阴性测试,但还有其他考虑因素。在这里,我们将概述在测试程序时必须考虑的三种测试启发式方法。 测试启发式是帮助指导测试的“经验法则”或简单的策略。

Boundary testing 边界测试

When performing boundary testing, we must check all boundaries in the specification. The testing for boundaries should include the exact upper and lower limits as well as the nearest values above the upper limit and below the lower limit. This ensures that we test all aspects pertaining to the boundary specified within the specifications.
在执行边界测试时,我们必须检查规范中的所有边界。边界测试应包括确切的上限和下限以及高于上限和低于下限的最接近值。这可确保我们测试与规范中指定的边界相关的所有方面。

For example, if a report could only contain a minimum of 10 lines and a maximum of 60 lines, there would be four sets of data which would need to be tested:
例如,如果报表只能包含最少 10 行和最多 60 行,则需要测试四组数据:

  1. The first set of data would be exactly 60 lines on the report - the upper limit.
    第一组数据正好是报告上的 60 行 - 上限。

  2. The second set of data would be exactly 10 lines on the report - the lower limit.
    第二组数据正好是报告上的 10 行 - 下限。

  3. The third set of data would be 61 lines on the report (the nearest value above the upper limit).
    第三组数据将是报表上的 61 行(高于上限的最接近的值)。

  4. The fourth set of data would be 9 lines on the report (the nearest value below the lower limit).
    第四组数据将是报告上的 9 行(低于下限的最接近的值)。

The above four tests would comprehensively test that the repot had the required number of lines on each page.
以上四个测试将全面测试 repot 是否在每页上具有所需的行数。

Control flow testing 控制流测试

When testing a program an important consideration is control flow, which is the sequence of execution of code. Control flow can be changed by instructions in your code such as selection constructs that cause branching or iteration constructs that cause looping. Consider the following code below and answer the question which follows:
在测试程序时,一个重要的考虑因素是控制流,即代码的执行顺序。控制流可以通过代码中的指令进行更改,例如导致分支的选择构造或导致循环的迭代构造。考虑下面的代码并回答下面的问题:

public String calculateGrade(double mark) { if (mark < 0) return "Invalid"; if (mark < 50) return "N"; if (mark < 60) return "C"; if (mark < 70) return "D"; if (mark < 80) return "HD"; return "Invalid"; }
Java 爪哇岛

As demonstrated in the code above, for each condition there are two possible paths to take. Each time a condition is tested, it directs the flow of control through the program. With five conditions there are multiple possible paths, and each must be tested independently to ensure that each path the program could possibly take is error free.
如上面的代码所示,对于每个条件,都有两条可能的路径可供选择。每次测试条件时,它都会引导控制流通过程序。有五个条件时,有多个可能的路径,每个路径都必须独立测试,以确保程序可能采用的每条路径都没有错误。

Precision testing 精密检测

The last testing heuristic concerns precision when dealing with numeric data, for instance:
最后一个测试启发式方法涉及处理数字数据时的精度,例如:

  • when double or floating-point numbers are involved, the testing needs to consider the precision of the input data. For example, if the input data can contain up to 5 decimal places, then the test data should have 5 decimal places.
    当涉及双精度或浮点数时,测试需要考虑输入数据的精度。例如,如果输入数据最多可以包含 5 个小数位,则测试数据应具有 5 个小数位。

  • as another example, if converting marks to grades you should find out to what precision a mark can be recorded. For example, if it is to be one decimal place, your testing should include 49.9 and 50.0. If it is to 2 decimal places, the testing should include 49.99 and 50.00.
    再举一个例子,如果将分数转换为成绩,您应该了解可以记录分数的精度。例如,如果要保留一个小数位,则您的测试应包括 49.9 和 50.0。如果精确到小数点后 2 位,则测试应包括 49.99 和 50.00。

Care should be taken when comparing floating point numbers for equality as the representation in the machine may not be exact.
在比较浮点数是否相等时应小心,因为机器中的表示可能不准确。