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_generationYou 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_chickenAfterwards
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.

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?
“in vacuo”? If you choose to write in English, write in English: “in isolation”.
http://www.bartelby.com/61/22/I0212200.html
It is english.
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
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).
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 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 getAny ideas?
Hum. Nevermind, I suppose. After installing/uninstalling Rails gems for a bit, it seems to work now.
Running into another issue:
Pretty minimal test case at http://pastie.textmate.org/90972.
>In PTK’s
route.rb, I domap.resources :foos.If I make an integration test that does
get "/foos", though,assert_response :successfails due to a0response code.However, the
controllerattribute of the test session is set correctly, and if I explictly triggercontroller.index, the response is successful.Thankful for any help/hints.