We are using RSpec in a rails project for unit testing. I would like to set up some performance tests in RSpec, but do it in a way as to not disrupt the 'regular' features and fixtures.
Ideally I'd be able to tag my performance specs in a certain way such that they are not run by default. Then when I specify to run these specs explicitly it will load a different set of fixtures (it makes sense to do performance testing with a much larger and more 'production-like' dataset).
Is this possible? It seems like it should be.
Has anyone set up something like this? How did you go about it?
Installing RSpec Boot up your terminal and punch in gem install rspec to install RSpec. Once that's done, you can verify your version of RSpec with rspec --version , which will output the current version of each of the packaged gems. Take a minute also to hit rspec --help and look through the various options available.
Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.
I managed to get what I was looking for via the following:
# Exclude :performance tagged specs by default
config.filter_run_excluding :performance => true
# When we're running a performance test load the test fixures:
config.before(:all, :performance => true) do
# load performance fixtures
require 'active_record/fixtures'
ActiveRecord::Fixtures.reset_cache
ActiveRecord::Fixtures.create_fixtures('spec/perf_fixtures', File.basename("products.yml", '.*'))
ActiveRecord::Fixtures.create_fixtures('spec/perf_fixtures', File.basename("ingredients.yml", '.*'))
end
# define an rspec helper for takes_less_than
require 'benchmark'
RSpec::Matchers.define :take_less_than do |n|
chain :seconds do; end
match do |block|
@elapsed = Benchmark.realtime do
block.call
end
@elapsed <= n
end
end
# example of a performance test
describe Api::ProductsController, "API Products controller", :performance do
it "should fetch all the products reasonably quickly" do
expect do
get :index, :format => :json
end.to take_less_than(60).seconds
end
end
But I tend to agree with Marnen's point that this isn't really the best idea for performance testing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With