Slide 1

Slide 1 text

A brief introduction to testing and TDD Madrid.rb Thursday, August 30, 12

Slide 2

Slide 2 text

Fernando Blat @ferblape Raúl Naveiras @rnaveiras Thursday, August 30, 12

Slide 3

Slide 3 text

(1) Testing, general ideas (2) Test Driven Development (3) Miscellaneous stuff Thursday, August 30, 12

Slide 4

Slide 4 text

(1) Testing, general ideas (2) Test Driven Development (3) Miscellaneous stuff Thursday, August 30, 12

Slide 5

Slide 5 text

Software testing is the process to verify that a piece of software has been developed according to the given specifications Thursday, August 30, 12

Slide 6

Slide 6 text

Code that executes our code against a set of expectations and assertions Tests Thursday, August 30, 12

Slide 7

Slide 7 text

Unit tests Tests individual parts of your code in a isolated way Thursday, August 30, 12

Slide 8

Slide 8 text

def  get_last_element(array)    array.last end Thursday, August 30, 12

Slide 9

Slide 9 text

def  get_last_element(array)    array.last end Thursday, August 30, 12

Slide 10

Slide 10 text

def  get_last_element(array)    array.last end Thursday, August 30, 12

Slide 11

Slide 11 text

def  get_last_element(array)    array.last end def  test_get_last_element Thursday, August 30, 12

Slide 12

Slide 12 text

def  get_last_element(array)    array.last end def  test_get_last_element    a  =  [1,2,3] Thursday, August 30, 12

Slide 13

Slide 13 text

def  get_last_element(array)    array.last end def  test_get_last_element    a  =  [1,2,3]    last  =  get_last_element(a) Thursday, August 30, 12

Slide 14

Slide 14 text

def  get_last_element(array)    array.last end def  test_get_last_element    a  =  [1,2,3]    last  =  get_last_element(a)    assert_equal  last,  3 Thursday, August 30, 12

Slide 15

Slide 15 text

def  get_last_element(array)    array.last end def  test_get_last_element    a  =  [1,2,3]    last  =  get_last_element(a)    assert_equal  last,  3 end Thursday, August 30, 12

Slide 16

Slide 16 text

And what about Rails? Easy unit testing for your models Thursday, August 30, 12

Slide 17

Slide 17 text

And what about Rails? Easy unit testing for your models Product,  Review,  Tag,  User Thursday, August 30, 12

Slide 18

Slide 18 text

Rails test folder test/ !"" fixtures/ !"" functional/ !"" integration/ !"" performance/ !"" test_helper.rb #"" unit/ Thursday, August 30, 12

Slide 19

Slide 19 text

Rails test folder test/ !"" fixtures/ !"" functional/ !"" integration/ !"" performance/ !"" test_helper.rb #"" unit/ !"" helpers/ !"" product_test.rb !"" review_test.rb !"" tag_test.rb #"" user_test.rb Thursday, August 30, 12

Slide 20

Slide 20 text

ProductTest require  'test_helper' class  ProductTest  <  ActiveSupport::TestCase    #  test  "the  truth"  do    #      assert  true    #  end end Thursday, August 30, 12

Slide 21

Slide 21 text

Run the tests Thursday, August 30, 12

Slide 22

Slide 22 text

Run the tests $  ruby  -­‐I  lib:test  test/unit/product_test.rb   Thursday, August 30, 12

Slide 23

Slide 23 text

Run the tests $  ruby  -­‐I  lib:test  test/unit/product_test.rb   Run  options:   Thursday, August 30, 12

Slide 24

Slide 24 text

Run the tests $  ruby  -­‐I  lib:test  test/unit/product_test.rb   Run  options:   #  Running  tests: Thursday, August 30, 12

Slide 25

Slide 25 text

Run the tests $  ruby  -­‐I  lib:test  test/unit/product_test.rb   Run  options:   #  Running  tests: Finished  tests  in  0.001588s,  0.0000  tests/s,  0.0000   assertions/s. Thursday, August 30, 12

Slide 26

Slide 26 text

Run the tests $  ruby  -­‐I  lib:test  test/unit/product_test.rb   Run  options:   #  Running  tests: Finished  tests  in  0.001588s,  0.0000  tests/s,  0.0000   assertions/s. 0  tests,  0  assertions,  0  failures,  0  errors,  0  skips Thursday, August 30, 12

Slide 27

Slide 27 text

New feature! Thursday, August 30, 12

