Testing and continuous integration
הוצג
6:03
הנדסת תוכנה
Software engineering is how systems outlive their authors. The first three units shape the product: requirements, architecture and interface design. The next…
קריינות באנגלית · כתוביות באנגלית ובסינית 中文 שאפשר להחליף ביניהן
בשיעור הזה
| English | משמעות |
|---|---|
| test pyramid/test ˈpɪrəmɪd/ | פירמידת בדיקות |
| unit tests/ˈjuːnɪt tests/ | Tests of individual units of behaviour, usually isolated from unrelated parts of the system. |
| integration tests/ˌɪntɪˈɡreɪʃn tests/ | tests that check several parts working together |
| end-to-end tests/end tʊ end tests/ | tests of the whole system from the user's point of view |
| Continuous integration/kənˈtɪnjuːəs ˌɪntɪˈɡreɪʃn/ | building and testing every change automatically as soon as it is pushed |
| coverage/ˈkʌvərɪdʒ/ | the share of the code that the tests run |
| Mutation testing/mjuːˈteɪʃn ˈtestɪŋ/ | making small deliberate bugs to check that the tests catch them |
| stub/stʌb/ | a stand-in that returns fixed answers to the code under test |
| mock/mɒk/ | a stand-in that also checks how the code under test calls it |
| fake/feɪk/ | |
| Equivalence partitioning/ɪˈkwɪvələns pɑːˈtɪʃənɪŋ/ | dividing inputs into groups that should behave the same, and testing one value from each |
| Boundary value analysis/ˈbaʊndəri ˈvæljuː əˈnæləsɪs/ | testing values at and just beyond the edges of each input range |
| flaky tests/ˈfleɪki tests/ | tests that sometimes pass and sometimes fail with no code change |
The pyramid
- The test pyramid 测试金字塔: many unit tests 单元测试 (fast, isolated), fewer integration tests 集成测试 (with a real database) and a few end-to-end tests 端到端测试 (the whole app).
- Continuous integration 持续集成 (CI) runs them on every push, and a red result blocks the merge.

The pyramid puts — at the base
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Many fast units, fewer integration, few slow end-to-end.
Coverage, honestly
- Line coverage 覆盖率 counts the lines that ran; branch coverage counts the decisions that went both ways. The course suite has 92% line and 81% branch coverage. The branch number is the honest one: each untested branch is an untested behaviour.
- The gate at 85% line coverage blocks merges, and the list of uncovered branches is the to-do list. A test without an assertion only makes coverage look good.
Branch coverage is the honest number because —
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
92% line with 81% branch: the gap is decisions never exercised.
FR-2 at all three levels
- The same rule, FR-2 (due date = borrow date + 21), tested at each level:
| Level | What the test checks |
|---|---|
| Unit (milliseconds) | three dates: a normal one, February in a leap year, the end of the year |
| Integration (seconds) | the loan is saved and reads back correctly |
| End-to-end (slowest) | a scripted member borrows a book and sees the due date |
- Red, then green, in CI: push the failing sixth-borrow test, and CI goes red; write the limit check, and it goes green, coverage rises and the merge is unblocked. Test behaviour (the due date), not internal details.
Due = borrow date + 21 is tested at the unit level with —
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Pure function: normal, leap February, year boundary.
Put the red-green cycle for the sixth-borrow rule in order.
רמת קושי 3/5 · 200 מטבעות + בונוס קטן בהשלמה הראשונה היום
Seeing the test fail first proves it can fail; a test that was never red may be testing nothing.
Test doubles and mutation testing
- A stub 桩 returns fixed data; a mock 模拟对象 also checks how it was called ("exactly one reminder sent"); a fake 伪实现 is a working shortcut, such as a database in memory. Use doubles at the boundaries, never for the logic being tested.
- Mutation testing 变异测试 checks the tests themselves: change
>=to>in the loan-limit check and run the tests again. - If every test still passes, the mutant survives, and a boundary test (attempting to borrow a sixth book while holding five) is missing. The mutation score is killed over total: $45$ of $50$ is $90\%$.
A test suite kills 38 of 40 mutants. What is its mutation score, in per cent?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
38 / 40 = 0.95 = 95%.
A test must check that exactly one reminder was sent. Which double fits?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
A mock records its calls, so the test can assert there was exactly one.
Partitions and boundaries
- Equivalence partitioning 等价类划分 splits inputs into classes the code should treat the same way. For "up to 5 loans": holding 0–4 (borrowing allowed) and holding 5 or more (refused). One test per class is the minimum.
- Boundary value analysis 边界值分析 adds tests at the edges, where off-by-one bugs live: holding 4 (allowed) and holding 5 (refused). That is exactly the test that kills the
>=to>mutant.
For "a member may hold up to 5 loans", which pair of holdings is the boundary test?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
One allowed, one refused, on either side of the edge.
Worked example: what flaky tests cost
- A test that fails at random 1% of the time looks harmless. A suite of 200 such tests, with independent failures, is all green with probability $0.99^{200} = \mathbf{13.4\%}$.
- Then most red builds are noise, people stop trusting red, and real failures slip through. Set flaky tests 不稳定测试 aside and fix them at once; never just rerun until green.
100 tests, each failing independently at random on 1% of runs: chance the suite is all green (one decimal, %)?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
$0.99^{100} = 0.366$.
Rerunning a flaky test until it passes is an acceptable fix.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
It hides real failures; quarantine and fix the cause.
Marks that slip away
- A test with no assertion. It raises coverage and checks nothing.
- Line coverage as the truth. $92\%$ line coverage can hide only $81\%$ branch coverage; read the branch number.
- Never watching the test fail. Push it red first, so you know it can fail.
Recap
- The pyramid: many unit tests, fewer integration tests, a few end-to-end tests.
- Branch coverage is the honest number: $81\%$ against $92\%$ line coverage.
- Test behaviour, not internal details; a red CI run blocks the merge.
מה השתבש? רק צוות האתר רואה זאת.