rake:test not running custom tests in subdirectory
Asked Answered
M

4

8

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?

Millard answered 6/3, 2013 at 20:6 Comment(0)
S
11

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.

Surat answered 6/3, 2013 at 21:0 Comment(1)
Re: "I don't know why they're not automatically getting picked up" -- I don't know if this is intentional, or just an asymmetry that developed as changes were made. So I added a comment on the pull request that added the test:all task.Millard
R
4

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
Runofthemill answered 25/4, 2014 at 13:52 Comment(0)
T
3

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.

Trevar answered 24/6, 2014 at 18:29 Comment(0)
B
3

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
Busywork answered 15/7, 2014 at 1:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.