Back to tree

🩺 测试:上线前的体检🩺 Testing: a Checkup Before Launch

上一片里,💻小电脑捉完了 4 只虫子。可怎么知道没有第 5 只?总不能等用户来踩坑。这一片给程序做体检:测试。 Last leaf, 💻 Little Computer caught 4 bugs. But how can it know there isn’t a fifth? It can’t wait for users to stumble on it. This leaf gives the program a checkup: testing.

第 1 步:给程序做体检Step 1: A checkup for programs

先看体检怎么做——喂进输入,核对答案。First, how a checkup works — feed in inputs, check the answers.

体检报告:通过
测试就像体检:给程序喂一组输入,看输出对不对。比如 avg([90, 80]) 应该得 85。这样的一条检查,叫一个测试用例。每一条都对上,报告就盖上“通过”章。 Testing is like a checkup: feed the program some input and check the output. For example, avg([90, 80]) should give 85. Each check like that is called a test case. When every one matches, the report gets a PASS stamp.

那该喂哪些输入?随手挑几个正常分数,够吗?So which inputs? Are a few ordinary scores enough?

第 2 步:专挑边界下手Step 2: Aim at the edges

边界陷阱:0、空、超大
不够。虫子最爱躲在边界:0、空、超大。空名单求平均要除以 0,程序当场晕倒。10 万人的名单,可能慢到卡死。及格线 60 分,就测 59、60、61 三个数。好测试员专挑这些刁钻的地方。 Not enough. Bugs love hiding at the edges: 0, empty, huge. Averaging an empty list divides by 0 — the app faints. A list of 100,000 names might crawl to a halt. With a pass line of 60, test 59, 60 and 61. Good testers aim right at these tricky spots.

可每改一行就手动测一遍,不得累坏?But re-testing by hand after every edit — wouldn’t that wear you out?

第 3 步:让电脑自己测Step 3: Let the computer do the testing

自动测试:每次改完都跑一遍
所以把测试也写成程序,让电脑自己跑。每次提交代码,流水线就把全部测试跑一遍。全过才放行,有一条不过就拦下。加新功能时碰坏了旧功能,也能马上发现,这叫回归测试。记住:测试能证明有虫,却证明不了没虫。 So we write the tests as code and let the computer run them. Every time code is committed, a pipeline runs every test. All pass, it ships; one fails, it’s stopped. Broke an old feature while adding a new one? You’ll know at once — that’s regression testing. Remember: testing can show bugs are there, never that none are left.

🎮 你来当测试员(30 秒)🎮 Your turn: be the tester (30 seconds)

道理讲完了。成绩单小程序有 4 个功能要体检:每轮挑出最能抓虫的那组输入。Theory done. The score app has 4 features to check: each round, pick the input most likely to catch a bug.

一句话记住它:测试 = 给程序体检:专挑边界下手,每次改完让电脑自动跑一遍。 Remember it in one line: testing is a checkup for programs — aim at the edges, and let the computer re-run it after every change.
测试用例:喂输入、对答案,全对才盖“通过”章Test case: feed input, check the answer — all correct earns a PASS 边界陷阱:0、空、超大,还有差 1 分的及格线Edge traps: 0, empty, huge — and the pass line, off by one 自动测试每次改完都跑;全通过 ≠ 没虫,只是还没发现Auto tests run on every change; all green ≠ bug-free, just none found yet
内容参考 OpenStax 等公开教材,多来源核对 · AI 生成、人工审核 · 发现错误欢迎指正,帮这片叶子长得更好。 Based on OpenStax and other open textbooks, cross-checked across sources · AI-generated, human-reviewed · Spotted a mistake? Tell us — help this leaf grow.