Code Free Acceptance and Integration Testing

In Ruby on Rails this is made possible through Cucumber, in PHP it’s possible using Behat + MinkExtension.

Behat is the integration testing framework, and Mink is the magic for easy code-free web-based acceptance test.

Wouldn’t it be nice if you could describe how the application was supposed to work in plain English.. and all the testing happened automagically? Well here you go:

Run Behat with the MinkExtension installed and it performs all the magic: simulates navigating to the /wiki/Main-Page, entering text into a search field, pressing a button, and then scanning the page for a piece of text. Sounds awesome? Cool let’s set it up.

Step 1: Install Behat and MinkExtension

If you aren’t familiar with composer, follow the instructions from packagist. It will change your life. I’ll give you a minute and wait here until you have composer installed.

Done? Great!

Create  a file called composer.json add the following:

Run composer install   and give it a minute to sort out and install dependencies. It’s like apt-get or gem but for php modules! Yey!

This will install of the dependencies into a vendor folder and link to the behat binaries from the bin folder.

Step 2: Setup Behat

Run bin/behat --init   to create the “features” folder with a few defaults.

Create a file called behat.yml in you project’s root folder (next to your composer.json file) with the following:

Pay attention to the formatting. In Yaml, indentation dictates structure (as in Python it dictates control-flow) so its unusually easy to break something with zero compile-time syntax-based feedback.

Step 3: Setup the MinkExtension bootstrap

This is the only php you will have to edit. The MinkExtension context basically means you don’t have to write any php in order to perform typical web-related tests.

Replace the file features/bootstrap/FeatureContext.php with the following content:

 Step 4: Create your first feature test.

Create a file called features/wiki-search-test.feature with the following content:

Step 4: Profit

In your project’s root folder run:  bin/behat

This will hit up the web server, access pages, send data, and grep the output. All behind the scenes.

If all tests pass, you should see:

Glorious Testing

To see all of the cool code-free one-liners that Mink provides, run:  bin/behat -dl

Now go make more feature tests. Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *