Plugin tests not running

I want to build a plugin by extracting some code from an existing app. I figured the first thing I should do is get comfortable working on a plugin, so I generated a Rails 2.3.3 app, and from inside that, I generated a plugin. Before doing anything else, I thought I would try to run the plugin’s generated test. The file loads fine - but the test isn’t run. I can prove that by adding a couple of “puts” statements to the test.

I can’t figure out why the file is found - but the test does not run. What I started with is below. Grasping at straws, I also tried current ActiveSupport out of the picture by having the test inherit directly from Test::Unit::TestCase and using the older “def test_truth; assert true; end” syntax. But that didn’t change anything. There are no errors; the tests just get silently ignored. How can this be?

The steps I took:

$ rails generate bar
$ cd bar
$ script/generate plugin MyPlugin
$ cd vendor/plugin/my_plugin
$ rake test

RAILS version 2.3.3
Ruby 1.8.6 on MacOs X

vendor/plugins/my_plugin/test/my_plugin_test.rb

require ‘test_helper’

class MyPluginTest < ActiveSupport::TestCase
puts “In MyPluginTest file”

Replace this with your real tests.

test “the truth” do
puts “In truth test”
assert true
end
end

vendor/plugins/my_plugin/test/test_helper.rb

puts “vendor/plugins/my_plugin/test/test_helper is being read”
require ‘rubygems’
require ‘active_support’
require ‘active_support/test_case’

$ rake test
(in /Users/xxx/rails/bar/vendor/plugins/my_plugin)
/usr/local/bin/ruby -I"lib:lib:test" “/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb” “test/my_plugin_test.rb”
vendor/plugins/my_plugin/test/test_helper is being read
In MyPluginTest file

Hello Cynthia:

What you did actually makes perfect sense, but after rails got a
wrapper ActiveSupport::TestCase for Test:Unit there seems to be a bit
of a conflict with the testing tasks.

Option 1: If you want to load the Rails environment then just revert
back to RAILS_ROOT and run:

rake test:plugins

for details on this rake task:

rake -D test:plugins

Option 2: If you want to test your plugin in isolation, then you can
modify your plugin test as such:

require 'test/unit' # add to test_help, then you could remove the
active_support

class FooTest < Test::Unit::TestCase # replacing class FooTest <
ActiveSupport::TestCase

Then in your plugin directory rake test should work.

Option 3: I guess you could also load the rails environment in your
test_helper as well

plugin_test_dir = File.dirname(__FILE__)
require File.join(plugin_test_dir, '../../../../config/
environment')

require 'test_help'

Someone else might want to chime in if I have missed something.

HTH,
Nicholas

#vendor/plugins/my_plugin/test/test_helper.rb should contain this
ENV["RAILS_ENV"] = "test"
ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
require 'test/unit'
require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/
environment.rb'))
require 'test_help'

See http://guides.rubyonrails.org/plugins.html#tests for more
information.

This ticket on Lighthouse seems relevant - you may want to try the
solution there:

https://rails.lighthouseapp.com/projects/8994/tickets/1878

--Matt Jones

Thanks. Adding require 'test/unit' in my test_helper makes the tests
actually run. OK now to actually add some functionality!