Speed Up Your Paperclip Tests

Published on January 05, 2010 by Jesse Storimer

We all love Paperclip. One of the reasons why is that it makes it painless to resize images. Which is great when you are working in development or on production, but image resizing can really slow down your automated tests.

For example, I was working on a project that used factories instead of fixtures. Every time a factory object was created, which happened multiple times in most tests, Paperclip had to resize its images.

I added this little snippet to my test_helper.rb and saw my total testing time go from 100 seconds to 30 seconds. Meaning that 2/3 of the time spent running tests was actually spent resizing images!

# test/test_helper.rb

class ActiveSupport::TestCase
  # ...
end

# replace the class name with one appropriate for your app
class Screenshot
  before_post_process do |image|
    false # halts processing
  end
end

I’m sure that there are some automated testing purists out there who will cry that this method no longer tests the entire stack. It’s possible to not have ImageMagick installed on your system and still have passing tests. My answer to that is that any good testing strategy will involve an element of human testing which should catch this kind of problem.


Like what you read?

Join 2,000+ Ruby programmers improving their skills with exclusive content about digging deeper with sockets, processes, threads, and more - delivered to your inbox weekly.

I'll never send spam and you can unsubscribe any time.


comments powered by Disqus