testes generativos

Post on 15-Apr-2017

212 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

@luiz_hespanhaluiz.hespanha@nubank.com.brDesenvolvedor 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!

top related