使用rake从特定文件夹运行rspec测试

use*_*236 7 rake rspec capybara

我希望使用rake在包含spec文件夹的文件夹中运行特定的测试.我的文件夹结构如下:

- tests
  -spec
   - folder_A
   - folder_B
- rakefile
Run Code Online (Sandbox Code Playgroud)

因此,例如,当部署某些代码时,我只想在folder_A中运行测试.我如何使用rake做到这一点?我的rakefile存在于我的tests文件夹中.我目前有这个命令:

RSpec::Core::RakeTask.new(:spec)
 task :default => :spec
Run Code Online (Sandbox Code Playgroud)

这会像您期望的那样在spec文件夹中运行所有测试.我已经尝试将rake文件移动到spec文件夹并将rake任务编辑为:

RSpec::Core::RakeTask.new(:folder_A)
 task :default => :folder_A
Run Code Online (Sandbox Code Playgroud)

但是,这给了我一条消息:"没有找到匹配./spec{,/ /*}/*_ spec.rb的示例"(请注意,文件夹A和BI中的子目录包含被测应用程序的不同区域)

我可以在同一个rakefile中有两个不同的rake任务,它们只能从folder_A运行测试吗?

任何帮助都会很棒!!

jwa*_*ack 6

为什么不使用rspec?

rspec spec/folder_A
Run Code Online (Sandbox Code Playgroud)

更新后的回应

:specRakefile指的是Rspec的rake任务,而不是文件夹.您可以通过传递rake-task doc页面上显示的块来向任务发送选项

在您的情况下,您可以使用pattern选项传递文件夹的glob .

RSpec::Core::RakeTask.new(:spec) do |t|
  t.pattern = 'spec/folder_A/*/_spec.rb'
end
Run Code Online (Sandbox Code Playgroud)

对于两个不同的rake任务,您需要在自己RakeTask的每个任务中实例化.所以你的整个Rakefile看起来像这样:

require 'rspec/core/rake_task'

task :folder_A do
  RSpec::Core::RakeTask.new(:spec) do |t|
    t.pattern = 'spec/folder_A/*/_spec.rb'
  end
  Rake::Task["spec"].execute
end

task :folder_B do
  RSpec::Core::RakeTask.new(:spec) do |t|
    t.pattern = 'spec/folder_B/*/_spec.rb'
  end
  Rake::Task["spec"].execute
end

task :default do
  RSpec::Core::RakeTask.new(:spec)
  Rake::Task["spec"].execute
end
Run Code Online (Sandbox Code Playgroud)

有关方法和其他选项的详细信息,请参阅RakeTask docpattern.