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

Test with :)

703496dd6f9097efaa51f2b8c83b47ac?s=47 aquajach
October 26, 2013

Test with :)

The slides for Rubyconf China 2013 with animation off

703496dd6f9097efaa51f2b8c83b47ac?s=128

aquajach

October 26, 2013
Tweet

Transcript

  1. test with :) chen songyong

  2. about me

  3. about me • Remote worker! • Worked in start-ups, web

    consultancies , banks and digital agencies! • @aquajach in Twitter & Github
  4. test in old days

  5. do you ❤️ write test? do you write test?

  6. Q & A

  7. why you test • Forget to go for test first

  8. technical debt "we have not found anything to ease the

    transition from 'hairball of code with no unit tests' to 'unit-testable code'."
  9. trust your mind code

  10. my practice integration test controller test model / lib test

  11. my practice Test Implementation “Pay” button works controller deals with

    pay request model/lib makes the transaction views controller model/lib pass pass pass
  12. why you test • Forget to write test first! •

    Too much extra work
  13. is it true? Writing test adds 15-35% development time in

    return for a 40-90% reduction in defect density on otherwise like-for-like projects. - A paper in ESE •YES
  14. is it true? •No! v.s. test in rails console

  15. why you test • Forget to write test first! •

    Extra work! • Maintenance is painful
  16. it 'Sign Up should have title' do! visit '/tc/'! page.find('div#sign-up-modal').should

    have_css(‘#modalHeader.h5-sign-up-header’, :text => “Apple Now”) ! end
  17. why you test • Forget to write test first! •

    Extra work! • Maintenance is painful! • Running test takes longer and longer
  18. easiest way to go • CPU! ! • Data storage!

    ! • Memory
  19. test suite example • ruby 2.0.0 / rails 3.2.13 /

    rspec-rails ~>2.0 / PostgreSQL /Factory Girl / Database cleaner! • 370+ mixed with unit test, integration test (w/ capybara inc. JavaScript)! • No remote request (w/ VCR)
  20. Tool / Framework • spork! • zeus! • spring! •

    Parallel test! • Guard! • … … Ryan Bates’ Railscasts SKIP
  21. rspec v.s. rake spec Update: run via bin/rspec

  22. database clean strategy • :transaction! • simply rollback - fastest!

    • Failed at:! • multiple connections! • integration test
  23. database clean strategy • :truncation! • TRUNCATE table! • remove

    all records! • VACUUM table! • index! • Failed at:! • concurrent access to a table
  24. database clean strategy • :deletion! • DELETE from table! •

    scan the table! • less predictable! • Bad at:! • Large tables
  25. Deletion Truncation

  26. database Clean strategy

  27. refactor gemfile • stop loading gems not used in the

    code! • e.g. gem ‘unicorn’, require: false! • specify dependencies for test environment! • ‘jquery-rails’, ‘will_paginate’, ‘exception_notification’
  28. defer garbage collection

  29. Original + rspec command + DB clean strategy + refactor

    Gemfile + delay garbage collection 0 65 130 195 260
  30. factory v.s. fixture • Flexibility! ! • Callbacks & validations!

    ! • Speed
  31. ! best practice • create(:user) v.s. build(:user) v.s. build_stubbed(:user)! •

    require ‘spec_helper’ v.s. require ‘lib_class.rb’! • mock/stub or not to mock/stub! • let v.s. let! v.s. before block
  32. not about speed but... • Selective testing! • slow! •

    focus! • Fail immediately --fail-fast! • Display message immediately! -- format Fuubar! • rspec -p spec
  33. (real) Q & A