spork + autotest 实现rails自动化测试

针对队友自动化测试的总结,重新尝试了一遍,以下是总结,大家可以看一看

注意:这个demo的前提是已经配置好devise和Rspec,我的博客都有这方面的博文,可以参考


一、安装sport和autotest-rails

在gemfile中添加

gem "spork","~>0.9.2"

gem "autotest-rails","~>4.1.2"

执行 bundle install

安装spork

spork --bootstrap

输出:

Using RSpec

Bootstrapping /home/aaron/code/Rspec_demo/spec/spec_helper.rb.

Done. Edit /home/aaron/code/Rspec_demo/spec/spec_helper.rb now with your favorite text editor and follow the instructions.

上面做的就是修改spec/spec_helper.rb文件,在其头部添加了两段方法。Spork.prefork(只在启动时执行一次)和Spork.each_run(每次被RSpec调用时均被执行)。

 

二、修改Rspec配置,实现rspec与spork交互

require 'rubygems'

require 'spork'

#uncomment the following line to use spork with the debugger

#require 'spork/ext/ruby-debug'

Spork.prefork do

  # Loading more in this block will cause your tests to run faster. However,

  # if you change any configuration or code from libraries loaded here, you'll

  # need to restart spork for it take effect.

  # This file is copied to spec/ when you run 'rails generate rspec:install'

  

  ENV["RAILS_ENV"] ||= 'test'

  require File.expand_path("http://www.cnblogs.com/config/environment", __FILE__)

  require 'rspec/rails'

  require 'rspec/autorun'

  # Requires supporting ruby files with custom matchers and macros, etc,

  # in spec/support/ and its subdirectories.

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|

    # ## Mock Framework

    #

    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:

    #

    # config.mock_with :mocha

    # config.mock_with :flexmock

    # config.mock_with :rr

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures

    config.fixture_path = "#{::Rails.root}/spec/fixtures"

    # If you're not using ActiveRecord, or you'd prefer not to run each of your

    # examples within a transaction, remove the following line or assign false

    # instead of true.

    config.use_transactional_fixtures = false

    # If true, the base class of anonymous controllers will be inferred

    # automatically. This will be the default behavior in future versions of

    # rspec-rails.

    config.infer_base_class_for_anonymous_controllers = false

    # Run specs in random order to surface order dependencies. If you find an

    # order dependency and want to debug it, you can fix the order by providing

    # the seed, which is printed after each run.

    #     --seed 1234

  config.order = "random"

end

Spork.each_run do

  # This code will be run each time you run your specs.

  

  load "#{ Rails.root }/config/routes.rb"

  Dir["#{ Rails.root }/app/**/**.rb"].each { |f| load f }

end

# --- Instructions ---

# Sort the contents of this file into a Spork.prefork and a Spork.each_run

# block.

#

# The Spork.prefork block is run only once when the spork server is started.

# You typically want to place most of your (slow) initializer code in here, in

# particular, require'ing any 3rd-party gems that you don't normally modify

# during development.

#

# The Spork.each_run block is run each time you run your specs.  In case you

# need to load files that tend to change during development, require them here.

# With Rails, your application modules are loaded automatically, so sometimes

# this block can remain empty.

#

# Note: You can modify files loaded *from* the Spork.each_run block without

# restarting the spork server.  However, this file itself will not be reloaded,

# so if you change any of the code inside the each_run block, you still need to

# restart the server.  In general, if you have non-trivial code in this file,

# it's advisable to move it into a separate file so you can easily edit it

# without restarting spork.  (For example, with RSpec, you could move

# non-trivial code into a file spec/support/my_helper.rb, making sure that the

# spec/support/* files are require'd from inside the each_run block.)

#

# Any code that is left outside the two blocks will be run during preforking

# *and* during each_run -- that's probably not what you want.

#

# These instructions should self-destruct in 10 seconds.  If they don't, feel

# free to delete them.

end

三、告诉Rspec我们使用spork

 修改rspec配置文件(your_app/.rspec),告诉它我们用的是spork,添加代码:

--drb

  一般修改后完整的代码为:

--colour
--drb

四、此时RSpec与Spork即可完美结合了

启动Spork方法

 
Spork

执行RSpec方法

 
rspec spec

此时会发现RSpec执行时间明显缩短了。但如果通过执行”rake”来启动RSpec,仍然会很慢,这是因为rake启动时会加载工程环境,为所有tasks做准备。

五、添加Autotest自动执行测试

启动Autotest

 
autotest

Autotest就会自动启动,并监听文件变化,一旦改变,会立即执行改变部分相关的测试。
添加Autotest是最简单的,这是因为RSpec内部已经添加了对Autotest的一些支持,也是Rspec Rails推荐的做法。
简单也有一些问题,如Autotest自定义性不强,不够灵活。如果你追求自定义那就用Guard吧。

六、注意

此时当你修改app/models/person.rb时,可能并不会自动执行RSpec测试,这是因为Rails 的test环境默认对class做了缓存。
修改“config/environments/test.rb”

1
2
3
#config/environments/test.rb
#config.cache_classes = true #change this line to
config.cache_classes = false

七、参考文献

参考文献

  Factory Girl 使用方法的一些汇总:http://rubyer.me/blog/1460/

  Devise:https://github.com/plataformatec/devise

  RspecRails:https://github.com/rspec/rspec-rails

  Rails3-Devise-Rspec-cucumber 例子:https://github.com/RailsApps/rails3-devise-rspec-cucumber

  rspec+spork+autotest:http://rubyer.me/blog/1477/

原文地址:https://www.cnblogs.com/bendanchenzhicheng/p/2591648.html