Skip to content

Instantly share code, notes, and snippets.

@pixeltrix
Last active December 31, 2015 17:39
Show Gist options
  • Save pixeltrix/8021851 to your computer and use it in GitHub Desktop.
Save pixeltrix/8021851 to your computer and use it in GitHub Desktop.
Comparison of functional tests vs. integration tests using current implementation and a simple implementation using Rack::Test
require 'benchmark/ips'
require "action_controller/railtie"
class BlogApplication < Rails::Application
config.eager_load = false
config.root = File.dirname(__FILE__)
config.session_store :cookie_store, key: 'session'
config.secret_key_base = 'secret'
end
BlogApplication.initialize!
BlogApplication.routes.draw do
resources :posts, only: %w[index]
end
require 'rails/test_help'
class PostsController < ActionController::Base
def index
render text: 'Hello, World!'
end
end
class FunctionalTest < ActionController::TestCase
tests PostsController
def test_posts_index
get :index
assert_equal 'Hello, World!', response.body
end
end
class IntegrationTest < ActionDispatch::IntegrationTest
def test_posts_index
get '/posts'
assert_equal 'Hello, World!', response.body
end
end
class RackTest < ActiveSupport::TestCase
include Rack::Test::Methods
def app
Rails.application
end
def response
last_response
end
def test_posts_index
get '/posts'
assert_equal 'Hello, World!', response.body
end
end
Benchmark.ips do |x|
runner = MiniTest::Unit.new
functional = FunctionalTest.new('test_posts_index')
integration = IntegrationTest.new('test_posts_index')
rack = RackTest.new('test_posts_index')
x.report('Functional Test') do
functional.run(runner)
end
x.report('Integration Test') do
integration.run(runner)
end
x.report('Rack Test') do
rack.run(runner)
end
end
$ ruby test_comparison.rb
Calculating -------------------------------------
Functional Test 104 i/100ms
Integration Test 65 i/100ms
Rack Test 83 i/100ms
-------------------------------------------------
Functional Test 1056.1 (±5.4%) i/s - 5304 in 5.037137s
Integration Test 671.0 (±4.5%) i/s - 3380 in 5.047506s
Rack Test 845.4 (±4.6%) i/s - 4233 in 5.017750s
Run options: --seed 14859
# Running tests:
...
Finished tests in 0.005849s, 512.9082 tests/s, 512.9082 assertions/s.
3 tests, 3 assertions, 0 failures, 0 errors, 0 skips
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment