Risk-based testing focuses on what could break.
Risk-based testing foca no que pode quebrar.Aula
Test strategy in complex PRs
Nível: C1
Objetivo
Definir test strategy em C1 para PRs complexos com risk-based testing, coverage gaps, regression scenarios, fixtures, mocks e flakiness. O foco é testar o que importa, não追求 100% coverage.
Explicação
Test strategy em C1 para PRs complexos não é 'escreva mais testes'; é 'estes são os riscos, e estes são os testes certos para mitigá-los'. Seis eixos:
1. Risk-based testing
Testar pelo risco, não pelo código. C1 cita: 'this PR changes the payment flow; we need tests for the happy path, the failure path, and the idempotency'. Risk-based é o oposto de 'toda função precisa de teste'.
2. Coverage gaps
Onde a cobertura atual é fraca. C1 cita: 'the new error handling has no tests; the new edge case (empty cart) is not covered'. Coverage gap é a justificativa para o teste, não o objetivo.
3. Regression scenarios
O que pode quebrar. C1 cita: 'this change might break the import flow; we need a regression test for the legacy data format'. Regression test cobre mudanças específicas, não cobertura geral.
4. Fixtures e mocks
Dados de teste e dependências. C1 cita: 'this test uses a fixture with realistic data; not a mock with one happy path'. Fixture C1 tem dados parecidos com produção; mock C1 é para limites, não para substituir tudo.
5. Flakiness
Teste que às vezes passa, às vezes falha. C1 cita: 'this test depends on the current time and is flaky in CI; we need to inject a clock'. Flakiness C1 é tratada como bug, não ignorada.
6. Test pyramid
Unit, integration, end-to-end. C1 cita: 'this is an integration test, not a unit test; the test pyramid suggests most tests should be unit, with a few integration and very few e2e'. Test pyramid C1 guia a distribuição.
Exemplos
| Inglês | Tradução | Observação |
|---|---|---|
| This PR changes the payment flow; we need tests for the happy path, the failure path, and the idempotency. | Este PR muda o fluxo de pagamento; precisamos de testes para o happy path, o failure path, e a idempotência. | Risk-based: cita os cenários de risco. |
| The new error handling has no tests; the new edge case (empty cart) is not covered. | O novo tratamento de erro não tem testes; o novo edge case (carrinho vazio) não está coberto. | Coverage gap: cita o gap específico. |
| This test uses a fixture with realistic data; not a mock with one happy path. | Este teste usa uma fixture com dados realistas; não um mock com um happy path. | Fixture: dados realistas, não happy path. |
| This test depends on the current time and is flaky in CI; we need to inject a clock. | Este teste depende da hora atual e é flaky no CI; precisamos injetar um clock. | Flakiness: causa + mitigação. |
Erros comuns
- Perseguir 100% coverage: cobertura alta não significa boa cobertura; o que importa é risco coberto.
- Mock tudo: mock sem dados realistas perde cenários de borda; C1 prefere fixtures com dados reais para fluxos críticos.
- Ignorar flakiness: teste flaky no CI é pior que teste ausente; ele quebra a confiança do time.
- Testar o caminho feliz só: 'isso funciona' não é teste; C1 exige happy path, failure path, e edge cases.
- Confundir integration test com unit test: a test pyramid tem proporção; sem ela, testes lentos dominam o CI.
Prática guiada
Reescreva cada item abaixo no padrão C1 de test strategy.
- Risk-based: cite os riscos e os testes correspondentes.
- Coverage gap: cite o gap específico, não 'precisamos de mais testes'.
- Regression: cite o cenário de regressão e o teste que cobre.
- Fixture: use dados realistas; mock apenas para limites.
- Flakiness: trate como bug; cite a causa e a mitigação.
- Test pyramid: cite a proporção unit/integration/e2e.
Resumo
Test strategy C1 é risk-based: testa o que importa pelo risco, não pela cobertura. Os seis eixos são risk-based testing, coverage gaps, regression scenarios, fixtures vs mocks, flakiness como bug, e test pyramid (unit > integration > e2e). Sem risco nomeado, o teste é palpite; sem flakiness tratada, o CI perde confiança.
Vocabulário
Palavras principais
The new error handling has a coverage gap.
O novo tratamento de erro tem uma lacuna de cobertura.We need a regression test for the legacy data format.
Precisamos de um teste de regressão para o formato de dados legado.Use a fixture with realistic data, not a mock with a happy path.
Use uma fixture com dados realistas, não um mock com happy path.Use mocks for boundaries, not for everything.
Use mocks para limites, não para tudo.Flakiness in CI is treated as a bug.
Flakiness no CI é tratada como bug.The test pyramid suggests most tests should be unit.
A pirâmide de testes sugere que a maioria deve ser unit.The payment endpoint must be idempotent; we need a test for it.
O endpoint de pagamento precisa ser idempotente; precisamos de um teste para isso.História
História: Test strategy in complex PRs
Nível: C1
História em inglês
Defining test strategy for a complex payment PR
On Tuesday, I had to define the test strategy for a complex PR that changed the payment flow. I opened with risk-based: this PR changes the payment flow; we need tests for the happy path, the failure path, and the idempotency. I added a coverage gap: the new error handling has no tests; the new edge case (empty cart) is not covered. I questioned a flaky test: this test depends on the current time and is flaky in CI; we need to inject a clock. I closed with the test pyramid: most tests should be unit, with a few integration tests and very few e2e.
Tradução linha por linha
| Inglês | Tradução |
|---|---|
| On Tuesday, I had to define the test strategy for a complex PR that changed the payment flow. | Na terça, eu tive que definir a test strategy para um PR complexo que mudou o fluxo de pagamento. |
| I opened with risk-based: this PR changes the payment flow; we need tests for the happy path, the failure path, and the idempotency. | Eu abri com risk-based: este PR muda o fluxo de pagamento; precisamos de testes para o happy path, o failure path, e a idempotência. |
| I added a coverage gap: the new error handling has no tests; the new edge case (empty cart) is not covered. | Eu adicionei um coverage gap: o novo tratamento de erro não tem testes; o novo edge case (carrinho vazio) não está coberto. |
| I questioned a flaky test: this test depends on the current time and is flaky in CI; we need to inject a clock. | Eu questionei um teste flaky: este teste depende da hora atual e é flaky no CI; precisamos injetar um clock. |
| I closed with the test pyramid: most tests should be unit, with a few integration tests and very few e2e. | Eu fechei com a test pyramid: a maioria dos testes deve ser unit, com alguns integration e muito poucos e2e. |
Notas de vocabulário
- tests for the happy path, the failure path, and the idempotency = risk-based: cita os cenários de risco.
- the new error handling has no tests; empty cart is not covered = coverage gap: gap específico.
- depends on the current time and is flaky in CI; inject a clock = flakiness C1: causa + mitigação.
- most tests should be unit, a few integration, very few e2e = test pyramid: proporção clara.
Perguntas de compreensão
- O que diferencia test strategy C1 de 'mais testes'?
- Como tratar flakiness em C1?
- O que é a test pyramid em C1?
Prática com a história
- Escreva uma test strategy C1 para uma mudança no fluxo de pagamento.
- Escreva uma crítica C1 para um teste flaky.
- Escreva a test pyramid C1 para um time de 5 engenheiros.
Prática
Exercícios e teste
A próxima melhoria é transformar esses arquivos em perguntas com resposta no navegador. Por enquanto, esta página já organiza o estudo e permite seguir a aula inteira sem abrir os arquivos manualmente.