Escaping the hell of PHP testing using Ruby
January 1st, 2010
First, some background. Feel free to skip to the next header if you just want the details.
At work, we’re basically a 50/50 environment split between Ruby and PHP. In the last several months there’s been an effort to write more tests for the PHP side of things. We’ve been investigating various PHP testing tools and came to an inescapable conclusion: testing a web app using PHP is not pleasant (I’m being nice here).
I come from the point of view that if writing tests is unpleasant, no one will want to do it. The rest of the team is in agreement so, we had to come up with a different approach.
For some time I was honestly stumped trying to find that silver bullet solution. My answer came at RubyConf talking to Stephen Caudill and Corey Haines. Put simply, doing comprehensive integration testing was better than no testing and I should use Ruby to do that testing.
That was a revelation to me. I hadn’t considered just doing integration test but, during my indecision, we weren’t doing any meaningful testing. So, getting integration testing going and figuring the other layers later, sounded like a pragmatic approach.
Why a new library?
Yes, you can configure webrat to just hit a local webserver and that was my initial approach. This ended up requiring a fair bit of thought and configuration though. This was making testing painful again. I wanted to get to the point that people could checkout the code and not do much more than a handful of commands followed by rake spec.
No webserver or it’s configuration. No complicated webrat or rspec configuration. No test database management nightmares. No nothing. Just simple Rails-style testing.
gem install messiah
Messiah is my attempt at just that. It’s still a very young project and doesn’t hit all those goals yet.
It does however make for a decent first run experience even right now. Messiah itself primarily runs tests that hit php using itself so, it makes for a good demo:
You can see the actual spec file here and the php code it’s executing here.
The only real configuration, in spec_helper.rb, is pretty trivial:
“How’s it work?” or “I love Rack”
The most important part of messiah is a relatively simple idea. I wrote a Rack application that acts as a wrapper for CGI applications. So, it can actually test any CGI app not just PHP apps.
Besides the Rack app, it’s support code for configuring the environment for you at runtime. In the long run, that will be the more important part of messiah.
Where to next?
There are two big things I’m planning on adding in the next several days. First, a set of generators (thanks templater) for adding RSpec, Cucumber and others (preconfigured for messiah of course) to a codebase.
Second, I want to have first class database support. Coming up with a workable solution was surprisingly challenging. You need to consider connecting to the right database, keeping it in a sane state and easy way to work with known data.
My current approach based on database_cleaner, Dr. Nic’s Magic Models and Factory Girl seems to be working out nicely. This is not in the repo yet but, I do hope to push it soon.
How about everyone else out there? Any ideas?
January 1st, 2010 at 11:11 PM
Glad you're getting this out there! The architecture is intriguing (deceptively simple and tough to get your head around at the same time), you've done something pretty fantastic here.
Out of curiosity, what's the tie-in with the name? "Offering salvation to the damned" in the form of testing for PHP devs? ;)
January 2nd, 2010 at 10:53 AM
Thanks, glad you like it and thanks for getting my head going in the right direction.
As for the name, yeah, I thought of a dozens of one liners like that. One of the reasons I like the name.
January 19th, 2010 at 12:13 PM
Very interresting, we're just starting to use cucumber/webrat on a PHP application. I will follow Messiah very closely