How to run a full MiniTest suite without Rake?

2019-04-23 02:08发布

问题:

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

回答1:

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.



回答2:

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