Programming ≈ Fun

Written by Krešimir Bojčić

Sub-second RSpec Testing

One strength of TDD is fast feedback. You need your code to validate your assumptions. The faster the feedback, the better. In Rails “The Whole Universe” is required. Unsurprisingly tests are slow. I didn’t buy SSD to cut down test startup time from 7s to 3.8s.

The patient

Small example with 98 tests that are hardcore domain specific. Only one test suite with 8 tests needs Rails helpers for form rendering. If BDD is your game - this info is not going to help you much. I don’t buy it. Even headless drivers for integration testing (like Capybara) are way to slow for being useful in this “rapid feedback” scenarios. I am not against integrated tests but do agree with JBrains that says “Integration Tests Are a Scam”.

Option 1: Spork

  • The good: faster times 1.3s vs. 3.8s
  • The bad: you need to start it (here ‘screen’ helps so at least you don’t have to look at it)
  • The ugly:
    • needs to be restarted to pick up new things (based on config)
    • it is basically a hack - if it doesn’t work you might be in trouble to figure out why

In one line: Spork - it is great for Rails specific tests. Use it if you must.

Option 2: Go commando without ‘spec_helper’

  • Remove ‘spec_helper’ from require
  • Require all files that you need “manually” - forget the “all included hype” - it comes with price, price that you pay dearly every test run
  • Pull your logic out of Rails dependency claws, it is your logic darn it
  • Put your stuff in spec/lib folder
  • Put “slow” stuff in spec/slow folder

If you are using Vim you can add this to your vim.rc

map ,t :w\|!rspec spec/lib<cr>
map ,` :w\|!rspec spec/slow<cr>


In a nutshell

  • Don’t tangle tests with Rails stuff
  • Don’t use database in your tests
  • Stub out slow stuff
If you must it boils down to one thing - don’t do slow stuff if you want fast results.

I know it sounds dumb once you say it, but it still holds.

Integration tests have different agenda, so you have different approach there since it is enough to run them every once in a while. For TDD speed (or being fast enough) is crucial.

Conclusion

I’ve managed to get 0.67s for 90 tests that I run. It is not going to stay under second for very long but it is better than 1.3s than I get with Spork, and better than 3.8s that I get if I let ‘spec_helper’ include everything. I am keeping a close eye on my tests performance because the second they get slow enough, they will start dragging me to the “let’s fire it up and see” mode.

« Smalltalk, Lisp, Ruby Comparison - The Return of the Prodigal Son Speed comparison (Haskell, Ruby, Python, CLISP) »

Comments

Copyright © 2019 - Kresimir Bojcic (LinkedIn Profile)