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

Tests should be fast: why and how?

Masafumi Okura
September 04, 2019

Tests should be fast: why and how?

Speed is power, fast is good. But how about tests? Here I explain why speed of tests is important and how to improve it.

Masafumi Okura

September 04, 2019
Tweet

More Decks by Masafumi Okura

Other Decks in Technology

Transcript

  1. Tests should be fast: why and how? Event: Tokyo Rubyist

    Meetup Date: 2019-09-04 Speaker: OKURA Masafumi
  2. To make production code faster, we: • Measure performance with

    some specific tools such as “rack- mini-profiler” or “ruby-prof”. • Reduce N+1 queries with “bullet”. • Try to make Ruby code faster but it’s not a bottleneck. • Sometimes spend lots of time for small improvements.
  3. To make tests faster, we: • Profile tests with “—profile”

    option (if we use RSpec). • Look into slow tests and find the causes (described later). • Improve slow tests and win! • (If we struggle to find a bottleneck, “test-prof” gem helps us)!
  4. Software development without tests • We often miss errors/bugs before

    shipping. • We cannot do any refactoring because it causes bugs. • Fixing a bug causes more bugs including regressions. • These mean we develop software without any feedback.
  5. Strategy to make tests faster 1. Measure and profile using

    “profile” option and “test-prof” gem. 2. Make slow tests faster taking care of setup (before hook) and database interactions (typically “let” in RSpec). 3. Removing things your tests don’t need (optimizations and compilations).
  6. Strategy to make tests faster 1. Measure and profile using

    “profile” option and “test- prof” gem. 2. Make slow tests faster taking care of setup (before hook) and database interactions (typically “let” in RSpec). 3. Removing things your tests don’t need (optimizations and compilations).
  7. Profile option is: • Available natively for RSpec and as

    a plugin for Minitest • Used to show top 10 (default) slowest test cases. • The easiest way to know where’s slow in your test suite.
  8. “test-prof” is: • A collection of tools to profile your

    test suite. • Integrated with well-known general Ruby profiling tools such as “ruby-prof” and “stackprof”. • Used by many companies including GitLab and Dev.to.
  9. Example: Measuring time on before/let • Set RD_PROF=1 and run

    RSpec as usual • Get the output like below:
  10. Strategy to make tests faster 1. Measure and profile using

    “profile” option and “test-prof” gem. 2. Make slow tests faster taking care of setup (before hook) and database interactions (typically “let” in RSpec). 3. Removing things your tests don’t need (optimizations and compilations).
  11. Make slow tests faster taking care of setup (before hook)

    and database interactions (typically “let” in RSpec)
  12. Answer: Most of the time. (In my case, 5:50 out

    of 7:35 are spent on before and let)
  13. Notes on reducing before • Too much DRYness is the

    enemy here. Duplicate code from before block to it block and cut redundant parts. • Don’t mimic login through web interaction. Set cookie directly. • Use tags effectively. Load something only with certain tags.
  14. Notes on reducing let • If we use more than

    5 lets in one example, maybe too many lets (one of the exceptions is search). • Prefer build over create, prefer build_stubbed over build. • Use let! only when required.
  15. Strategy to make tests faster 1. Measure and profile using

    “profile” option and “test-prof” gem. 2. Make slow tests faster taking care of setup (before hook) and database interactions (typically “let” in RSpec). 3. Removing things your tests don’t need (optimizations and compilations).
  16. Compile once # config/webpacker.yml test: <<: *default compile: false #

    spec/rails_helper.rb RSpec.configure do |config| config.before(:suite) do Webpacker.compile end end
  17. Remove these for fast tests • Redundant `before each` call

    (or use some technique to load code only when required). • Redundant asset optimizations. • Webpacker compilation.
  18. Strategy to make tests faster 1. Measure and profile using

    “profile” option and “test-prof” gem. 2. Make slow tests faster taking care of setup (before hook) and database interactions (typically “let” in RSpec). 3. Removing things your tests don’t need (optimizations and compilations).
  19. pp self • Name: OKURA Masafumi • Software developer for

    several years using Ruby and Rails • Vimmer (and a staff of VimConf 2019!) • Freelance now, seeking international jobs!