Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adding extra test paths to Rails 5 `rails test` command

In Rails 4.x, you could add extra test paths to rake test by overriding the Rake task like so:

Rake::Task['test:run'].clear

namespace :test do
  Rails::TestTask.new(:run) do |t|
    paths = ['test/**/*_test.rb']
    paths << 'engines/foo_engine/test/**/*_test.rb'
    t.test_files = FileList.new *paths
  end
end

In Rails 5, this no longer works. One approach is to make a Minitest plugin that will load the extra paths for you. To make a Minitest plugin, you need to add a file in $LOAD_PATH that matches the minitest/*_plugin.rb glob.

For example, we could add to our Rails project lib/minitest/extra_tests_plugin.rb. This creates a Minitest plugin named extra_tests. In the file, you would have something like this:

module Minitest
  def self.plugin_extra_tests_init(*)
    # This is defined by railties/lib/rails/test_unit/minitest_plugin.rb
    if opts[:patterns].empty?
      ::Rails::TestRequirer.require_files(['engines/foo_engine/test'])
    end
  end
end

Now running rails test will also pick up all test files matching the engines/foo_engine/test/**/*_test.rb glob.

In Rails 5.1.3, Rails' Minitest integration was reworked again. The plugin will need to be updated like this:

module Minitest
  def self.plugin_extra_tests_init(*)
    if Rails::TestUnit::Runner.send(:extract_filters, ARGV).empty?
      tests = Rake::FileList['engines/foo_engine/test/**/*_test.rb']
      tests.to_a.each { |path| require File.expand_path(path) }
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.