Slide 28

Slide 28 text

New feature! Add friendly SEO URLs to our application Thursday, August 30, 12

Slide 29

Slide 29 text

New feature! Add friendly SEO URLs to our application Product URL: Thursday, August 30, 12

Slide 30

Slide 30 text

New feature! Add friendly SEO URLs to our application Product URL: #  before Thursday, August 30, 12

Slide 31

Slide 31 text

New feature! Add friendly SEO URLs to our application Product URL: #  before example.com/products/1 Thursday, August 30, 12

Slide 32

Slide 32 text

New feature! Add friendly SEO URLs to our application Product URL: #  before example.com/products/1 #  after Thursday, August 30, 12

Slide 33

Slide 33 text

New feature! Add friendly SEO URLs to our application Product URL: #  before example.com/products/1 #  after example.com/products/renault-­‐megane Thursday, August 30, 12

Slide 34

Slide 34 text

Nicenamificating class Product < ActiveRecord::Base ... # Sanitize name into to be URL friendly # I.e “Renault Megane” -> “renault-megane” def nicename name.downcase.gsub(/[^a-z]/, "-") end end Thursday, August 30, 12

Slide 35

Slide 35 text

ProductTest require  'test_helper' class  ProductTest  <  ActiveSupport::TestCase    test  "#nicename  method"  do        product  =  Product.new        product.name  =  "Samsung  Galaxy"        assert_equal  "samsung-­‐galaxy",  product.nicename    end end Thursday, August 30, 12

Slide 36

Slide 36 text

Run the tests $  ruby  -­‐I  lib:test  test/unit/product_test.rb   Run  options:   #  Running  tests: . Finished  tests  in  0.091707s,  10.9043  tests/s,  10.9043   assertions/s. 1  tests,  1  assertions,  0  failures,  0  errors,  0  skips Thursday, August 30, 12

Slide 37

Slide 37 text

ProductTest require  'test_helper' class  ProductTest  <  ActiveSupport::TestCase    test  "#nicename  method"  do        product  =  Product.new        product.name  =  "Samsung  Galaxy"        assert_equal  "samsung-­‐galaxy",  product.nicename        product.name  =  "iPhone  5S"        assert_equal  "iphone-­‐5s",  product.nicename    end end Thursday, August 30, 12

Slide 38

Slide 38 text

ProductTest require  'test_helper' class  ProductTest  <  ActiveSupport::TestCase    test  "#nicename  method"  do        product  =  Product.new        product.name  =  "Samsung  Galaxy"        assert_equal  "samsung-­‐galaxy",  product.nicename        product.name  =  "iPhone  5S"        assert_equal  "iphone-­‐5s",  product.nicename    end end Thursday, August 30, 12

Slide 39

Slide 39 text

Run the tests $  ruby  -­‐I  lib:test  test/unit/product_test.rb   Run  options:   #  Running  tests: F Finished  tests  in  0.091707s,  10.9043  tests/s,  10.9043   assertions/s. 1)  Failure: test_#nicename(ProductTest)  [test/unit/product_test.rb:6]: <"iphone-­‐5s">  expected  but  was <"iphone-­‐-­‐">. 1  tests,  1  assertions,  1  failures,  0  errors,  0  skips Thursday, August 30, 12

Slide 40

Slide 40 text

Nicenamificating class Product < ActiveRecord::Base ... # Sanitize name into to be URL friendly # I.e “Renault Megane” -> “renault-megane” def nicename name.downcase.gsub(/[^a-z0-9]/, "-") end end Thursday, August 30, 12

Slide 41

Slide 41 text

Nicenamificating class Product < ActiveRecord::Base ... # Sanitize name into to be URL friendly # I.e “Renault Megane” -> “renault-megane” def nicename name.downcase.gsub(/[^a-z0-9]/, "-") end end Thursday, August 30, 12

Slide 42

Slide 42 text

Run the tests $  ruby  -­‐I  lib:test  test/unit/product_test.rb   Run  options:   #  Running  tests: . Finished  tests  in  0.082707s,  11.5041  tests/s,  10.5043   assertions/s. 1  tests,  2  assertions,  0  failures,  0  errors,  0  skips Thursday, August 30, 12

Slide 43

Slide 43 text

Acceptance Tests Thursday, August 30, 12

Slide 44

Slide 44 text

Acceptance Tests Is a set of one or more automate tests conducted to determine if the requirements of a specification or contract are met Thursday, August 30, 12

