📚 Test Data | 测试数据
In software development, test data refers to the input values used to verify that a program behaves correctly under a range of conditions. Selecting appropriate test data is a crucial skill in IGCSE Computer Science, as it directly determines how thoroughly a program has been tested and how confidently we can rely on its output.
在软件开发中,测试数据是指用于验证程序在多种条件下是否正确运行的一组输入值。在 IGCSE 计算机科学中,选择合适的测试数据是一项至关重要的技能,因为它直接决定了程序测试的全面程度,以及我们对其输出结果的信任程度。
1. What Is Test Data | 什么是测试数据
Test data is a set of inputs deliberately chosen before testing begins. It is used to check whether a program produces the expected outputs, handles errors gracefully, and meets its specification. The choice of test data should be systematic, not random, so that every important path through the program is exercised.
测试数据是在测试开始之前有意选择的一组输入。它用于检查程序是否产生预期的输出、是否能妥善处理错误,以及是否满足其规格说明。测试数据的选择应当系统化而非随意,以确保程序中的每一条重要路径都被执行到。
There are three main categories of test data that students must understand: normal, boundary, and invalid data. Each category serves a distinct purpose and reveals different types of defects in a program.
学生必须理解三类主要测试数据:正常数据、边界数据和无效数据。每一类都有其独特的用途,并能揭示程序中不同类型的缺陷。
2. Normal Data | 正常数据
Normal data, also called valid or typical data, consists of values that are within the expected input range and should be accepted by the program. These values represent everyday, routine inputs that a user would normally enter. The expected result is that the program runs correctly and produces the correct output.
正常数据,也称为有效数据或典型数据,是由预期输入范围内的值组成,程序应当接受这些值。这些值代表用户通常会输入的日常、常规数据。预期结果是程序正常运行并产生正确的输出。
For example, consider a program that calculates a student’s grade from a test score between 0 and 100. Normal data might include values such as 45, 72, or 88. Each of these is clearly within the accepted range and should produce a valid grade.
例如,考虑一个根据 0 到 100 之间的测试分数计算学生等级的程序。正常数据可能包括 45、72 或 88 等值。每个值都明确在可接受范围内,应当产生有效的等级。
When selecting normal data, a good approach is to choose a spread of values across the entire valid range, rather than clustering near one end. This ensures that the program is tested under a variety of typical conditions.
在选择正常数据时,一个好的做法是在整个有效范围内分散选取数值,而不是集中在某一端附近。这可以确保程序在多种典型条件下得到测试。
3. Boundary Data | 边界数据
Boundary data refers to values that lie exactly at the edges of the valid input range, or just on either side of those edges. Boundary testing is based on the observation that many programming errors occur at the extremes of input ranges, such as off-by-one errors in conditions or loops.
边界数据是指恰好位于有效输入范围边缘的值,或刚好在该边缘两侧的值。边界测试基于这样一个观察:许多编程错误发生在输入范围的极端位置,例如条件或循环中的差一错误。
Continuing with the grading program example, if the valid range is 0 to 100, then boundary values include 0, 1, 99, and 100. Additionally, values just below and just above the boundary, such as -1 and 101, may be tested to confirm that the program rejects them appropriately. These near-boundary values are sometimes classified separately, but they are essential to a complete boundary test.
继续以成绩程序为例,如果有效范围是 0 到 100,那么边界值包括 0、1、99 和 100。此外,刚好低于和高于边界的值(如 -1 和 101)也可以被测试,以确认程序能正确拒绝它们。这些接近边界值的输入有时会被单独分类,但它们对于完整的边界测试至关重要。
Boundary testing is especially important when a program uses conditions such as if score >= 50 to determine pass or fail. A value of exactly 50 must be checked to ensure the comparison operator has been written correctly.
当程序使用如 if score >= 50 这样的条件来判断及格或不及格时,边界测试尤为重要。必须检查恰好为 50 的值,以确保比较运算符书写正确。
4. Invalid Data | 无效数据
Invalid data, also called erroneous data, consists of values that are outside the acceptable input range or of the wrong data type. The program should not process these values normally; instead, it should reject them with a suitable error message and continue running gracefully without crashing.
无效数据,也称为错误数据,由超出可接受输入范围或数据类型错误的值组成。程序不应正常处理这些值;相反,它应当以适当的错误信息拒绝它们,并且继续平稳运行而不崩溃。
Examples of invalid data for the grading program might include a score of -5, a score of 250, or a non-numeric input such as “abc”. Each of these should trigger an error-handling routine that informs the user of the problem.
对于成绩程序,无效数据的例子可能包括 -5 分、250 分,或非数字输入如 “abc”。这些输入都应当触发错误处理程序,告知用户问题所在。
Testing with invalid data verifies that validation routines are working correctly. A well-designed program should never crash or produce unexpected output when given invalid input; it should detect the problem and respond appropriately.
使用无效数据进行测试可以验证验证例程是否正常工作。一个设计良好的程序在接收到无效输入时绝不应崩溃或产生意外输出;它应当检测到问题并做出适当响应。
5. Test Plans and Test Data | 测试计划与测试数据
A test plan is a structured document that lists the test data to be used, the reason for choosing each value, the expected output, and the actual output observed during testing. Test plans are written before the testing process begins and are used to record the results systematically.
测试计划是一份结构化文档,列出将要使用的测试数据、选择每个值的原因、预期输出,以及测试过程中观察到的实际输出。测试计划在测试过程开始之前编写,用于系统地记录结果。
A typical test plan table contains the following columns: test number, test description, test data, test type (normal, boundary, or invalid), expected result, and actual result. This structure allows the programmer to quickly identify whether a test has passed or failed.
典型的测试计划表包含以下列:测试编号、测试描述、测试数据、测试类型(正常、边界或无效)、预期结果和实际结果。这种结构使程序员能够快速识别测试是否通过或失败。
| Test No. | Test Data | Type | Expected Output | Actual Output |
| 1 | 45 | Normal | Pass | Pass |
| 2 | 50 | Boundary | Pass | Pass |
| 3 | -5 | Invalid | Error message | Error message |
In the IGCSE examination, students are often asked to complete a test plan table or to suggest suitable test data for a given scenario. Practising this skill is essential for achieving high marks in the programming and algorithms papers.
在 IGCSE 考试中,学生经常被要求填写测试计划表或为给定场景建议合适的测试数据。练习这一技能对于在编程和算法试卷中取得高分至关重要。
6. Why Test Data Matters | 测试数据的重要性
Testing is the primary method of finding errors in a program before it is released to users. Without well-chosen test data, a program may appear to work correctly yet fail when it encounters an unusual input in real-world use. This can lead to data loss, incorrect results, or frustration for the user.
测试是在程序发布给用户之前发现错误的主要方法。如果没有精心选择的测试数据,程序可能看起来工作正常,但在实际使用中遇到不寻常输入时却会失败。这可能导致数据丢失、结果错误或用户体验不佳。
Systematic testing with a variety of data types also increases confidence in the program’s reliability. When a program passes tests for normal, boundary, and invalid data, the programmer can be reasonably assured that the program handles a wide spectrum of possible inputs correctly.
使用多种数据类型进行系统测试也能增强对程序可靠性的信心。当程序通过正常数据、边界数据和无效数据的测试时,程序员可以合理地确信程序能够正确处理各种可能的输入。
Furthermore, thorough testing saves time and money in the long run. Detecting and fixing a bug during development costs far less than correcting a defect after the software has been deployed to thousands of users.
此外,彻底测试从长远来看可以节省时间和金钱。在开发期间发现并修复错误,远比在软件已部署给成千上万用户之后修正缺陷要便宜得多。
7. Choosing Effective Test Data | 选择有效的测试数据
To choose effective test data, one must first examine the program’s specification and identify the valid input range, the expected output for each range, and any special cases or restrictions. This analysis forms the basis for deciding which values to include in each test category.
要选择有效的测试数据,必须首先检查程序的规格说明,确定有效输入范围、每个范围的预期输出,以及任何特殊情况或限制。这一分析构成了决定每个测试类别中应包含哪些值的基础。
A common strategy is to select three values from the normal range: one near the lower end, one near the middle, and one near the upper end. For boundary testing, the smallest valid value, the largest valid value, and values immediately outside these extremes should be chosen. For invalid testing, values of the wrong type and values far outside the range are appropriate.
一个常见策略是从正常范围中选择三个值:一个靠近低端,一个靠近中间,一个靠近高端。对于边界测试,应选择最小有效值、最大有效值,以及刚好超出这些极值的数值。对于无效测试,选择错误类型的值和远超出范围的值较为合适。
It is also important to consider whether the input data will be integer, real, character, or string. A program expecting a character input, for example, should be tested with a string of length greater than one to ensure validation is working correctly.
考虑输入数据是整数、实数、字符还是字符串也很重要。例如,一个期望字符输入的程序,应当用长度大于 1 的字符串进行测试,以确保验证正常工作。
8. Trace Tables and Test Data | 追踪表与测试数据
Trace tables are closely related to test data. A trace table is used to manually simulate the execution of a program or algorithm with a particular set of test data, recording the value of each variable and the result of each condition at every step.
追踪表与测试数据密切相关。追踪表用于以特定测试数据手动模拟程序或算法的执行过程,在每一步记录每个变量的值和每个条件的结果。
Using a trace table with carefully selected test data allows a programmer to verify the logic of an algorithm before it is even coded. It is an especially valuable technique for dry-running programs in the examination, where a question provides pseudo-code and asks for the output produced by a given input.
使用精心选择测试数据的追踪表,使程序员甚至可以在编码之前验证算法的逻辑。这在考试中是一项特别有价值的技术,因为题目会提供伪代码并询问给定输入所产生的输出。
When completing a trace table, the chosen test data should cover all branches of the algorithm. This ensures that every possible execution path is examined, and any logical errors can be identified and corrected.
在填写追踪表时,所选的测试数据应覆盖算法的所有分支。这样可以确保每一条可能的执行路径都得到检查,并且能够识别和纠正任何逻辑错误。
9. Common Exam Mistakes | 常见考试错误
One frequent mistake students make in examinations is confusing boundary values with normal values. A value of 0 in a range of 0 to 100 is not a normal value; it is a boundary value. Students must be precise in classifying each piece of test data according to the three categories.
学生在考试中常犯的一个错误是混淆边界值和正常值。在 0 到 100 的范围内,0 不是正常值;它是边界值。学生必须准确地将每一条测试数据按照三个类别进行分类。
Another common error is providing only one or two examples of test data when the question asks for a full set. A complete answer should include at least one normal value, at least one boundary value, and at least one invalid value, along with a justification for each.
另一个常见错误是当问题要求一组完整的测试数据时,只提供一个或两个例子。完整的答案应包括至少一个正常值、至少一个边界值和至少一个无效值,并为每个值提供理由。
Finally, students often forget to state the expected output for each test data value. Merely listing input values is insufficient; the examiner expects to see the anticipated result that demonstrates an understanding of what the program should do.
最后,学生常常忘记说明每个测试数据值的预期输出。仅仅列出输入值是远远不够的;考官期望看到预期结果,以证明学生对程序应执行的行为的理解。
10. Summary | 总结
Test data is an indispensable part of the software development process. Normal data verifies correct operation under typical conditions, boundary data checks the edges of valid input ranges, and invalid data confirms that the program handles errors gracefully.
测试数据是软件开发过程中不可或缺的一部分。正常数据验证典型条件下的正确操作,边界数据检查有效输入范围的边缘,无效数据则确认程序能够妥善处理错误。
A well-structured test plan, combining all three categories of test data, provides strong evidence that a program is robust and reliable. Mastery of this topic not only earns marks in IGCSE examinations but also forms a foundation for professional programming practice.
一份结构完善的测试计划,结合所有三类测试数据,为程序的健壮性和可靠性提供了有力证据。掌握这一主题不仅能在 IGCSE 考试中获得分数,也为专业编程实践奠定了基础。
Published by TutorHao | Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导