@luiz_hespanha

luiz.hespanha@nubank.com.br

Desenvolvedor de Software @nubankbrasil
Testes generativos
Motivação
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 2
Testes automatizados.
Testes automatizados
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 3
• Garantir regressão rapidamente.
• Garantir que o que está “entrando
novo” funciona.
Testes automatizados
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 4
• Testes unitários (lógica)
• Testes de integração dentro de um
serviço (BD, Kafka, etc).
• Testes e2e (Integração entre os
serviços)
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 5
Disclaimer
Código Clojure nos próximos slides.
Testes baseados em exemplos
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 6
(= a b)
Testes baseados em exemplos
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 7
(= [5 2 1] (reverse [1 2 5])
(= [1 2 3 4 5] (reverse [5 4 3 2 1])
(= [5] (reverse [5])
(= [] (reverse [])
(= ["c" "b" "a"] (reverse ["a" "b" "c"]))
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 8
Testes automatizados
Nossos testes estão limitados a nossa
imaginação.
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 9
Testes automatizados
Como podemos melhorar os nossos
testes?
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 10
color
blank
Testes generativos
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 11
Testes generativos
Pensar em propriedades
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 12
O que são propriedades?
“Constraints" e “invariantes" que são
verdade *sempre*.
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 13
Um pouco mais formal…
“Property-based testing encourages a high level approach to testing in
the form of abstract invariants functions should satisfy universally,
with the actual test data generated for the programmer by the
testing library. In this way code can be hammered with thousands of
tests that would be infeasible to write by hand, often uncovering subtle
corner cases that wouldn’t be found otherwise.”

Real World Haskell by Bryan O’Sullivan, Don Stewart & John Goerzen
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 14
Testes generativos
(prop/for-all
[a (gen/vector gen/any)
[b (gen/vector gen/any)]
(= (count (concat a b))
(+ (count a) (count b))))]
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 15
Testes generativos
(tc/quick-check 1000 my-property)
;; => {:result true
;; :num-tests 1000
;; :seed 1395119077}
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 16
color
blank
Será que acha bugs mesmo?
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 17
Testes generativos
(-> #{} (conj 109) (conj -110)
transient (disj! -110)
persistent! (conj -110)
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 18
Testes generativos
(def transient-property
(prop/for-all
[a (gen/vector gen-action)]
(= (apply-actions #{} a)
(apply-actions #{} (filter-transients a)))))
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 19
Testes generativos
(tc/quick-check 100000 transient-property)
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 20
Fail…
{:result false,
:failing-size 92,
:num-tests 2893,
:fail "...",
:shrunk
{:total-nodes-visited 440
:depth 83
:result false
:smallest "..."}}
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 21
Generators
(prop/for-all
[a (gen/vector gen/any)
b (gen/vector gen/any)]
(= (count (concat a b))
(+ (count a) (count b))))
Generators
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 22
Generators
any any-printable boolean byte bytes char
char-alpha-numeric char-ascii hash-map int keyword
list map nat neg-int pos-int ratio s-neg-int s-posint
string string-alpha-numeric string-ascii tuple
vector
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 23
Generators
podemos criar os nossos próprios
geradores.
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 24
Generators
(prop/for-all
[p (gen/vector gen/purchases)]
(= (:total-value (close-bill p))
(sum-purchases p)))
Fluxo de trabalho com testes generativos
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 25
Sempre que o teste generativo
encontrar um bug, escrever um teste
unitário com o exemplo que falhou.
Fluxo de trabalho com testes generativos
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 26
Durante o desenvolvimento deixar o
número de testes alto, e diminuir
quando fizer o “merge" no “master".
Testes generativos
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 27
• Onde estamos usando no Nubank:
• Sistema de contabilidade interno.
• Criação de números de cartão de
crédito.
• Integração com terceiros via
protocolos de baixo nível.
Ferramentas
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 28
Clojure -> https://github.com/clojure/test.check
Scala -> http://www.scalatest.org/user_guide/property_based_testing
Java -> https://github.com/pholser/junit-quickcheck
Haskell -> https://hackage.haskell.org/package/QuickCheck
…provavelmente tem framework para outras linguagens também.
…de qualquer forma, os seus testes não precisam usar a mesma linguagem da sua
aplicação.
Conclusão
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 29
Testes generativos
são complementares
aos seus testes
baseados em
exemplos, e ajudam
a melhorar a
confiabilidade dos
seus testes.
full page
photo
“Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 30
Obrigado!

Testes generativos

  • 1.
  • 2.
    Motivação “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 2 Testes automatizados.
  • 3.
    Testes automatizados “Nu MinimalKeynote Template” © Nu Bank - 05.01.2014 3 • Garantir regressão rapidamente. • Garantir que o que está “entrando novo” funciona.
  • 4.
    Testes automatizados “Nu MinimalKeynote Template” © Nu Bank - 05.01.2014 4 • Testes unitários (lógica) • Testes de integração dentro de um serviço (BD, Kafka, etc). • Testes e2e (Integração entre os serviços)
  • 5.
    “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 5 Disclaimer Código Clojure nos próximos slides.
  • 6.
    Testes baseados emexemplos “Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 6 (= a b)
  • 7.
    Testes baseados emexemplos “Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 7 (= [5 2 1] (reverse [1 2 5]) (= [1 2 3 4 5] (reverse [5 4 3 2 1]) (= [5] (reverse [5]) (= [] (reverse []) (= ["c" "b" "a"] (reverse ["a" "b" "c"]))
  • 8.
    “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 8 Testes automatizados Nossos testes estão limitados a nossa imaginação.
  • 9.
    “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 9 Testes automatizados Como podemos melhorar os nossos testes?
  • 10.
    “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 10 color blank Testes generativos
  • 11.
    “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 11 Testes generativos Pensar em propriedades
  • 12.
    “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 12 O que são propriedades? “Constraints" e “invariantes" que são verdade *sempre*.
  • 13.
    “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 13 Um pouco mais formal… “Property-based testing encourages a high level approach to testing in the form of abstract invariants functions should satisfy universally, with the actual test data generated for the programmer by the testing library. In this way code can be hammered with thousands of tests that would be infeasible to write by hand, often uncovering subtle corner cases that wouldn’t be found otherwise.” Real World Haskell by Bryan O’Sullivan, Don Stewart & John Goerzen
  • 14.
    “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 14 Testes generativos (prop/for-all [a (gen/vector gen/any) [b (gen/vector gen/any)] (= (count (concat a b)) (+ (count a) (count b))))]
  • 15.
    “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 15 Testes generativos (tc/quick-check 1000 my-property) ;; => {:result true ;; :num-tests 1000 ;; :seed 1395119077}
  • 16.
    “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 16 color blank Será que acha bugs mesmo?
  • 17.
    “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 17 Testes generativos (-> #{} (conj 109) (conj -110) transient (disj! -110) persistent! (conj -110)
  • 18.
    “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 18 Testes generativos (def transient-property (prop/for-all [a (gen/vector gen-action)] (= (apply-actions #{} a) (apply-actions #{} (filter-transients a)))))
  • 19.
    “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 19 Testes generativos (tc/quick-check 100000 transient-property)
  • 20.
    “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 20 Fail… {:result false, :failing-size 92, :num-tests 2893, :fail "...", :shrunk {:total-nodes-visited 440 :depth 83 :result false :smallest "..."}}
  • 21.
    “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 21 Generators (prop/for-all [a (gen/vector gen/any) b (gen/vector gen/any)] (= (count (concat a b)) (+ (count a) (count b)))) Generators
  • 22.
    “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 22 Generators any any-printable boolean byte bytes char char-alpha-numeric char-ascii hash-map int keyword list map nat neg-int pos-int ratio s-neg-int s-posint string string-alpha-numeric string-ascii tuple vector
  • 23.
    “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 23 Generators podemos criar os nossos próprios geradores.
  • 24.
    “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 24 Generators (prop/for-all [p (gen/vector gen/purchases)] (= (:total-value (close-bill p)) (sum-purchases p)))
  • 25.
    Fluxo de trabalhocom testes generativos “Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 25 Sempre que o teste generativo encontrar um bug, escrever um teste unitário com o exemplo que falhou.
  • 26.
    Fluxo de trabalhocom testes generativos “Nu Minimal Keynote Template” © Nu Bank - 05.01.2014 26 Durante o desenvolvimento deixar o número de testes alto, e diminuir quando fizer o “merge" no “master".
  • 27.
    Testes generativos “Nu MinimalKeynote Template” © Nu Bank - 05.01.2014 27 • Onde estamos usando no Nubank: • Sistema de contabilidade interno. • Criação de números de cartão de crédito. • Integração com terceiros via protocolos de baixo nível.
  • 28.
    Ferramentas “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 28 Clojure -> https://github.com/clojure/test.check Scala -> http://www.scalatest.org/user_guide/property_based_testing Java -> https://github.com/pholser/junit-quickcheck Haskell -> https://hackage.haskell.org/package/QuickCheck …provavelmente tem framework para outras linguagens também. …de qualquer forma, os seus testes não precisam usar a mesma linguagem da sua aplicação.
  • 29.
    Conclusão “Nu Minimal KeynoteTemplate” © Nu Bank - 05.01.2014 29 Testes generativos são complementares aos seus testes baseados em exemplos, e ajudam a melhorar a confiabilidade dos seus testes.
  • 30.
    full page photo “Nu MinimalKeynote Template” © Nu Bank - 05.01.2014 30 Obrigado!