9

Looked at this question already, and that more or less mirrors how I currently run my whole suite.

Additionally, I've setup the following rake task:

Rake::TestTask.new do |t|
  t.name = "spec:models"
  t.libs << "test"
  t.pattern = "spec/models/*_spec.rb"
end

But I notice when I run this using time rake spec:models, that it completes in about 2.36 seconds. If I run all the individual tests in that directory using ruby /path/to/spec.rb (all are isolated from ActiveRecord currently--no persistance yet, so super fast), their cumulative total user time is 2.36 seconds, yet I'm also noticing while each file takes 0.4 user seconds to execute from start to finish, the actual "test" time reported by MiniTest is much, much faster (such that my whole suite, after loading depencies, should be executing in less than 0.15-ish seconds, not 2.36 seconds).

Example (for one spec file):

Started

11/11:         100% |======================================| Time: 00:00:00

Finished in 0.02223s
11 tests, 15 assertions, 0 failures, 0 errors, 0 skips
ruby path/to/spec.rb  0.43s user 0.06s system 88% cpu 0.559 total

I suspect that Rake is reloading libraries between the execution of each test, and that's accounting for the extra time. Is there anyway I can verify this, or run my entire suite without using Rake?

Btw, when I say "user time" earlier, I'm referring to the first number outputted by prepending time to my ruby command to run a single test file, so time ruby /path/to/spec.rb = ruby path/to/spec.rb 0.43s user 0.06s system 88% cpu 0.559 total

Community
  • 1
  • 1
neezer
  • 19,720
  • 33
  • 121
  • 220

2 Answers2

8

Create a file spec.rb in the top directory of your project:

$:<<'spec'  # add to load path
files = Dir.glob('spec/**/*.rb') 
files.each{|file| require file.gsub(/^spec\/|.rb$/,'')}  

Run it:

ruby spec.rb

If this works for you, you can rename the spec.rb file as you like, or move it to a better location, or alter the load paths as you like, etc.

joelparkerhenderson
  • 34,808
  • 19
  • 98
  • 119
1

This will run any tests in the spec directory and subdirectories with a _spec.rb at the end of the filename:

ruby -Itest spec/**/*_spec.rb
Matthew Lehner
  • 3,967
  • 3
  • 26
  • 35
  • 1
    Hmm, that only seems to run one file (my final output with `ruby -Itest spec/models/*_spec.rb` shows 5 tests completed, whereas running `rake spec:models` above shows 77 tests completed). What's up with that? – neezer May 15 '12 at 16:11
  • I would like to know why Matthew's suggestion doesn't work for me either. – Benjamin Tan Wei Hao Aug 21 '12 at 04:46
  • 1
    I think you probably need quotes around spec/**/_spec.rb or your shell will expand it out, I suspect to just the one file, unless you're using a shell that understands that notation. – Ghoti Jul 22 '13 at 13:53
  • It looks like all these comments are not using the wildcard syntax for recursive subdirectories properly. The `**/*` needs to be there exactly the same. I've tested in both bash and zsh. – Matthew Lehner Oct 28 '14 at 17:47