Testing and Debugging Strategies
AP Computer Science Principles · Unit 1 — Creative Development
Test cases come from the specification
A test case is a specific input together with the expected output, and the expected output must be decided from the specification before running the program. Running the program and then "expecting" whatever it printed tests nothing.
Good test suites are chosen, not collected. Use these sources of inputs:
- Typical values from the middle of each category the specification describes — one value for every branch of the logic.
- Boundary values, also called edge cases: the exact values where the behaviour is supposed to change, plus one value on each side. If the rule is "90 and above is an A", test 89, 90 and 91. Errors such as
>instead of≥are invisible everywhere except at the boundary. - Extreme and unusual inputs: an empty list, a list with one element, all-negative values, repeated values, the first and last positions of a list.
Testing has a hard limit that the exam likes to probe: a program that passes every test you ran is not proven correct. Testing can show the presence of errors, never their absence, because you cannot run every possible input. What testing gives you is evidence, and the quality of that evidence depends entirely on how well the inputs were chosen.
score > 90 cho kết quả đúng ở 85 và 95, chỉ sai đúng một điểm 90. Bộ ca kiểm không có giá trị 90 sẽ báo xanh cho đoạn mã sai.Debugging: making the hidden state visible
Once a test fails, debugging is the process of finding and fixing the cause. The framework lists several effective techniques:
- Hand tracing: executing the code on paper, one statement at a time, recording each variable's value in a trace table with one column per variable and one row per change. Tracing slowly is faster than guessing.
- Adding extra output statements: inserting temporary
DISPLAYstatements to show the value of a variable at a chosen point, for example at the end of each loop iteration. Where you put the statement decides what you learn. - Test cases: re-running the inputs that failed, and the boundary inputs around them, after every change.
- Visualizations and debuggers: tools that show variable values or let the program pause at a chosen line.
A disciplined debugging step has three parts: a hypothesis ("I think count is incremented even when the condition is false"), a probe that would confirm or reject it (display count after each iteration on the input [4, -2]), and a conclusion. Changing code at random until the output looks right usually produces a program that passes one test and fails others.
Worked example: Thiết kế bộ ca kiểm làm lộ lỗi
The specification says: scores of 90 or more earn "A", scores from 80 up to 89 earn "B", all other scores earn "C". A student wrote:
PROCEDURE letter(score)
{
IF(score > 90)
{
RETURN("A")
}
IF(score ≥ 80)
{
RETURN("B")
}
RETURN("C")
}She tested letter(95), letter(85) and letter(70), and all three matched. Explain why her tests did not find the error, and give the smallest set of additional tests that checks both boundaries.
Solution
Ba ca của bạn ấy đều nằm giữa các nhóm: 95 → "A", 85 → "B", 70 → "C" — đúng cả ba, nên báo xanh. Lỗi nằm ở dòng score > 90: với đúng 90, điều kiện đầu sai, điều kiện 90 ≥ 80 đúng, thủ tục trả "B" trong khi đặc tả đòi "A". Chỉ một giá trị nguyên duy nhất làm lộ lỗi, và cả ba ca đều không chạm tới nó.
Bộ ca thêm nhỏ nhất để soát cả hai biên: letter(90) mong "A" (sẽ thất bại, lộ lỗi); letter(89) mong "B"; letter(80) mong "B"; letter(79) mong "C". Biên 80 hiện đang đúng vì dùng ≥, nhưng vẫn phải kiểm — bạn không biết trước biên nào sai.
Sửa: đổi score > 90 thành score ≥ 90, rồi chạy lại cả 7 ca.