Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rake:test not running custom tests in subdirectory

I'm using Rails 4.0.0.beta1. I added two directories: app/services and test/services.

I also added this code, based on reading testing.rake of railties:

namespace :test do
  Rake::TestTask.new(services: "test:prepare") do |t|
    t.libs << "test"
    t.pattern = 'test/services/**/*_test.rb'
  end
end

I have found that rake test:services runs the tests in test/services; however, rake test does not run those tests. It looks like it should; here is the code:

Rake::TestTask.new(:all) do |t|
  t.libs << "test"
  t.pattern = "test/**/*_test.rb"
end

Did I overlook something?

like image 304
David J. Avatar asked Mar 06 '13 20:03

David J.


3 Answers

Add a line like this after your test task definition:

Rake::Task[:test].enhance { Rake::Task["test:services"].invoke }

I don't know why they're not automatically getting picked up, but this is the only solution I've found that works for Test::Unit.

I think if you were to run rake test:all it would run your additional tests, but rake test alone won't without the snippet above.

like image 116
Jim Stewart Avatar answered Oct 05 '22 08:10

Jim Stewart


For those using a more recent Rails version (4.1.0 in my case)

Use Rails::TestTask instead of Rake::TestTask and override run task:

namespace :test do
  task :run => ['test:units', 'test:functionals', 'test:generators', 'test:integration', 'test:services']
  Rails::TestTask.new(services: "test:prepare") do |t|
    t.pattern = 'test/services/**/*_test.rb'
  end
end
like image 25
ybart Avatar answered Oct 05 '22 08:10

ybart


Jim's solution works, however it ends up running the extra test suite as a separate task and not as part of the whole (at least using Rails 4.1 it does). So test stats are run twice rather than aggregated. I don't feel this is the desired behaviour here.

This is how I ended up solving this (using Rails 4.1.1)

# Add additional test suite definitions to the default test task here

namespace :test do
  Rails::TestTask.new(extras: "test:prepare") do |t|
    t.pattern = 'test/extras/**/*_test.rb'
  end
end

Rake::Task[:test].enhance ['test:extras']

This results in exactly expected behaviour by simply including the new test:extras task in the set of tasks executed by rake test and of course the default rake. You can use this approach to add any number of new test suites this way.

If you are using Rails 3 I believe just changing to Rake::TestTask will work for you.

like image 27
Chris Nicola Avatar answered Oct 05 '22 09:10

Chris Nicola