2012-12-14 56 views
1

目前,我在我的測試套件(由Selenium Ruby Webdriver編寫)中一次性運行所有的selenium腳本,使用rake gem在「用Ruby啓動命令提示符「終端。如何在運行selenium ruby​​ webdriver腳本時輸出結果以從命令提示符輸出文件ruby window

要做到這一點,我必須創建一個名稱爲「rakefile.rb」的文件,內容如下,並在我的終端中調用「rake」:(我已經知道這個知識是基於我以前發佈的人員指南)。

task :default do 
    FileList['file*.rb'].each { |file| ruby file } 
end 

但是,如果在執行時,一個腳本了故障運行將被終止。

有人請幫助指導我如何修改「rakefile.rb」所以,如果有一個腳本失敗,則系統會忽略它,並繼續在我的測試套件運行下一個腳本?

另外,你可以請我建議一種方法來編寫所有的結果,當腳本運行到一個輸出文件?,或每個腳本的結果放在每個輸出文件和一個輸出文件將顯示腳本列表失敗。任何幫助表示讚賞。非常感謝。

回答

0

我在單元框架內運行所有測試。我自己使用測試單元,但你也可以使用rspec。這也使您能夠將斷言添加到代碼中,然後由單元框架報告。如果一個測試失敗或錯誤,您可以繼續進行下一個測試。

我的Rake文件的簡化版本,看起來像這樣

require 'rake/testtask' 

#this will run all tests in directory with no dependencies 
Rake::TestTask.new do |t| 
    t.libs << "test" 
    t.test_files = FileList['FAL*.rb'] 
    t.verbose = true 
end 

#or you could run individual files like this 

task :FAL001 do 
    ruby "FAL001.rb" 
end 

和每一個測試用例看起來像這樣

require "test-unit" 
gem "test-unit" 
require "selenium-webdriver" 

class FAL001 < Test::Unit::TestCase 
    def testFAL001 #methods that begin with test are automatically run 
    #selenium code goes here 
    assert_true(1 == 1) 
    end 
    def test002 
    #another test goes here 
    end 
end 
1

您可以使用beginrescue來捕獲測試腳本中的任何故障。

喜歡的東西

begin 
raise "Ruby test script failed" 
rescue 
puts "Error handled" 
end 

而你的情況會是這樣

task :default do 
    FileList['file*.rb'].each { |file| 
    begin 
     ruby file 
    rescue 
     puts "Test script failed because of #{$!}" 
    end 
    } 
end 

和寫入該會是這樣的

task :default do 
    $stdout = File.new('console.out', 'w') 
    $stdout.sync = true 
    FileList['*.rb'].each { |file| 
    begin 
     ruby file 
    rescue 
     puts "test script failed because of #{$!}" 
    end 
    } 
end 

,做什麼的文件重寫$ stdout來重定向控制檯輸出。

+0

大非常感謝你,底座上你的指導,只是增加一次日誌消息,現在我能在我的測試套件運行所有測試腳本,然後完成執行腳本後顯示失敗的測試名單中,「rakefile.rb」文件現在是:任務:默認DO $標準輸出= File.new ('console.out','w') $ stdout.sync = true \t FileList ['test * .rb']。each {| file | 開始 ruby​​文件 \t救援 \t看跌期權「下面的測試報告意外的行爲:」 看跌期權「#{文件} \ n」個 \t年底 } 結束 – battleship

+0

然而,能否請你指導我更多的方式來修改「rakefile.rb」能夠將執行每個失敗測試的內容導出到每個輸出文件?這意味着我期望執行每個腳本的內容將被寫入輸出文件而不是顯示在我的Ruby終端上(例如:當我運行測試腳本「test_GI-1.rb」時,然後執行此內容腳本將被寫入輸出文件「test_GI-1.out」而不是顯示在我的終端中。再次感謝你的偉大指南。 – battleship

+0

這樣的事'ruby example.rb> test.txt' – Amey

相關問題