2012-09-26 25 views
0

我正在開發一個轉換器,並使用rspec來檢查轉換的值是否正確。爲了檢查值,我需要加載轉換後的文件(用JSON編寫)。所以我準備了一個方法來加載spec_helper.rb中的轉換文件。我在每個具有:before鉤子的spec文件中調用這個方法(但這不是這個問題的實質)。用參數運行rspec多次

然後,我想爲多個轉換結果運行相同的rspec測試。這是一個正確的形象。

file_names = [ 'file1', 'file2', 'file3' ] # OR use Dir.glob to get some file paths 
file_names.each do |file| 
    all_rspec_tests(file) 
end 

請告訴我迭代某些文件名的最佳做法。我嘗試了幾種模式。

1)在Rakefile中,我們無法通過RakeTask傳遞變量。然後我使用ENV(環境變量),如下所示:how to pass command-line parameter to Rspec script? 它沒有預期的工作。特別是,RakeTast不會按順序運行(看起來像在其他線程上運行),因此每個測試都無法正確運行。

2)製作一個模塊,並像這樣擴展RSpec: Run the same spec multiple times w/ different filters 但它是使用JavaScript的模式。而且我不知道這種方法會成功。

回答

0

我通常使用:

system "rspec \"%s\" --format CustomFormatter --require environment" % file 

但是,你需要有兩個文件的文件夾 '規範' custom_formatter.rb和environment.rb中

custom_formatter.rb:

require "rspec/core/formatters/base_text_formatter" 
require 'yaml' 
class CustomFormatter < RSpec::Core::Formatters::BaseTextFormatter 
    def initialize(output) 
    super(output) 
    end 
    def example_started proxy 
    end 
    def example_passed proxy 
    p "\tPassed: " + proxy.description 
    end 
    def example_failed proxy 
    end 
    def example_group_started group 
    end 
    def example_group_finished group 
    end 
end 

環境。 rb:

require "rubygems" 
require "watir-webdriver" 
require "watir-webdriver/wait" 
require "test/unit" 
require 'system/report_html_template' 
require "system/logger" 
require "system/report" 
require "rspec" 

include Selenium 
include Watir 

RSpec.configure do |config| 
    config.fail_fast = true 
    config.before(:all) do 
    end 
    config.after(:all) do 
    end 
end