Search for the best Integration testing

When you make a Rails application, there are some things unit and functional tests just can’t test. Like if something actually works in practice, or if caching is working the way it should. If you don’t test these, you can get run into a lot of problems, especially if that code is an application installed in multiple places. And it can be a pain to make sure code you add works as intended before a release (if could take you hours to test). So how do you get around this?

Well one way is using a service called Selenium RC, combined with Test::Unit style scripts. Selenium opens a browser, and runs a series of actions you define in your tests. And this can be quite handy for things like JS/AJAX forms. But what if you don’t need that? The whole process of loading a browser just to test the login is working, or that a cache is removed when something is updated doesn’t need the features of a browser. So what do you use?

Well myself and my work colleagues recently started using an integration system called Webrat, and it’s worked out really well. Combined with a custom test helper, which provides various custom methods, and alongside gems like shoulda and factory-girl, we have written a series of integration tests that are both small in size, and fastest to run (currently 5.5mins) compared to similar tests with Selenium or by testing this stuff by hand. And what’s more, once you have the gems in place, it’s a simple rake test:integration call to run them. No browser configuration or starting up the Selenium RC server. There is however Selenium built into the Webrat gem, so if you need to do JavaScript testing, it’s not hard to.

Writing tests using Webrat has made writing tests fun (not tiresome) and I look forward to writing more of them in the future. I even wrote a neat (in my opinion anyway) method_missing definition that allows a bunch of different method calls to create users with permissions, or to setup new items, and some neat application specific item creation methods.

Our entire integration test setup is contained within the test/integration folder, so feel free to check it out, particularly the integration_test_helper.rb and caching_test.rb (which demonstrates most of the code).

I’m interested to hear how others have approached the problem though. How do you test you caching? How do you test AJAX functionality? Post in the comments.

Notes

  1. k776 posted this