Frage

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?

War es hilfreich?

Lösung

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.

Andere Tipps

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

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.

Or simply run rake test:all

If you want to run all tests by default, override test task:

namespace :test do
  task run: ['test:all']
end
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top