Slide 45

Slide 45 text

Acceptance Tests Provides more value because starts from the interface Thursday, August 30, 12

Slide 46

Slide 46 text

Acceptance Tests Helps to think like our feature should work Thursday, August 30, 12

Slide 47

Slide 47 text

Acceptance Tests Helps focus on the development of code that is really needed Thursday, August 30, 12

Slide 48

Slide 48 text

Acceptance Tests Less fragile is not coupled to the implementation Thursday, August 30, 12

Slide 49

Slide 49 text

Acceptance Tests require 'acceptance/acceptance_helper' feature 'Login', %q{ Given that I want to protect the products from not logged users When click in "New product" Then I should be redirected to login form } do scenario 'Should show login page when clicking new product link' do visit '/' click_link 'New Product' current_path.should == '/login' page.should have_content 'Log in' end end Thursday, August 30, 12

Slide 50

Slide 50 text

(1) Testing, general ideas (2) Test Driven Development (3) Miscellaneous stuff Thursday, August 30, 12

Slide 51

Slide 51 text

Test Driven Development Is a software development process that relies on the repetition of a very short development cycle Thursday, August 30, 12

Slide 52

Slide 52 text

Test Driven Development Thursday, August 30, 12

Slide 53

Slide 53 text

Test Driven Development Involves writing test before writing the code being tested Thursday, August 30, 12

Slide 54

Slide 54 text

Test Driven Development 1 - Write a small test, run it and check that it fails Red Thursday, August 30, 12

Slide 55

Slide 55 text

Test Driven Development 2 - Write the minimum piece of code to make that test pass. No more Green Thursday, August 30, 12

Slide 56

Slide 56 text

Test Driven Development 3 - Refactor: means changing the internal structure of an existing code without changing its behaviour Refactor Thursday, August 30, 12

Slide 57

Slide 57 text

Test Driven Development Repeat Red Green Refactor Thursday, August 30, 12

Slide 58

Slide 58 text

Benefits of TDD We know that our code is working! Thursday, August 30, 12

Slide 59

Slide 59 text

Benefits of TDD We know that our code will continue to work Thursday, August 30, 12

Slide 60

Slide 60 text

Benefits of TDD We know when we are done Thursday, August 30, 12

Slide 61

Slide 61 text

Benefits of TDD Our tests helped us design our code Thursday, August 30, 12

Slide 62

Slide 62 text

Benefits of TDD Our tests are documentation of our code does Thursday, August 30, 12

Slide 63

Slide 63 text

Benefits of TDD Able to release more often Thursday, August 30, 12

Slide 64

Slide 64 text

But I don’t have to do TDD It takes more time fix bugs Thursday, August 30, 12

Slide 65

Slide 65 text

But I don’t have to do TDD It takes more time to constantly manually test code to see if it’s all working Thursday, August 30, 12

Slide 66

Slide 66 text

But I don’t have to do TDD It takes more time to figure out what code is supposed to do Thursday, August 30, 12

Slide 67

Slide 67 text

But I don’t have to do TDD It takes time to figure out if my changes will break something Thursday, August 30, 12

Slide 68

Slide 68 text

Benefits of TDD Think long-term, not shot-term dude!! Thursday, August 30, 12

Slide 69

Slide 69 text

“If I don’t need to make it work, I can go a lot faster.” -- Kent Beck Thursday, August 30, 12

Slide 70

Slide 70 text

SHOW TIME!! -- Bebanjo Style Thursday, August 30, 12

Slide 71

Slide 71 text

(1) Testing, general ideas (2) Test Driven Development (3) Miscellaneous stuff Thursday, August 30, 12

Slide 72

Slide 72 text

http://jenkins-ci.org/ Thursday, August 30, 12

Slide 73

Slide 73 text

Thursday, August 30, 12

Slide 74

Slide 74 text

http://travis-ci.org/ Thursday, August 30, 12

Slide 75

Slide 75 text

LINKS http://guides.rubyonrails.org/testing.html http://everydayrails.com/2011/01/11/beginning-rails- testing.html http://www.codeschool.com/courses/rails-testing-for- zombies Thursday, August 30, 12

Slide 76

Slide 76 text

Any Questions? Thursday, August 30, 12

Slide 77

Slide 77 text

Thursday, August 30, 12

Slide 78

Slide 78 text

Thursday, August 30, 12

Slide 79

Slide 79 text

Thank you!! Thursday, August 30, 12