Plugin Test Kit: Initial Release

Posted by Mathew Abonyi Wed, 09 Aug 2006 17:00:00 GMT

PluginTestKit (a plugin test development kit)

Plugins can and, I think, ought to be just like the main application part of your code. Just like your application, it ought to be self-sufficient. You should be able to run tests outside of a specific environment, allowing your plugin to be seen in its proper light. If it doesn’t work in vacuo, it is prone to design flaws because it is relying on the application from which it came. This is at least the conclusion I came to in writing my own plugins and wanting to develop them outside the application proper. Plugins by their nature should be usable in other current or future application with relative ease. Sounds too utopian? Maybe.

Introduction

Seeing as there is no standardised way to test a plugin using a Rails environment beyond directly including it within a Rails application, it is rather difficult to test plugins, whether they are in vacuo or in the vendor/plugins directory of an application. Plugin Test Kit provides the most common environment requirements or piece of environments you need to test your plugin.

There is a generator called plugin_test_kit which does all the library and template copying that you need from the plugin. Whenever you make a new plugin, just call the generator on it and away you go. To upgrade, just act as if you are installing for the first time – be sure to back up any changes you have – and overwrite the library files and whichever other files you wish to refresh. The generator can be called both from ./script/generate in a Rails app or from its own ./generate within the plugin’s root directory.

This plugin will of course evolve as my own needs evolve, but if anyone has a particular assertion, requirement or other feature they would like to see, just ask. Also, any design changes which you’d like to suggest, I’m all ears. Most of the design I came up with while writing the README, so it is bound to have flaws.

Installation

You need to install Plugin Test Kit in your plugins directory. This can be your repository of plugins on which you are working, or the vendor/plugins directory of your Rails application. However, you must use a Rails application to install it first, just like every other plugin. After that point, PTK can start doing its thing.

  $ ./script/plugin source http://mabs29.googlecode.com/svn/trunk/plugins
  $ ./script/plugin install plugin_test_kit

  If you want to move it to a repository of your plugins, outside a Rails app
  $ mv vendor/plugins/plugin_test_kit ../repository/plugins/

Now you can start using it.

Usage

You have two options for generating the files from the plugin_test_kit library. The first is using the in-built Rails ./script/generate.

  user:~/ $ cd rails-app
  user:rails-app $ ./script/generate plugin_test_kit wild_chicken
        create  config
        create  fixtures
        create  lib
        create  lib/ptk
        ...
        readme  after_generation

You also have the option of using the generate script provided in plugin_test_kit. It has been removed of its dependency on a Rails application, allowing you to call it from a repository of plugins divorced from the root of a Rails application.

  user:~/ $ cd repos/plugins/plugin_test_kit
  user:plugin_test_kit $ ./generate plugin_test_kit wild_chicken

Afterwards

Once you have installed the libraries into your own plugin’s test directory using the generator, you will be presented with additional customisations which you can make. The templates contain further instructions on how to customise each one. You may now treat your test environment pretty much as you would a normal Rails application test.

Enjoy!

Posted in  | 8 comments | 2 trackbacks

Comments

  1. topfunky said about 2 hours later:

    I modified some of Scott Barron’s code and wrote plugin_test which allows you to write unit tests against your plugin (using a local sqlite database and plugin-specific fixtures).

    http://topfunky.net/svn/plugins/plugin_test/

    I’m using it to test most of my plugins. Maybe we could merge these and have a super-duper plugin test system?

  2. English said 1 day later:

    “in vacuo”? If you choose to write in English, write in English: “in isolation”.

  3. Englisher said 2 days later:

    http://www.bartelby.com/61/22/I0212200.html

    It is english.

  4. Tom said 17 days later:

    I’ve just started using this framework. I needed to create an empty test/config/environments/test.rb in my plugin to get my empty tests to run.

    My setup.framework is set to :action_controller and I’m running on Windows

    Next task is to write some tests

  5. Mathew Abonyi said 20 days later:

    Tom, are you at any point requiring other gems from Rails? If the Rails::Initializer fires, it will look for environment files. Without the :rails framework, you will be required to set up an environment just as in a normal rails app (like test/config/environment/test.rb). The :rails framework has special hacks that prevent that from happening.

    If you want to bypass all of that and take a little performance hit, just use the :rails framework. It should be pretty infallible (for Rails 1.1, at least).

  6. Henrik N said about 1 year later:

    Seems this is exactly what I need, but I can’t get it working.

    I generated (using the edge Rails generator) a new app. I then installed PTK, copied over my own plugin and generated the PTK files into it.

    In my plugin’s test dir, I tried running a file (“simple_test.rb”) containing only
    
      require File.join(File.dirname(__FILE__), 'ptk_helper')
    
    First, I got an error to the effect of “ActiveResource missing”. After installing that gem (does PTK only work with Gem Rails btw, not vendor/rails?), I now get
    
      Gem::Exception: can't activate activesupport (= 1.4.2), already activated activesupport-1.4.2.7357]
    
        method activate    in rubygems.rb at line 246
        ⋮
        method initialize_frameworks    in ptk.rb at line 107
        method process    in ptk.rb at line 97
        method send    in ptk.rb at line 93
        method run    in ptk.rb at line 93
        at top level    in ptk_helper.rb at line 4
        method require    in simple_test.rb at line 5
        at top level    in simple_test.rb at line 5
    

    Any ideas?

  7. Henrik N said about 1 year later:

    Hum. Nevermind, I suppose. After installing/uninstalling Rails gems for a bit, it seems to work now.

  8. Henrik N said about 1 year later:

    Running into another issue:

    Pretty minimal test case at http://pastie.textmate.org/90972.

    >

    In PTK’s route.rb, I do map.resources :foos.

    If I make an integration test that does get "/foos", though, assert_response :success fails due to a 0 response code.

    However, the controller attribute of the test session is set correctly, and if I explictly trigger controller.index, the response is successful.

    Thankful for any help/hints.

Trackbacks

Use the following link to trackback from your own site:
http://www.mathewabonyi.com/articles/trackback/18

  1. From Paris Hilton Sex Tape Video 2 - Paris Hilton Exposed *HOT*
    Where to buy latest Paris Hilton sex tape on internet
    Recently leaked footage of the new Paris Hilton sex tape
  2. From My Shiny Weblog!
    Action cache plugin updated to support UTF
    Thanks to a comment from Hui to "this post":http://blog.craz8.com/articles/2006/08/11/action-cache-plugin-bug-fix I've updated the Action Cache plugin as he suggested. At this time, I've also used the "Plugin Test Kit":http://www.mathewabonyi.com/...

(leave url/email »)

   Comment Markup Help Preview comment