Search code examples
redminefunctional-testingredmine-plugins

How to create functional tests for a Redmine 3 plugin


I have created few Redmine 3 plugins and now I want to code the tests to ensure a better stability if possible.

But now I can't create anything that works, I tried to look at other plugin's tests, and I can't reproduce things to work in any way possible.

You guys can give me a simple canvas example of how to proceed ?

I actually created redmine/plugins/redmine_timebank/test/functional/timebank_helper_test.rb containing this :

require 'redmine'
require 'versions_controller'

class TimebankHelperTest < ActionController::TestCase
  fixtures :projects,
           :issues,
           :issue_statuses,
           :versions,
           :trackers,
           :projects_trackers,
           :issue_categories,
           :time_entries

  def setup
    @controller = VersionsController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
    User.current = User.where(:admin => true).first
    @request.session[:user_id] = User.current.id
    @project = Project.find(1)
  end

  def test_timebank_table
    puts "AWWWWWWWWWWWWWWWWWWWW YEAHHHHHHHHHHHHHHHHHHHHH !"
  end

end

But when I execute the command :

RAILS_ENV=test bin/rake redmine:plugins:test --trace

I get those errors :

** Execute redmine:plugins:test:functionals
/usr/bin/ruby2.3 -I"lib:test"  "/usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb" "plugins/*/test/functional/**/*_test.rb" 
/usr/share/redmine/lib/redmine/core_ext/active_record.rb:18:in `<top (required)>': uninitialized constant ActiveModel (NameError)
    from /usr/share/redmine/lib/redmine/core_ext.rb:1:in `require'
    from /usr/share/redmine/lib/redmine/core_ext.rb:1:in `block in <top (required)>'
    from /usr/share/redmine/lib/redmine/core_ext.rb:1:in `each'
    from /usr/share/redmine/lib/redmine/core_ext.rb:1:in `<top (required)>'
    from /usr/share/redmine/lib/redmine.rb:18:in `require'
    from /usr/share/redmine/lib/redmine.rb:18:in `<top (required)>'
    from /usr/share/redmine/plugins/redmine_timebank/test/functional/timebank_helper_test.rb:1:in `require'
    from /usr/share/redmine/plugins/redmine_timebank/test/functional/timebank_helper_test.rb:1:in `<top (required)>'
    from /usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb:10:in `require'
    from /usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb:10:in `block (2 levels) in <main>'
    from /usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb:9:in `each'
    from /usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb:9:in `block in <main>'
    from /usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb:4:in `select'
    from /usr/lib/ruby/vendor_ruby/rake/rake_test_loader.rb:4:in `<main>'
rake aborted!

Seeing uninitialized constant ActiveModel (NameError) make me thinks that I need more depedencies, but I tough that I just need to require redmine to all import the rest.

Other plugins seems to import some thing like in their test/test_helper.rb :

require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper')

And this line into each testing file :

require File.dirname(__FILE__) + '/../test_helper'

Rake test says that none of those importing files could be found. Such as :

/usr/share/redmine/plugins/redmine_timebank/test/test_helper.rb:1:in `require': cannot load such file -- /usr/share/redmine/test/test_helper (LoadError)

Which stuff I do need to import into my functional test in order to get it works ?

Is this test/test_helper still exists ? Where can I find it ?

Thank you very much and have a great day !


Solution

  • I'll assume you have a plugin called redmine_foo that you want to test.

    What you've seen other plugins do is correct, include Redmine's test/test_helper by doing

    require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper')
    

    in redmine_foo/test/test_helper.rb.

    If your Redmine does not have test/test_helper.rb, this is a sure sign something is wrong / missing. I'm not sure running tests against a Redmine located in /usr/share is a good idea anyway, so first of all, get a proper checkout of Redmine from github or the official SVN on redmine.org, add your plugin to the plugins directory (i.e. in a folder named plugins/redmine_foo) and work with that.

    In your test case, require your plugin's test helper (the one-liner from above), i.e. in redmine_foo/test/functional/foos_controller_test.rb do:

    require File.expand_path('../../test_helper', __FILE__)
    
    class FoosControllerTest < ActionController::TestCase
      setup do
        # setup stuff specific to your application / test case
      end
    
      test 'should show hello world' do
        get :show
        assert_response :success
        assert_match /hello world/, response.body
      end
    end
    

    There is no need to initialize any of the @controller, @request, @response instance variables if your test case inherits from ActionController::TestCase.

    To get the most simple Redmine plugin possible, with above test running successfully, all you have to do is to create the controller, add a route and basic init.rb:

    redmine_foo/app/controllers/foos_controller:

    class FoosController < ApplicationController
      def show
        render text: 'hello world'
      end
    end
    

    redmine_foo/init.rb:

    Redmine::Plugin.register :redmine_foo do
      name 'Redmine Foo'
      version '1.0.0'
    end
    

    redmine_foo/config/routes.rb:

    resource :foo
    

    Run your tests from the Redmine base directory using NAME=redmine_foo bin/rake redmine:plugins:test