FC ConcursosFC Concursos

Questão FC586511

According to the text, as you begin to write tests you will discover a pattern. What pattern is it?

Questões Interdisciplinares de Inglês

Instituto AOCP · 2018

Set the Table

When you begin writing tests, you will discover a common pattern:

1. Create some objects

2. Stimulate them

3. Check the results

While the stimulation and checking steps are unique test-to-test, the creation step is often familiar. I have a 2 and 3. If I add them, I expect 5. If I subtract them, I expect – 1, if I multiply them, I expect 6. The stimulation and expected results are unique, the 2 and the 3 don’t change.

If this pattern repeats at different scales (and it does), then we’re faced with the question of how often do we want to create new objects. Looking back at our initial set of constraints, two constraints come into conflict:

· Performance—we would like our tests to run as quickly as possible · Isolation—we would the success or failure of one test to be irrelevant to other tests

For performance sake, assuming creating the objects (we’ll call them collectively the “fixture”) is expensive, we would like to create them once and then run lots of tests.

But sharing objects between tests creates the possibility of test coupling. Test coupling can have an obvious nasty effect, where breaking one test causes the next ten to fail even though the code is correct. Test coupling can have a subtle really nasty effect, where the order of tests matters. If I run A before B, they both work, but if I run B before A, then A fails. Worse, the code exercised by B is wrong, but because A ran first, the test passes.

Kent Beck – Test-Driven Development By Example. Addison-Wesley Professional; Edição: 1. Novembro, 2002. Page 82.

According to the text, as you begin to write tests you will discover a pattern. What pattern is it?

  1. Define the tests, stimulate the results, and check the tests.
  2. Check the tests, stimulate the objects, and validate the results.
  3. Create objects, stimulate them, and check the results.
  4. Check the objects, check the performance and ensure insulation.
  5. Analyze performance and ensure insulation.
Mostrar resposta e comentário

Gabarito

Certo

Comentário do professor

A alternativa correta sintetiza o padrão básico de qualquer teste: Criar objetos, estimular esses objetos e verificar os resultados.

Primeiro monta-se a situação inicial, o chamado fixture. Em seguida aplica-se a ação que queremos testar. Por fim, comparamos o estado ou a saída obtida com o resultado esperado.

Esse fluxo garante clareza: separar montagem, estímulo e verificação facilita entender o propósito do teste e localizar falhas.

Há, porém, uma tensão prática entre performance e isolamento. Reusar fixtures melhora velocidade, mas pode gerar acoplamento de testes e ordens dependentes. A solução comum é criar fixtures por teste ou restaurar o estado entre testes quando a criação for cara.

Resumindo: entender e seguir essas três etapas torna os testes mais legíveis, confiáveis e fáceis de manter.

Conteúdo do catálogo FC Concursos · Atualizado em

Continue aprendendo