boas praticas de testes, bad smell e outros macetes

52
BOAS PRÁTICAS DE TESTES, BAD SMELL E OUTROS MACETES Por Ismael Soares

Upload: ismael

Post on 14-Apr-2017

151 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Boas Praticas de Testes, Bad Smell e Outros Macetes

BOAS  PRÁTICAS  DE  TESTES,  BAD  SMELL  E  OUTROS  MACETES

Por Ismael Soares

Page 2: Boas Praticas de Testes, Bad Smell e Outros Macetes

POR QUE VOCÊ TESTA SEU CÓDIGO?

Page 3: Boas Praticas de Testes, Bad Smell e Outros Macetes

ME AJUDA NO DESIGN

GARANTIR REGRAS DE NEGÓCIO

ENTENDER O NEGÓCIO

AJUDA RESOLVER PROBLEMAS COMPLEXOS

ETC.

Page 4: Boas Praticas de Testes, Bad Smell e Outros Macetes

OK,  MAS  COMO  O  TESTE  TE  AJUDA  COM  ESTAS  COISAS?

Page 5: Boas Praticas de Testes, Bad Smell e Outros Macetes

FEEDBACK!

Page 6: Boas Praticas de Testes, Bad Smell e Outros Macetes

VOCÊ  OS  ESCUTA?

Page 7: Boas Praticas de Testes, Bad Smell e Outros Macetes

Mike  Cohn  (Succeeding  with  Agile)

PIRÂMIDE  DE  TESTES

Page 8: Boas Praticas de Testes, Bad Smell e Outros Macetes

UNIDADE

Page 9: Boas Praticas de Testes, Bad Smell e Outros Macetes

MAS  O  QUE  É  UMA  UNIDADE?

Page 10: Boas Praticas de Testes, Bad Smell e Outros Macetes

UMA UNIDADE DE NEGÓCIO É UM COMPORTAMENTO DO SEU CÓDIGO!

______________________________ Martin Fowler

Page 11: Boas Praticas de Testes, Bad Smell e Outros Macetes

NÃO É UMA CLASSE OU UM MÉTODO

Thought  Works

Page 12: Boas Praticas de Testes, Bad Smell e Outros Macetes

VOCÊ DEVE SE PREOCUPAR MAIS COM O QUE

DO QUE COM COMO

Page 13: Boas Praticas de Testes, Bad Smell e Outros Macetes

ESCREVENDO  TESTES  MELHORES

Page 14: Boas Praticas de Testes, Bad Smell e Outros Macetes

Referência:

https://info.thoughtworks.com/praticas-e-tendencias-em-teste-ebook.html

Page 15: Boas Praticas de Testes, Bad Smell e Outros Macetes

#  TRATE  O  CÓDIGO  DE  TESTES  COMO  DE  PRODUÇÃO

https://8thlight.com/blog/uncle-bob/2013/09/23/Test-first.html

TEST FIRST

Page 16: Boas Praticas de Testes, Bad Smell e Outros Macetes

TESTS ARE SPECS

Page 17: Boas Praticas de Testes, Bad Smell e Outros Macetes

ROTTEN TESTS

Page 18: Boas Praticas de Testes, Bad Smell e Outros Macetes

SIMPLE DESIGN

Page 19: Boas Praticas de Testes, Bad Smell e Outros Macetes

#  UTILIZE  UM  PADRÃO  DE  TESTES

Page 20: Boas Praticas de Testes, Bad Smell e Outros Macetes

OS 3 AS - ARRANGE, ACT E ASSERT

Organizar:  Configure  suas  informações  e  qualquer  outro  dado  necessário  ao  teste

Agir:  Execute  a  ação  que  vai  validar  

Verificar:  Veja  o  que  era  esperado  realmente  aconteceu

Page 21: Boas Praticas de Testes, Bad Smell e Outros Macetes

PADRÃO BDD

Page 22: Boas Praticas de Testes, Bad Smell e Outros Macetes

#  NÃO  CONVIVA  COM  TESTES  INSTÁVEIS

Page 23: Boas Praticas de Testes, Bad Smell e Outros Macetes

ERADICATING NON-DETERMINISM IN TESTS

http://martinfowler.com/articles/nonDeterminism.html

Não  passa  segurança

Precisa  de  interpretação

Vira  paisagem  com  tempo

Page 24: Boas Praticas de Testes, Bad Smell e Outros Macetes

#  UTILIZE  DUBLES  DE  TESTES

http://rkmael.com/2014/11/13/dubles_testes/

Page 25: Boas Praticas de Testes, Bad Smell e Outros Macetes

