📚 Computer Science: Principles and Design Methods for Selecting Test Data | 计算机:测试数据的选择原则与设计方法
Testing is a critical phase of software development that verifies whether a program behaves correctly under a wide range of inputs. Without carefully selected test data, even a well-written program can contain hidden errors that only appear under specific conditions. This article discusses the principles of selecting test data and outlines practical methods for designing test cases, in line with the CIE A-Level Computer Science syllabus.
测试是软件开发中至关重要的阶段,用于验证程序在各种输入下是否行为正确。如果没有精心选择的测试数据,即使是编写良好的程序也可能隐藏着只在特定条件下才出现的错误。本文围绕 CIE A-Level 计算机科学大纲,讨论测试数据选择的原则,并概述设计测试用例的实用方法。
1. Why Test Data Selection Matters | 1. 为什么测试数据的选择至关重要
Successful compilation does not guarantee a correct program. Many logical errors only emerge when the program is executed with particular inputs. Selecting suitable test data helps uncover these errors quickly and systematically. It also provides documented evidence that the program meets its specification.
编译成功并不能保证程序正确。许多逻辑错误只有在使用特定输入执行程序时才会显现。选择合适的测试数据有助于快速、系统地发现这些错误,同时也能提供有据可查的证据,证明程序符合规格要求。
2. Categories of Test Data | 2. 测试数据的分类
Test data is usually divided into three broad categories: normal, boundary and invalid data. Normal data are typical valid inputs, boundary data are inputs at the edges of the valid range, and invalid data are inputs outside the acceptable range. A well-designed test suite must include all three types to achieve good coverage.
测试数据通常分为三大类:正常数据、边界数据和无效数据。正常数据是典型的有效输入,边界数据是位于有效范围边缘的输入,无效数据则是超出可接受范围的输入。一个好的测试集必须包含这三种类型,才能实现良好的覆盖率。
3. Equivalence Partitioning | 3. 等价类划分
Equivalence partitioning is a black-box technique that divides the possible inputs into groups, or partitions, where the program is expected to behave the same way for every input in a group. For example, if a program accepts marks from 0 to 100, the input domain can be divided into three equivalence classes: valid marks from 0 to 100, invalid marks below 0, and invalid marks above 100. Testing one representative from each class is sufficient to expose most errors in that class.
等价类划分是一种黑盒测试技术,它将可能的输入划分为若干组或等价类,并对同一组中的所有输入预期程序的行为相同。例如,如果程序接受0到100之间的分数,则输入域可分为三个等价类:0到100之间的有效分数、低于0的无效分数和高于100的无效分数。从每个类中测试一个代表值足以发现该类中的大多数错误。
4. Boundary Value Analysis | 4. 边界值分析
Experience shows that errors often occur at the edges of an input range. A program that uses the condition if score >= 0 and score <= 100 may fail if the programmer writes if score > 0 and score < 100, mistakenly rejecting 0 and 100. Boundary value analysis therefore tests values at, immediately above, and immediately below each boundary. For the range 0 to 100, you would test -1, 0, 1, 99, 100 and 101. This method is highly effective at locating off-by-one errors.
经验表明,错误经常发生在输入范围的边缘。使用条件 if score >= 0 and score <= 100 的程序,如果程序员误写成 if score > 0 and score < 100,就会错误地拒绝0和100。因此,边界值分析要测试每个边界上、紧邻之上和紧邻之下的值。对于0到100的范围,应测试 -1、0、1、99、100 和 101。这种方法对定位差一错误非常有效。
5. Designing Normal Test Data | 5. 设计正常测试数据
Normal test data confirms that the program performs its core function for typical inputs. For each valid equivalence class, you should choose at least one value near the centre of the range, not just values near the boundaries. For example, for a mark range of 0 to 100, 50 is a representative normal value. Normal data should also include a variety of realistic values, such as 23, 74 and 99, to increase confidence in the program’s logic.
正常测试数据用于确认程序在典型输入下能否执行其核心功能。对于每个有效等价类,至少应选择一个接近范围中心的值,而不是只选择边界附近的值。例如,对于0到100的分数范围,50就是一个具有代表性的正常值。正常数据还应包含各种实际值,如23、74和99,以提高对程序逻辑的信心。
6. Designing Boundary Test Data | 6. 设计边界测试数据
Boundary test data focuses on the thresholds where the
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply