View The Space is Hiring

Thursday, December 8, 2011

Cut down on those objects to speed up your cucumber tests

Chances are that a big reason your tests are so slow is all of those extra objects you are creating. Recently I cut down on the objects in my tests and I was able to speed up by cucumber runs by 30-40%. I achieved most of those gains by making some small changes to factory girl. I don't want to bore you with the detail of our object model but it goes something like this. Our main object is a space and our tests frequently create several of them. The problem is that a space has a reference to a user, property, an contacts. Each of those actually have references to other objects. Just by creating a few spaces we end up creating a web of objects but our tests didn't need all of these extra objects. Enter the default objects within our factory_girl definitions:





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def default_user
default_email = 'default@test.com'
User.find_by_email(default_email) || Factory(:user, :email=>default_email)
end

def default_property
street_address = '5555 5th Ave'
Property.find_by_street_address(street_address) || Factory(:property, :street_address=>street_address)
end

def default_space
floor = "99th-98th floor"
Space.find_by_floor(floor) || Factory(:space, :floor=>floor)
end


factory :space do
property { default_property }
user { default_user }
suite { "##{Forgery(:basic).number.to_i}" }
floor { Forgery(:basic).number.to_i }
rental_rate { Forgery(:basic).number * 100 }
description { Forgery(:lorem_ipsum).words(rand 100) }
space_available "25,000 sq ft"
availability_type "arranged"
status 'active'
end




Instead of creating new properties, users, and contacts each time a space is created, by default all spaces share the same nested objects. Most often this is not at all a problem in any of our tests. If we do require spaces do not share the same property, we specify it the Given section of our test.

3 comments:

  1. very nice! check out parallel_tests as well, works pretty nice for CI. needed some work for external services but i've seen some gains

    ReplyDelete
  2. @ilya - I use parallel_tests as well. Everything helps :-).

    ReplyDelete
  3. Looking for more exciting and interesting gourmet recipes ... try to visit my favorite food site ... www.gourmetrecipe.com ... they have lots of recipes and cuisines for you to choose from! Not only that, they do have also recipe video for you to watch!

    ReplyDelete