http://rkmael.com/2014/11/17/comparando-dubles-testes/

COMPARANDO DUBLES

Page 26: Boas Praticas de Testes, Bad Smell e Outros Macetes

#  CUIDADO  COM  EXCESSO  DE  MOCKS

Page 27: Boas Praticas de Testes, Bad Smell e Outros Macetes

SEM PERCEBER VOCÊ PODE ESTAR MOCKANDO UM COMPORTAMENTO

Page 28: Boas Praticas de Testes, Bad Smell e Outros Macetes

QUANDO USAR MOCKS?

Quando  o  comportamento  não  é  responsabilidade  da  unidade  testada.

Exemplo:  um  Service  chamando  outro,  um  Service  chamando  um  DAO,  um  Controller  chamando  um  Service,  etc.

Page 29: Boas Praticas de Testes, Bad Smell e Outros Macetes

E UMA REGRA CHAMADA DENTRO DE UM MODEL/ENTITY, DEVO MOCKAR?

Page 30: Boas Praticas de Testes, Bad Smell e Outros Macetes

MÁS  PRÁTICAS

Page 31: Boas Praticas de Testes, Bad Smell e Outros Macetes

#  TUDO  NO  MÉTODO  BEFORE

Page 32: Boas Praticas de Testes, Bad Smell e Outros Macetes

@Before  -­‐  uma  vez  antes  de  cada  teste

@BeforeClass  -­‐  uma  vez  apenas  antes  de  todos  os  testes

@After  -­‐  uma  vez  depois  de  cada  teste

@AfterClass  -­‐  uma  vez  apenas  depois  de  todos  os  testes

Page 33: Boas Praticas de Testes, Bad Smell e Outros Macetes

#NÃO  EVIDENCIAR  O  CENÁRIO

Page 34: Boas Praticas de Testes, Bad Smell e Outros Macetes

ERRADO

CORRETO

Page 35: Boas Praticas de Testes, Bad Smell e Outros Macetes
Page 36: Boas Praticas de Testes, Bad Smell e Outros Macetes
Page 37: Boas Praticas de Testes, Bad Smell e Outros Macetes

#CENARIOS  GIGANTES

Page 38: Boas Praticas de Testes, Bad Smell e Outros Macetes
Page 39: Boas Praticas de Testes, Bad Smell e Outros Macetes
Page 40: Boas Praticas de Testes, Bad Smell e Outros Macetes

SE O CENÁRIO ESTA MUITO GRANDE É PORQUE SEU

CÓDIGO ESTÁ MUITO ACOPLADO

Page 41: Boas Praticas de Testes, Bad Smell e Outros Macetes

#CENARIOS  IMPRONUNCIAVEIS

Page 42: Boas Praticas de Testes, Bad Smell e Outros Macetes

deveRetornarMensagemDeValidacaoDaDataDeEmissaoEBloquearANotaFiscalQuandoOUsuarioNaoTiverPermissoesEADataDeEmissaoUltrapassarADataParametrizadaEOStatusDeRecebimen

toForDiferenteDeReceberComDataAcimaDoParametrizado

Page 43: Boas Praticas de Testes, Bad Smell e Outros Macetes

SE VOCÊ NÃO CONSEGUE EXPRESSAR DE FORMA

SIMPLES UM CENARIO, É SINAL DE QUE ESTA MUITO

COMPLEXO

Page 44: Boas Praticas de Testes, Bad Smell e Outros Macetes

#TESTE  DE  EXCEPTION  SE  VALIDAR  A  MENSAGEM

Page 45: Boas Praticas de Testes, Bad Smell e Outros Macetes

ERRADO

Page 46: Boas Praticas de Testes, Bad Smell e Outros Macetes

CORRETO

Page 47: Boas Praticas de Testes, Bad Smell e Outros Macetes

#NÃO  VALIDAR  A  ORDEM  DAS  INTERAÇÕES

Page 48: Boas Praticas de Testes, Bad Smell e Outros Macetes

ERRADO

Page 49: Boas Praticas de Testes, Bad Smell e Outros Macetes

CORRETO

Page 50: Boas Praticas de Testes, Bad Smell e Outros Macetes

#ALTERAR  O  CÓDIGO  DE  PRODUÇÃO  APENAS  POR  CAUSA  DOS  TESTES

Page 51: Boas Praticas de Testes, Bad Smell e Outros Macetes

DEIXAR O MÉTODO DEFAULT PRA TESTA-LO

CRIAR SETTER PARA FAZER INJEÇÃO DE DEPENDÊNCIA

CRIAR MÉTODO PARA ALTERAR O ESTADO DO CÓDIGO DURANTE O TESTE