Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A brief introduction to testing and TDD

A brief introduction to testing and TDD

Fernando Blat

August 30, 2012
Tweet

More Decks by Fernando Blat

Other Decks in Programming

Transcript

  1. Software testing is the process to verify that a piece

    of software has been developed according to the given specifications Thursday, August 30, 12
  2. Code that executes our code against a set of expectations

    and assertions Tests Thursday, August 30, 12
  3. Unit tests Tests individual parts of your code in a

    isolated way Thursday, August 30, 12
  4. 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
  5. 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
  6. 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
  7. And what about Rails? Easy unit testing for your models

    Product,  Review,  Tag,  User Thursday, August 30, 12
  8. Rails test folder test/ !"" fixtures/ !"" functional/ !"" integration/

    !"" performance/ !"" test_helper.rb #"" unit/ Thursday, August 30, 12
  9. 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
  10. ProductTest require  'test_helper' class  ProductTest  <  ActiveSupport::TestCase    #  test

     "the  truth"  do    #      assert  true    #  end end Thursday, August 30, 12
  11. Run the tests $  ruby  -­‐I  lib:test  test/unit/product_test.rb   Run

     options:   #  Running  tests: Thursday, August 30, 12
  12. 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
  13. 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
  14. New feature! Add friendly SEO URLs to our application Product

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

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

    URL: #  before example.com/products/1 #  after Thursday, August 30, 12
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. Acceptance Tests Helps focus on the development of code that

    is really needed Thursday, August 30, 12
  29. 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
  30. Test Driven Development Is a software development process that relies

    on the repetition of a very short development cycle Thursday, August 30, 12
  31. Test Driven Development 1 - Write a small test, run

    it and check that it fails Red Thursday, August 30, 12
  32. Test Driven Development 2 - Write the minimum piece of

    code to make that test pass. No more Green Thursday, August 30, 12
  33. Test Driven Development 3 - Refactor: means changing the internal

    structure of an existing code without changing its behaviour Refactor Thursday, August 30, 12
  34. Benefits of TDD We know that our code will continue

    to work Thursday, August 30, 12
  35. But I don’t have to do TDD It takes more

    time fix bugs Thursday, August 30, 12
  36. 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
  37. 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
  38. But I don’t have to do TDD It takes time

    to figure out if my changes will break something Thursday, August 30, 12
  39. “If I don’t need to make it work, I can

    go a lot faster.” -- Kent Beck Thursday, August 30, 12