How to run multiple Rails unit tests at once
Asked Answered
P

4

18

I often run the various test groups like:

rake test:units
rake test:functionals

I also like to run individual test files or individual tests:

ruby -Itest test/unit/file_test.rb
ruby -Itest test/unit/file_test.rb -n '/some context Im working on/'

There's also:

rake test TEST=test/unit/file_test.rb

And I've even created custom groupings in my Rakefile:

  Rake::TestTask.new(:ps3) do |t|
    t.libs << 'test'
    t.verbose = true
    t.test_files = FileList["test/unit/**/ps3_*_test.rb", "test/functional/services/ps3/*_test.rb"]
  end

What I haven't figured out yet is how to run multiple ad-hoc tests at the command line. In other words, how can I inject test_files into the rake task. Something like:

rake test TEST=test/unit/file_test.rb,test/functional/files_controller_test.rb

Then I could run a shell function taking arbitrary parameters and run the fast ruby -Itest single test, or a rake task if there's more than one file.

Passible answered 11/7, 2011 at 21:37 Comment(0)
P
12

I ended up hacking this into my RakeFile myself like so:

Rake::TestTask.new(:fast) do |t|
  files = if ENV['TEST_FILES']
    ENV['TEST_FILES'].split(',')
  else
    FileList["test/unit/**/*_test.rb", "test/functional/**/*_test.rb", "test/integration/**/*_test.rb"]
  end

  t.libs << 'test'
  t.verbose = true
  t.test_files = files
end
Rake::Task['test:fast'].comment = "Runs unit/functional/integration tests (or a list of files in TEST_FILES) in one block"

Then I whipped up this bash function that allows you to call rt with an arbitrary list of test files. If there's just one file it runs it as ruby directly (this saves 8 seconds for my 50k loc app), otherwise it runs the rake task.

function rt {
  if [ $# -le 1 ] ; then
    ruby -Itest $1
  else
    test_files = ""
    while [ "$1" != "" ]; do
      if [ "$test_files" == "" ]; then
        test_files=$1
      else
        test_files="$test_files,$1"
      fi
      shift
    done
    rake test:fast TEST_FILES=$test_files
  fi
}
Passible answered 11/7, 2011 at 22:20 Comment(1)
thanks dasil003 for pointing the Rake::TestTask class, that's exactly what I need.Outman
N
18
bundle exec ruby -I.:test -e "ARGV.each{|f| require f}" file1 file1

or:

find test -name '*_test.rb' | xargs -t bundle exec ruby -I.:test -e "ARGV.each{|f| require f}"
Nephew answered 16/3, 2013 at 23:28 Comment(1)
Also you may want to replace ruby with ruby -w in both those examples.Nephew
P
12

I ended up hacking this into my RakeFile myself like so:

Rake::TestTask.new(:fast) do |t|
  files = if ENV['TEST_FILES']
    ENV['TEST_FILES'].split(',')
  else
    FileList["test/unit/**/*_test.rb", "test/functional/**/*_test.rb", "test/integration/**/*_test.rb"]
  end

  t.libs << 'test'
  t.verbose = true
  t.test_files = files
end
Rake::Task['test:fast'].comment = "Runs unit/functional/integration tests (or a list of files in TEST_FILES) in one block"

Then I whipped up this bash function that allows you to call rt with an arbitrary list of test files. If there's just one file it runs it as ruby directly (this saves 8 seconds for my 50k loc app), otherwise it runs the rake task.

function rt {
  if [ $# -le 1 ] ; then
    ruby -Itest $1
  else
    test_files = ""
    while [ "$1" != "" ]; do
      if [ "$test_files" == "" ]; then
        test_files=$1
      else
        test_files="$test_files,$1"
      fi
      shift
    done
    rake test:fast TEST_FILES=$test_files
  fi
}
Passible answered 11/7, 2011 at 22:20 Comment(1)
thanks dasil003 for pointing the Rake::TestTask class, that's exactly what I need.Outman
C
2

There's a parallel_tests gem that will let you run multiple tests in parallel. Once you have it in your Gemfile, you can just run as ...

bundle exec parallel_test integration/test_*.rb

For me I setup a short rake task to run only the tests I want.

Cicatrix answered 13/2, 2012 at 21:36 Comment(3)
bundle exec -n0 parallel_test integration/test_*.rb to run them without parallelisationPerice
it is not -n0 it is -n1Emilia
Super useful way if you are already using this gem to run tests in parallel some times (in CI, for instance) but still go to regular rake test in development to run some tests just as the matter of habbit, as in my case.Complexioned
L
0

Bash Script

RUBY_MULTI_TEST="/tmp/ruby_multi_test.rb"

function suitup-multi-test-prepare {
  sudo rm $RUBY_MULTI_TEST 2> /dev/null
}

function suitup-multi-test-add {
  WORK_FOLDER=`pwd`
  echo "require '$WORK_FOLDER/$1' " >> $RUBY_MULTI_TEST
}

function suitup-multi-test-status {
  cat $RUBY_MULTI_TEST 2> /dev/null
}

function suitup-multi-test-run {
  suitup-multi-test-status
  ruby -I test/ $RUBY_MULTI_TEST
}

ery@tkpad:rails_app:$ suitup-multi-test-prepare
ery@tkpad:rails_app:$ suitup-multi-test-add test/functional/day_reports_controller_test.rb
ery@tkpad:rails_app:$ suitup-multi-test-add test/functional/month_reports_controller_test.rb
ery@tkpad:rails_app:$ suitup-multi-test-run
Lail answered 31/7, 2013 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.