การทดสอบและคุณภาพของการออกแบบ
แนะนำใหม่ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| refactoring/rɪˈfæktərɪŋ/ | การปรับโครงสร้างโค้ด |
| unit test/ˈjuːnɪt test/ | การทดสอบหน่วย |
| arrange/əˈreɪndʒ/ | เตรียมพร้อม |
| act/ækt/ | ดำเนินการ |
| assert/əˈsɜːt/ | การยืนยัน |
| single responsibility principle/ˈsɪŋɡl rɪˌspɒnsəˈbɪlɪti ˈprɪnsɪpl/ | หลักการความรับผิดชอบเดียว |
| coupling/ˈkʌplɪŋ/ | การเชื่อมโยง |
| cohesion/kəʊˈhiːʒn/ | ความเข้มแข็ง |
Did the change break anything?
- You move the fine calculation out of
Loaninto its own class. The program still starts, but is every result still the same? - Clicking around proves very little. A good test suite answers in one second, and this lesson shows how to write one.
Tests pin behaviour
- Refactoring 重构 changes structure without changing behaviour. It is only safe when the behaviour is pinned down.
- Unit tests 单元测试 are those pins: each one checks one promise the code makes.
Arrange, act, assert
- One test, one behaviour: arrange 准备 the objects, act 执行 with one call, assert 断言 the outcome the caller can see.
def test_renew_ok():
loan = Loan(20) # arrange
out = renew(loan, 7) # act
assert out == 27 # assert
เรียงลำดับส่วนประกอบของ unit test
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
จัดวาง, ปฏิบัติ, ยืนยัน — การกระทำหนึ่งต่อการทดสอบ
Behaviours, not methods
- Name tests after behaviours: "renew past the limit raises" is a behaviour;
test_daysis just a method name. - A behaviour test survives renames and fails exactly when a promise changes. Assert what callers see, not private fields.
การทดสอบหน่วยที่ดีควรมีชื่อตาม —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ชื่อพฤติกรรมยังคงอยู่หลังการเปลี่ยนชื่อและล้มเหลวอย่างแม่นยำเมื่อผลลัพธ์ที่สัญญาไว้เปลี่ยนแปลง
การทดสอบที่ยืนยันว่าฟิลด์ส่วนตัว loan._days เป็นพฤติกรรมทดสอบที่ดี
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
มันยืนยันการจัดเก็บ ไม่ใช่คำมั่นสัญญา จึงจะพังเมื่อมีการปรับปรุงโค้ด ยืนยันสิ่งที่ผู้เรียกเห็น เช่น จำนวนวันที่ถูกส่งกลับ
Test the boundary
renewallows up to 30 days, because the check is> 30. Test three points: inside (20 + 7 = 27), exactly on the limit (20 + 10 = 30, allowed) and past it (20 + 11 raises, loan unchanged).- The boundary test catches the classic slip of writing
>=for>.
import pytest
def test_renew_on_limit():
loan = Loan(20)
assert renew(loan, 10) == 30
def test_renew_past_limit_raises():
loan = Loan(20)
with pytest.raises(ValueError):
renew(loan, 11)
assert loan.days == 20
pytest.raisespasses only if the call raises that error. The last line checks the second promise: a failed renewal changes nothing.
กฎคือ "ยกเว้นหาก days + extra > 30" renew(Loan(20), 10) จะส่งค่าอะไร?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
20 + 10 = 30 ไม่เกิน 30 ดังนั้นการต่ออายุจึงอนุญาตให้ทำได้
The coverage grid
- List behaviours down the side and tests across the top. Each filled cell is a behaviour a test pins.
- An empty row is the next test to write. Coverage follows the list of promises, not the lines of code.

แถวว่างในตารางความครอบคลุมหมายถึง —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
แถวคือพฤติกรรม คอลัมน์คือการทดสอบ: แถวว่างคือข้อสอบถัดไปที่ต้องเขียน
จับคู่การทดสอบแต่ละตัวกับพฤติกรรมที่ยืนยัน
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
หนึ่งพฤติกรรมต่อการทดสอบ;แถวว่างในตารางคือบรรทัดถัดไปที่จะเขียน
Refactor under green
- Move fines into a
FineCalculatorclass (the single responsibility principle 单一职责原则: one reason to change), and the renew tests still pass untouched. - Coupling 耦合 (how much classes depend on each other) went down; cohesion 内聚 (one purpose per class) went up. That is the direction of every good change.
การย้ายการคำนวณที่ละเอียดเข้าไปใน FineCalculator ปลอดภัยเมื่อ —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
การทดสอบสีเขียวตลอดการย้ายคือหลักฐานว่าโครงสร้างเปลี่ยนแปลงแต่พฤติกรรมไม่เปลี่ยน
Worked example: one bug, one red test
- The three renew tests run and print
3 passed. - Someone changes the check to
>= 30. Nowtest_renew_boundaryfails and the other two pass: one red test names the exact broken promise.
การตรวจสอบถูกแก้ไขผิดเป็น days + extra >= 30 การทดสอบการต่ออายุทั้งสามข้อล้มเหลวกี่ข้อ?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
เฉพาะการทดสอบขอบเขต (20 + 10 = 30) ที่เปลี่ยนผลลัพธ์ 27 ยังคงผ่านและ 31 ยังคงเกิดข้อผิดพลาด
Three common slips
- Asserting private internals. Every refactor then breaks the test.
- Sharing changeable state between tests. Test B passes only if test A ran first.
- Chasing line coverage on getters while the limit branch has no test.
Recap
- Arrange, act, assert: one behaviour per test, named after the behaviour.
- Test inside, on and past every boundary; the grid shows the next test to write. For
renew, limit 30 days:
| Test | Loan days | Expected |
|---|---|---|
| Inside | 27 | allowed |
| On the limit | 30 | allowed |
| Past it | 31 | raises; loan unchanged |
- Green tests make refactoring safe; aim for low coupling and high cohesion.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้