如何使用rake运行指定目录中的所有ruby文件

时间:2013-04-08 13:40:18

标签: ruby testing directory rake

我已经安装了Ruby 1.8.7,ci_reporter 1.8.4,测试单元2.5.4,rake 10.0.3。

我的testA.rb,testB.rb ... testZ.rb:

require 'includeA.rb'
require 'includeB.rb'
require 'includeC.rb'
require 'includeD.rb'

Begin of the code...
... End of the code

这是我的rakefile:

require 'rake/testtask'
require 'rubygems'
gem 'ci_reporter'
gem 'test-unit' 
require 'test/unit' 
require 'ci/reporter/rake/test_unit'  

task :default => [:test]

task :test do
  sh "ruby -I E:/pathToIncludesA/" " -I E:/pathToIncludesB/" " -I E:/pathToIncludesC/" " -I E:/pathToIncludesD/" " E:/pathToTests/testA.rb"
  sh "ruby -I E:/pathToIncludesA/" " -I E:/pathToIncludesB/" " -I E:/pathToIncludesC/" " -I E:/pathToIncludesD/" " E:/pathToTests/testB.rb"
  ...
  sh "ruby -I E:/pathToIncludesA/" " -I E:/pathToIncludesB/" " -I E:/pathToIncludesC/" " -I E:/pathToIncludesD/" " E:/pathToTests/testZ.rb"
end

我用:

启动测试执行
rake CI_REPORTER=reports test

一切正常,但现在我的“pathToTest”中会添加很多测试,现在我想在rakefile中运行所有测试但只有一个cmd。

我从Windows批处理cmd:for /r "E:\ExempleOfTests\" %%i in (*.rb) do ruby "%%i"

尝试此操作

它运行文件夹ExempleOfTests中的所有.rb文件。

所以现在我正在重构我的rakefile,尝试合并windows batch cmd。

这是我的新rakefile:

require 'rake/testtask'
require 'rubygems'
gem 'ci_reporter'
gem 'test-unit' 
require 'test/unit' 
require 'ci/reporter/rake/test_unit'  

task :default => [:test]

task :test do
  sh "for /r E:/pathToTests/ %%i in (*.rb) do ruby -I E:/pathToIncludesA/ -I E:/pathToIncludesB/ -I E:/pathToIncludesC/ -I E:/pathToIncludesD/ %%i"
end

但是当我使用“rake CI_REPORTS = reports test”启动时我的输出控制台:

unexpected %%i
rake aborted
commande failed with status (1) ...

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

您可以像这样运行:

find path/to/test/folder -name '*_test.rb' | xargs -n1 -I{} bundle exec ruby -Itest {}

然后根据需要将其包装到rake任务中。