Slide 1

Slide 1 text

TESTES DE PROPRIEDADE (((on Rails)))

Slide 2

Slide 2 text

Eu ♥ Ruby!

Slide 3

Slide 3 text

Eu ♥ Clojure!

Slide 4

Slide 4 text

(((((((((((((yep!)))))))))))))

Slide 5

Slide 5 text

4:20 PM

Slide 6

Slide 6 text

Andrew Rosa @_andrewhr

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Prop… what?

Slide 9

Slide 9 text

it "sorts a list" do v = [3, 1, 2] s = sort(v) expect(s).to eq([1, 2, 3]) end

Slide 10

Slide 10 text

it "sorts a list" do v = [3, 1, 2] s = sort(v) expect(s).to eq([1, 2, 3]) end

Slide 11

Slide 11 text

it "sorts a list" do v = [3, 1, 2] s = sort(v) expect(s).to eq([1, 2, 3]) end

Slide 12

Slide 12 text

it "sorts a list" do v = [3, 1, 2] s = sort(v) expect(s).to eq([1, 2, 3]) end

Slide 13

Slide 13 text

it "sorts a list" do v = [3, 1, 2] s = sort(v) expect(s).to eq([1, 2, 3]) end

Slide 14

Slide 14 text

EXEMPLO

Slide 15

Slide 15 text

#sorts ascending sorts an empty list sorts a list with single item sorts a list with multiple items ! Finished in 0.00179 seconds 3 examples, 0 failures

Slide 16

Slide 16 text

PROPRIEDADE

Slide 17

Slide 17 text

∀ a, b → int, int (a + b) = (b + a)

Slide 18

Slide 18 text

∀ a, b → int, int (a + b) = (b + a)

Slide 19

Slide 19 text

∀ a, b → int, int (a + b) = (b + a)

Slide 20

Slide 20 text

∀ a, b → int, int (a + b) = (b + a)

Slide 21

Slide 21 text

∀ a, b → int, int (a + b) = (b + a)

Slide 22

Slide 22 text

∀ a, b → int, int (a + b) = (b + a)

Slide 23

Slide 23 text

INVARIANTE

Slide 24

Slide 24 text

∀ v → vector int each pair a, b in sort(v) a <= b

Slide 25

Slide 25 text

∀ v → vector int each pair a, b in sort(v) a <= b

Slide 26

Slide 26 text

∀ v → vector int each pair a, b in sort(v) a <= b

Slide 27

Slide 27 text

∀ v → vector int each pair a, b in sort(v) a <= b

Slide 28

Slide 28 text

∀ v → vector int each pair a, b in sort(v) a <= b

Slide 29

Slide 29 text

∀ v → vector int each pair a, b in sort(v) a <= b

Slide 30

Slide 30 text

GERADORES

Slide 31

Slide 31 text

∀ v → vector int each pair a, b in sort(v) a <= b

Slide 32

Slide 32 text

∀ v → vector int each pair a, b in sort(v) a <= b v → [] v → [-1] v → [2 -1 1 -1 -2 5] v → [4] v → [7 9 -4 -4] …

Slide 33

Slide 33 text

CONTRA-EXEMPLO a.k.a. uma falha

Slide 34

Slide 34 text

[23 -17 8 -20 -35 -25 -22 10 11 -19 9 -3 -42 -33 -34 -11 7 -28 -5 38 39 -26 -29 42 0 -41 33 -35 19]

Slide 35

Slide 35 text

SHRINKING

Slide 36

Slide 36 text

!

Slide 37

Slide 37 text

!

Slide 38

Slide 38 text

[23 -17 8 -20 -35 -25 -22 10 11 -19 9 -3 -42 -33 -34 -11 7 -28 -5 38 39 -26 -29 42 0 -41 33 -35 19]

Slide 39

Slide 39 text

[42 0]

Slide 40

Slide 40 text

def sort(v) if v.include?(42) v else v.sort end end

Slide 41

Slide 41 text

COBERTURA

Slide 42

Slide 42 text

01 def sort(v) 02 if v.include?(42) 03 v 04 else 05 v.sort 06 end 07 end

Slide 43

Slide 43 text

01 def sort(v) 02 if v.include?(42) 03 v 04 else 05 v.sort 06 end 07 end v1 v2 v3 v4 …

Slide 44

Slide 44 text

PROPRIEDADE EXEMPLO

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

EXEMPLO in = “hello world!” out = “SGVsbG8gd29ybGQ=\n” expect(encode(in)).to eq(out)

Slide 47

Slide 47 text

PROPRIEDADE ∀ in → string in = decode(encode(in))

Slide 48

Slide 48 text

QuickCheck http://www.cse.chalmers.se/~rjmh/QuickCheck

Slide 49

Slide 49 text

Erlang QuickCheck http://www.quviq.com/products/erlang-quickcheck

Slide 50

Slide 50 text

Rantly https://github.com/hayeah/rantly

Slide 51

Slide 51 text

test.check https://github.com/clojure/test.check

Slide 52

Slide 52 text

…on Rails?

Slide 53

Slide 53 text

(+ clj-webdriver test.check)

Slide 54

Slide 54 text

Rails App Banco de Dados Testes Selenium* API webdriver JDBC UI

Slide 55

Slide 55 text

{:action :add-entry :via :ui :value 2995}

Slide 56

Slide 56 text

(defmulti dispatch! :action) ! (defmethod dispatch! :add-entry [action] (to base-url) (input-text “#entry_value" (str (:value action))) (submit "#entry_value"))

Slide 57

Slide 57 text

(defspec track-all-money-on-database 25 (for-all [actions gen-actions] (prepare-database! test-db) (dispatch-all! actions) (= (total-of actions) (total-recorded-in test-db))))

Slide 58

Slide 58 text

(defspec track-all-money-on-database 25 (for-all [actions gen-actions] (prepare-database! test-db) (dispatch-all! actions) (= (total-of actions) (total-recorded-in test-db))))

Slide 59

Slide 59 text

(defspec track-all-money-on-database 25 (for-all [actions gen-actions] (prepare-database! test-db) (dispatch-all! actions) (= (total-of actions) (total-recorded-in test-db))))

Slide 60

Slide 60 text

(defspec track-all-money-on-database 25 (for-all [actions gen-actions] (prepare-database! test-db) (dispatch-all! actions) (= (total-of actions) (total-recorded-in test-db))))

Slide 61

Slide 61 text

(defspec track-all-money-on-database 25 (for-all [actions gen-actions] (prepare-database! test-db) (dispatch-all! actions) (= (total-of actions) (total-recorded-in test-db))))

Slide 62

Slide 62 text

(defspec track-all-money-on-database 25 (for-all [actions gen-actions] (prepare-database! test-db) (dispatch-all! actions) (= (total-of actions) (total-recorded-in test-db))))

Slide 63

Slide 63 text

(defspec track-all-money-on-database 25 (for-all [actions gen-actions] (prepare-database! test-db) (dispatch-all! actions) (= (total-of actions) (total-recorded-in test-db))))

Slide 64

Slide 64 text

https://github.com/andrewhr/rails-test-check-example

Slide 65

Slide 65 text

Ashton Kemerling https://www.youtube.com/watch?v=HXGpBrmR70U

Slide 66

Slide 66 text

Ashton Kemerling https://www.youtube.com/watch?v=HXGpBrmR70U Também no Ruby Rogues #196 ;-)

Slide 67

Slide 67 text

Simulant https://github.com/Datomic/simulant

Slide 68

Slide 68 text

IDÉIAS LÁ FORA Wild, Wild West

Slide 69

Slide 69 text

Obrigado! Andrew @_andrewhr