我可以在一个文件中运行所有测试:

rake test TEST=path/to/test_file.rb

但是,如果我只想在该文件中运行一个测试,我该怎么做呢?

我正在寻找类似的功能:

rspec path/to/test_file.rb -l 25

当前回答

有两种方法:

“手动”运行测试(参见Andrew Grimm的回答)。 Hack Rake::TestTask目标以使用不同的测试加载器。

Rake::TestTask(来自Rake 0.8.7)理论上能够通过“TESTOPTS=blah-blah”命令行选项将额外的选项传递给MiniTest::Unit,例如:

% rake test TEST=test/test_foobar.rb TESTOPTS="--name test_foobar1 -v"

在实践中,选项——name(测试名称的过滤器)将不起作用,因为rake的内部结构。为了解决这个问题,你需要在Rakefile中写一个小猴子补丁:

# overriding the default rake tests loader
class Rake::TestTask
  def rake_loader
    'test/my-minitest-loader.rb'
  end
end

# our usual test terget 
Rake::TestTask.new {|i|
  i.test_files = FileList['test/test_*.rb']
  i.verbose = true 
}

这个补丁需要你创建一个文件test/my-minitest-loader.rb:

ARGV.each { |f|
  break if f =~ /^-/
  load f
}

要打印Minitest的所有可能选项,请键入

% ruby -r minitest/autorun -e '' -- --help

其他回答

不需要宝石: ruby - test test/lib/testRb——name /some_test/

来源:http://blog.arvidandersson.se/2012/03/28/minimalicous-testing-in-ruby-1-9

安装gem minest -focus并使用关键字focus on test/spec,如下所示,只运行特定的测试。

focus
def test
end


focus
it "test" do
end

这将不需要传递任何命令行参数。

你有没有试过:

ruby path/to/test_file.rb --name test_method_name

你可以用它来运行一个文件:

rake test TEST=test/path/to/file.rb

我也用过

ruby -I"lib:test" test/path/to/file.rb

为了更好的显示。

如果你在minitest中使用Turn gem,请确保使用Turn.config.pattern选项,因为Turn minitest运行器不尊重arg中的——name选项。