I won’t try to convince you to test your project. If you are a techie, you’re probably tired to eat: TDD, CI… in each conference, slide or blog post (even here). Instead here’s a tip to mix my favorite tools together: behat & atoum.
Quick introduction to atoum and behat
To skip if you already know the toolsAtoum is a modern “unit test” framework write in PHP. I already expressed my opinion about it
Behat is a “Behavior Driven Development” aka “BDD” framework write in PHP by @everzet. Initially based on cucumber in Ruby, the tool has a great evolution and some Ruby nerds consider it as good as cucumber (and sometimes, even better. Sorry I don’t have the link to the tweet). In short, it’s IMHO, the best tool to do this job for PHP. Behat also uses the gherkin syntax and allows your tests to be readable by anyone involved in your project.
Atoum asserter into your Behat context
Behat is a modern tool (i.e: loosely coupled) and the best thing: it doesn’t try to reinvent the wheel. You’re free to pick any “asserter” you want! If you enjoy the atoum fluent syntax, you probably want to import the same goodness inside Behat. It’s easy and you just have to follow the classic 3-steps: “Install-Configure-Enjoy”.
Install
First, install Behat and atoum: pear, git clone… If you’re lazy or smart, you probably rely on composer:
"require-dev": {
"behat/behat": "2.4@stable",
"mageekguy/atoum": "dev-master",
}
Configure
Then, inside your Behat FeatureContext class, use the atoum’s asserter namespace and don’t forget to (auto)load everything.
This is really simple, but some people were mixing wrong behat/atoum concepts. For example: it’s a really bad idea to use the atoum test class inside Behat. I don’t know what kind of “hope” you could have doing that, but I can predict you a nice headache.
Enjoy
Now you will be able to code your Behat steps using the atoum style:
It’s been a while since I posted here, my position at Plemi and the project Retentio required much time this last months. We’ve been playing with a lot of tools and I wishes I could have more time to share all the goodness of our experiences.
In the path of my previous post about the continuous integration I’d like to talk about atoum, a new framework for PHP unit testing.
In the PHP QA world, Sebastian Bergman is king. He’s the developer of PHPUnit, the powerful and standard framework for unit testing in PHP.
Yet a new challenger just came in the QA Arena: Atoum from Frédéric Hardy. I like to have choices, so I strongly support alternative projects when it’s reasonable, this means when the “philosophy” behind the projects aren’t the same and it’s not a waste of efforts justified by ego.
I tested atoum in 3 projects this last 3 months, One word: awesome. This tool embraces KISS principles. Is PHPUnit complex ? Not really, it just looks complex. Both frameworks are really great: I like the clover-html output of PHPUnit, I like the fluent interface of the atoum’s asserters, etc.
But, I would advise beginners and new comers into the TDD world to start with atoum. The learning curve is unbeatable, the tests are much more “readable” (IMHO) and finally, writing unit tests with atoum is fun.
Here’s a little comparison of asserters usage for example:
This is a major victory for the “underrated unit testing practice”, because when something is fun, people want to play. Atoum does not provide much documentation at the moment, but it’s easy to find what you need in the asserters directory.
As PHPUnit, Atoum provides standard reports:
- jUnit reports which can be displayed in many continuous integration plateform
- Clover coverage (will be released soon) thanks to Julien Bianchi
So it will perfectly fit into your continuous integration stack, here at Plemi we already switched. (We won’t migrate our projects on PHPUnit, since there is no point to do a such thing.)
This post doesn’t mentions all the features of PHPUnit and Atoum, mock, test isolation, but right now, with theses two cool tools, you have no excuse to don’t test your PHP code. Try it, love it, atoum is the missing key for the TDD gate.