Is it possible to run a single test in MiniTest?

2020-02-07 13:59发布

I can run all tests in a single file with:

rake test TEST=path/to/test_file.rb

However, if I want to run just one test in that file, how would I do it?

I'm looking for similar functionality to:

rspec path/to/test_file.rb -l 25

14条回答
Juvenile、少年°
2楼-- · 2020-02-07 15:02

There are 2 ways to do it:

  1. Run tests 'manually' (see Andrew Grimm's answer).
  2. Hack Rake::TestTask target to use a different tests loader.

Rake::TestTask (from rake 0.8.7) theoretically is able to pass additional options to MiniTest::Unit with a "TESTOPTS=blah-blah" command line option, for example:

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

In practice, the option --name (a filter for test names) won't work, due to rake internals. To fix that you'll need to write a small monkey patch in your 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 
}

This patch requires you to create a file test/my-minitest-loader.rb:

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

To print all possible options for Minitest, type

% ruby -r minitest/autorun -e '' -- --help
查看更多
聊天终结者
3楼-- · 2020-02-07 15:04

You can use this to run a single file:

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

I also used

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

for better display.

查看更多
登录 后发表回答