Back in September 2010, I was still actively working on my MooseX::Declare inspired and Devel::Declare powered layer over Test::Class; Test::Class::Sugar and was feeling some pain as I tried to extend it. I was struggling to combine the xUnit style behaviour of Test::Class with Moose's expressive tools for declaring attributes and generally writing object oriented code. I don't want to go into the details (because I've mercifully forgotten them), but I couldn't see a way to make Test::Class's innards compatible with Moose's innards.
So, when a message from rjbs appeared in my IRC client pointing me at his Moosey testing framework, Test::Routine, it appeared at the most opportune moment possible. You may have noticed that Test::Class::Sugar hasn't been updated in a long time - that's because I've switched all my testing to Test::Routine.
Wrapping my head around Test::Routine
When I first started playing with Test::Routine there was one thing I found rather frustrating. In xUnit style testing, test isolation (a very good thing) is isolated because each test is run with a fresh instance of the test class. It's much harder to have your tests getting tangled when you do this. However, consider something like:
If Test::Routine worked like xUnit, that big fixture would make the test suite slow. The usual way fixture building is done in xUnit style classes is that the test suite calls
setup before it runs a test and
teardown after. With Test::Routine I found that I could dispense with an explicit setup phase by declaring my attributes with the
lazy_build flag and using builders to set up my attributes and an
after hook to clear up any attributes that needed resetting. So I might do:
Now, this is all very well, and it certainly works, but it's not very declarative. What would be rather handy, I said to Rik, would be if I could write something like:
and let the
after run_test part be handled automatically by Test::Routine. Rik agreed that that would be cool, but that he wasn't in a hurry to get it written. Nor was I. And there we left it.
It's Advent all over again
Spool forward to now. Rik's written about Test::Routine in his 2010 advent calendar and the world knows about it. But in 2011, he's not just plotting an Advent calendar, he's also the recently crowned (on Halloween, natch) Perl Pumpking and he can't just dash off 25 articles about cool perl modules like he has done for the past couple of years. Once again, a chat window opens up in my IRC client.
"Hey, Piers, would you like to write an article for this year's Advent calendar?"
Well... there's an invitation you take very seriously.
Not very many lines of code later
Do you know how hard Moose rocks? Really? In one evening's hacking, most of which was spent reading documentation and a couple of different MooseX modules to see how it was done, I can now write:
And it all Just Works. The code isn't on the main line of Test::Routine. The documentation is laughably non-existent and I'm not entirely sure that the implementation is properly robust, but it works. If you're interested in having a play, you can pull the latest version from my Test::Routine github fork. Enjoy yourselves.