2012-12-14 57 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

相关问题