การแยกส่วน การทดสอบ และการแก้ไขข้อผิดพลาด
แนะนำใหม่ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| decomposition/ˌdiːkɒmpəˈzɪʃn/ | การแยกย่อย |
| testing/ˈtestɪŋ/ | การทดสอบ |
| debugging/ˈdiːbʌɡɪŋ/ | การดีบัก |
Where is the bug in 300 lines?
- A shop program prints the wrong total. All 300 lines run in one long block, so the mistake could be anywhere, and every change risks a new one.
- Split the same program into small functions, each checked on its own, and the question shrinks to "which piece?". This lesson is about building programs you can check and fix.
One job per function
- Decomposition 分解 means splitting a problem until each piece does one clear job. A good piece is small enough to test alone.
- Give each function a short docstring that states its contract: what goes in, what comes out, and the units.
def add_fee(amount):
"""Return amount (pence) plus a fixed 3 pence fee."""
return amount + 3
def order_total(unit_pence, quantity):
"""Return the order total in pence, fee included."""
return add_fee(unit_pence * quantity)
การแบ่งส่วนใดของโปรแกรม "พิมพ์ใบเสร็จ" คือการแยกย่อยที่ดีที่สุด?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
แต่ละส่วนมีหน้าที่ชัดเจนและมีอินพุตและเอาต์พุตที่แน่นอน ดังนั้นจึงสามารถทดสอบแยกต่างหาก การแบ่งตามตำแหน่งในโค้ดจะทำให้เกิดส่วนที่มีสัญญาไม่ชัดเจน
Return in the middle, print at the edges
- A function that returns its result can be used by other functions and checked by a test. A function that only prints cannot: its caller gets
None. - Keep
input()andprint()at the edges of the program. The middle should be functions that take values and return values.
ติดตามลำดับ order_total(250, 4)
order_total จะหยุดชะงักจนกว่า add_fee(1000) จะทำงานเสร็จ แล้วจึงใช้ค่าที่กลับคืนมา เนื่องจากฟังก์ชันทั้งสองส่งค่ากลับออก มา จึงสามารถทดสอบแต่ละส่วนได้อย่างอิสระ
def show(x): print(x + 1). ค่าที่ result = show(4) เก็บไว้ใน result คืออะไร?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
show พิมพ์ 5 แต่ไม่มี return ดังนั้นการเรียกให้ผลลัพธ์เป็น None ไม่มีสิ่งใดสามารถใช้หรือทดสอบ 5 ได้
A test compares actual with expected
- Testing 测试 means running code on an input where you already know the right answer.
assert order_total(250, 4) == 1003is silent when it passes and raisesAssertionErrorwhen it fails. - Work out the expected value by hand before you run the code; otherwise you will just copy whatever the code printed.
ลองคำนวณด้วยมือก่อนรันโปรแกรม: order_total(120, 3) จะส่งผลลัพธ์ออกมาเท่าไร ในหน่วยเพนนิ?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
$120 \times 3 = 360$ เพนซ์ บวกกับค่าธรรมเนียม 3 เพนซ์ จะได้ $363$ จำนวนนี้ถูกนำไปวางทางขวาของ assert order_total(120, 3) == 363
Test the boundaries
- Bugs hide at the edges: zero, one, empty, the largest value and the exact boundary of a rule. One typical case is not enough.
- Testing
average([])shows that the function divides by zero and raisesZeroDivisionError. A test ofaverage([4, 6])alone would never find that.
- Choose tests for
order_total(unit_pence, quantity). - Typical:
order_total(250, 4) == 1003. - Zero quantity:
order_total(250, 0) == 3. The fee still applies; is that what the shop wants? A boundary test makes you decide. - Zero price:
order_total(0, 4) == 3.
คุณกำลังทดสอบ average(xs) ซึ่งคืนค่า sum(xs) / len(xs).input ใดบ้างที่เป็นกรณีขอบเขต worthy of adding? เลือกทั้งหมดที่เกี่ยวข้อง
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
การหาหารด้วยศูนย์จากลิสต์ว่าง และการทดสอบกรณีขอบเขตด้วยรายการเดียวและทั้งหมดเป็นศูนย์ ส่วนการซ้ำกรณีปกติภายใต้ชื่อตัวแปรใหม่ไม่ได้ทดสอบสิ่งใหม่ๆ
Write the test first
- Write a test that fails, run it and see it fail, write the simplest code that passes, then tidy the code while the tests stay green.
- The test is the contract written as code. If a later change breaks the contract, a test fails at once.

เรียงลำดับลูปแบบทดสอบก่อน
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
การเห็นการทดสอบล้มเหลวเป็นครั้งแรกพิสูจน์ว่ามันตรวจสอบสิ่งใดสิ่งหนึ่งจริง; การทดสอบที่ไม่เคยล้มเหลวอาจไม่มีการตรวจสอบอะไรเลย
Debug by halving the search
- Debugging 调试 means finding where what you believe and what the code does split apart. First reproduce the bug with the smallest input that fails.
- Then print a value halfway through the suspect code. If it is right, the bug is in the second half; if not, the first half. Each run halves the search, so 8 stages need only 3 runs.
มีบัก somewhere ใน pipeline ของ 16 ขั้นตอน Each run with one well-placed print ลดพื้นที่ค้นหาครึ่งหนึ่ง ต้องใช้จำนวน runs มากที่สุดเท่าไรเพื่อหาขั้นตอนนั้นได้
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
$16 \to 8 \to 4 \to 2 \to 1$: สี่ครั้งครึ่ง เพราะ $2^4 = 16$
All tests pass, output wrong? Check the wiring
- When every function passes its tests but the program is still wrong, look between the functions: arguments in the wrong order, or a returned value that is thrown away.
discount(80, 25)is60.0, but a call writtendiscount(25, 80)gives5.0. Callingadd_fee(sub)without storing the result leavessubunchanged.
หากฟังก์ชันทุกตัวผ่านการทดสอบของตัวเอง โปรแกรมทั้งหมดต้องให้ผลลัพธ์ที่ถูกต้อง
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
การเรียกใช้ระหว่างฟังก์ชันอาจยังผิด: แทนที่จะเป็น discount(25, 80) เป็น discount(80, 25), หรือค่าที่ถูกส่งคืนที่ไม่เคยถูกเก็บไว้
Marks that slip away
- Printing instead of returning. The caller gets
None, and nothing can be tested. - Only typical tests. Always add zero, empty and boundary cases.
- Expected values copied from the output. Work them out by hand first.
- Changing many things at once. Fix one thing, re-run all the tests, then move on.
You've got it
- Decomposition: one job per function, a docstring contract, return values in the middle and print only at the edges.
- Testing:
assert actual == expected, with the expected value worked out first. Bugs hide at the edges:
| Edge | Example test |
|---|---|
| Zero | order_total(250, 0) |
| One | average([5]) |
| Empty | average([]) |
| Largest | the biggest allowed quantity |
| Boundary | exactly on a rule's limit |
- Debugging: reproduce with the smallest failing input, halve the search with one print per run, and check the wiring between tested functions.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้