Josiah Kiehl
Josiah Kiehl

Reputation: 3633

Is it possible to run a single test in MiniTest?

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

Upvotes: 210

Views: 82620

Answers (17)

Artur INTECH
Artur INTECH

Reputation: 7256

If you're using Minitest::TestTask and no Rails, you can also pass the name of a test using the N environment variable:

rake test N=test_some_test_name

Upvotes: 0

mechnicov
mechnicov

Reputation: 15248

Rails 7.1 introduce new syntax for specific lines for tests

Now you can run not only specific tests by single line

bin/rails test test/models/user_test.rb:10 # by line 10

but also by lines range

bin/rails test test/models/user_test.rb:10-20 # by lines 10..20

or even by few ranges

bin/rails test test/models/user_test.rb:10-20:25-30 # by lines 10..20 and 25..30

Upvotes: 3

Nivedha Senthil
Nivedha Senthil

Reputation: 957

Install gem minitest-focus and use the keyword focus on test/spec like below to run only the specific test.

focus
def test
end


focus
it "test" do
end

This would not need any command line argument to be passed.

Upvotes: 1

Elliot Winkler
Elliot Winkler

Reputation: 2344

I'm looking for similar functionality to rspec path/to/file.rb -l 25

With Nick Quaranto's "m" gem, you can say:

m spec/my_spec.rb:25

Upvotes: 46

Vasanth Saminathan
Vasanth Saminathan

Reputation: 585

I am in Rails Version 4.2.11.3 and Ruby Version 2.4.7p357

Below one worked for me.

ruby -Itest <relative_minitest_file_path> --name /<test_name>/

Upvotes: 3

Derek Hill
Derek Hill

Reputation: 6454

If you are using MiniTest with Rails 5+ the best way to run all tests in a single file is:

bin/rails test path/to/test_file.rb

And for a single test (e.g. on line 25):

bin/rails test path/to/test_file.rb:25

See http://guides.rubyonrails.org/testing.html#the-rails-test-runner

Upvotes: 43

Rimian
Rimian

Reputation: 38418

You can pass --name to run a test by its name or a number within its name:

-n, --name PATTERN               Filter run on /regexp/ or string.

e.g.:

$ ruby spec/stories/foo_spec.rb --name 3

FAIL (0:00:00.022) test_0003_has foo
Expected: "foo"
Actual: nil

This flag is documented in Minitest’s README.

Upvotes: 13

user4636030
user4636030

Reputation:

Following will work

def test_abc
end

test "hello world"
end

This can run by

bundle exec ruby -I test path/to/test -n test_abc

bundle exec ruby -I test path/to/test -n test_hello_word

Upvotes: 1

sloneorzeszki
sloneorzeszki

Reputation: 1524

I use ruby /path/to/test -n /distinguishable word/

Edit: -n is a shorthand for --name. distinguishable word can be any string you put in the test description, I usually use some random word that I know won't be present in other tests' descriptions.

Upvotes: 2

wteuber
wteuber

Reputation: 1238

I'm looking for similar functionality to:

rspec path/to/test_file.rb -l 25

There is a gem that does exactly that: minitest-line.

gem install minitest-line
ruby test/my_file -l 5

from https://github.com/judofyr/minitest-line#minitest-line

Upvotes: 1

Andrew Grimm
Andrew Grimm

Reputation: 81450

Have you tried:

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

Upvotes: 176

tuhaj
tuhaj

Reputation: 537

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.

Upvotes: 25

randomor
randomor

Reputation: 5663

No gem required: ruby -Itest test/lib/test.rb --name /some_test/

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

Upvotes: 63

Laknath
Laknath

Reputation: 528

If you are using Turn gem with minitest, just make sure to use Turn.config.pattern option since Turn Minitest runner doesn't respect --name option in ARGs.

Upvotes: 1

jduan
jduan

Reputation: 2854

The command should be:

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

Upvotes: 244

fguillen
fguillen

Reputation: 38772

This is one of the things that bother me about the string name definition in tests.

When you have:

def test_my_test
end

you always know how your test is named so you can execute it like this:

ruby my_test -n test_my_test

But when you have something like:

it "my test" do
end

you are never sure how this test is really named internally so you can not use the -n option just directly.

To know how this test is named internally you only have an option: execute the whole file to try to figure out looking in the logs.

My workaround is (temporally) add something to the test name very unique like:

it "my test xxx" do
end

and then use the RegEx version of the '-n' parameter like:

ruby my_test.rb -n /xxx/

Upvotes: 53

Alexander Gromnitsky
Alexander Gromnitsky

Reputation: 3029

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

Upvotes: 12

Related Questions