Testes

Testes unitários: a base da pirâmide

Na pirâmide de testes, os testes unitários formam a base larga: muitos, rápidos e baratos. São eles que dão ao time a coragem de refatorar sem medo. Mas escrever bons testes unitários é uma habilidade — e tem armadilhas.

O que é (e o que não é) um teste unitário

Um teste unitário verifica uma unidade de comportamento de forma isolada, sem tocar banco, rede ou sistema de arquivos. Se ele precisa de um servidor no ar, provavelmente é um teste de integração disfarçado.

O padrão AAA

Um bom teste é legível como uma historinha: Arrange, Act, Assert.

@Test
void deveAplicarDescontoParaClienteVip() {
    // Arrange
    Carrinho carrinho = new Carrinho(new Cliente(Tipo.VIP));
    carrinho.adicionar(new Item("Livro", 100.0));

    // Act
    double total = carrinho.total();

    // Assert
    assertEquals(90.0, total); // 10% de desconto VIP
}

O que faz um bom teste

  • Rápido: milhares deles rodam em segundos.
  • Determinístico: mesmo input, mesmo resultado, sempre.
  • Isolado: não depende da ordem de execução nem de outros testes.
  • Focado: testa um comportamento; o nome descreve a regra de negócio.

O que NÃO testar

Não teste detalhes de implementação nem getters/setters triviais. Teste comportamento, não estrutura interna — assim o teste sobrevive a refatorações.

Cobertura de 100% não significa qualidade. Significa que todas as linhas rodaram — não que todas estão corretas.

Conclusão

Testes unitários são o investimento de maior retorno em qualidade: feedback instantâneo, documentação viva do comportamento e a segurança para evoluir o código. Comece por eles antes de subir na pirâmide.

In the test pyramid, unit tests form the wide base: many, fast and cheap. They are what gives the team the courage to refactor without fear. But writing good unit tests is a skill — and it has pitfalls.

What a unit test is (and isn't)

A unit test verifies a unit of behavior in isolation, without touching the database, network or file system. If it needs a server up and running, it's probably an integration test in disguise.

The AAA pattern

A good test reads like a little story: Arrange, Act, Assert.

@Test
void deveAplicarDescontoParaClienteVip() {
    // Arrange
    Carrinho carrinho = new Carrinho(new Cliente(Tipo.VIP));
    carrinho.adicionar(new Item("Livro", 100.0));

    // Act
    double total = carrinho.total();

    // Assert
    assertEquals(90.0, total); // 10% VIP discount
}

What makes a good test

  • Fast: thousands of them run in seconds.
  • Deterministic: same input, same result, always.
  • Isolated: doesn't depend on execution order or on other tests.
  • Focused: tests one behavior; the name describes the business rule.

What NOT to test

Don't test implementation details or trivial getters/setters. Test behavior, not internal structure — that way the test survives refactoring.

100% coverage doesn't mean quality. It means every line ran — not that every line is correct.

Conclusion

Unit tests are the highest-return investment in quality: instant feedback, living documentation of behavior and the confidence to evolve the code. Start with them before moving up the pyramid.

Leia também

Voltar ao blog