Testing and continuous integration
Introducido
6:03
Ingeniería de software
Software engineering is how systems outlive their authors. The first three units shape the product: requirements, architecture and interface design. The next…
Narración en inglés · subtítulos en inglés y 中文 que puedes cambiar
En esta lección
| English | Significado |
|---|---|
| test pyramid/test ˈpɪrəmɪd/ | pirámide de pruebas |
| 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 |
La pirámide
- La pirámide de pruebas: muchas pruebas unitarias (rápidas, aisladas), menos pruebas de integración (base de datos real), pocas pruebas de extremo a extremo (toda la aplicación). CI las ejecuta en cada empuje: rojo bloquea la fusión.


The pyramid puts — at the base
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Many fast units, fewer integration, few slow end-to-end.
Cobertura, con honestidad
- La cobertura de líneas cuenta las líneas ejecutadas; la cobertura de ramas cuenta las ramas decididas. El conjunto de cursos: 92% de líneas, 81% de ramas — el número de ramas es el honesto (cada rama sin probar es un comportamiento sin probar).
- El umbral en 85% de líneas bloquea las fusiones; la lista de ramas sin cubrir es la cola de trabajo. Una prueba sin aserción es teatro de cobertura.
Branch coverage is the honest number because —
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
92% line with 81% branch: the gap is decisions never exercised.
FR-2 en los tres niveles
- Unitaria: fecha de vencimiento = fecha de préstamo + 21 — tres casos (normal, febrero bisiesto, límite de año). Integración: el préstamo se registra y se lee de nuevo. Extremo a extremo: un miembro scriptado presta y ve la fecha de vencimiento.
- Rojo-verde en CI: empuja la sexta prueba de préstamo fallida, CI rojo; implementa la comprobación del límite, verde, sube la cobertura, desbloquea la fusión. Asegura el comportamiento (la fecha de vencimiento), no los internals.
Due = borrow date + 21 is tested at the unit level with —
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Pure function: normal, leap February, year boundary.
Put the red-green cycle for the sixth-borrow rule in order.
Dificultad 3/5 · 200 monedas + un pequeño bono en el primer intento de hoy
Seeing the test fail first proves it can fail; a test that was never red may be testing nothing.
Esta tarjeta se corrigió, por eso aparece en inglés hasta que se actualice su traducción.
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?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
38 / 40 = 0.95 = 95%.
A test must check that exactly one reminder was sent. Which double fits?
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
A mock records its calls, so the test can assert there was exactly one.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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?
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
One allowed, one refused, on either side of the edge.
Esta tarjeta se corrigió, por eso aparece en inglés hasta que se actualice su traducción.
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, %)?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
$0.99^{100} = 0.366$.
Rerunning a flaky test until it passes is an acceptable fix.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
It hides real failures; quarantine and fix the cause.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
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.
¿Qué salió mal? Solo el equipo del sitio ve esta información.