Behavior-Driven Development With Behat and Mink
Unit testing is not the only practice software engineers should use to ensure a project is operating correctly. Behavior-driven development has been growing in popularity since 2008. In short, BDD starts with a “story.” Whoever is in charge of designing the software’s flow writes use-cases for features in human-readable language. Tests are then run using headless browsers and other automation tools to ensure the story can be execute properly.
A short story in Gherkin, Behat’s markup language, might look like this:
1 2 3 4 5
This story looks eerily similar to a functional requirements document because, well, that’s what it is. Typically someone doing quality assurance would have to manually follow this scenario every time a new feature or bug fix has been implemented in order to properly regression test. But Behat can automatically test these scenario in a variety of browsers, headless or not. This is great for testing complex features like those involving AJAX requests. Mink defines how Behat should translate the Gherkin steps into browser commands in order to test the site. We can also define our own custom steps here.
Let’s take a look at how we can implement Behat to test an e-commerce site.
Clone the simple e-commerce site and checkout the shop-basic tag. This post assumes you are working in either OSX or Linux.
1 2 3 4 5 6
Press enter after the composer install for each field to keep default values. Now you should be able to navigate to http://127.0.0.1:8080/search to see a working ecommerce site. Try buying a red hat to see how the site works. (Note: You will not be charged for the any red hats on this site.)
Including Behat and Mink Libraries
We need to add a few dependencies to our project using composer. This took a little time to figure out, and at the time of writing this post works. However be warned - you may need to adjust the version numbers in order to get this working in the future.
1 2 3 4 5 6 7 8 9 10 11 12 13
Run composer update to download the the Behat and Mink libraries. Then create the necessary Behat files.
This creates a new directory, “features”, which holds our Gherkin and Behat FeatureContext. Modify the FeatureContext to extend MinkContext. This allows us to leverage the pre-defined steps included in Mink.
1 2 3 4 5 6 7
We should also create a new file in the project root directory - behat.yml.
1 2 3 4 5 6 7
Creating the Gherkin Story
Time to write our features in human terms. Create two new files: features/search.feature and features/buy.feature.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12
These files describe, in fairly simple terms, what the application should do. We should be able to search for a red hat, and see the description in the results.
Now that we have created a couple of Gherkin stories, we can run Behat to test our site’s behavior in a headless browser.
Behat is now using a headless browser to confirm whether our app adheres to the steps we’ve defined in the .feature files. We should see our stories outputted to the terminal. If everything works we should see a lot of green, and a summary of successful scenarios and steps.
The completed source code can be found on the behat branch of the Shop-Top repository on GitHub here: https://github.com/nater1067/Shop-Top/tree/behat .
What Comes Next
Now that you’ve got Behat running successfully within a Symfony 2 application, it’s time to get your hands dirty with more advanced test behavior-driven development. You’ve seen very simple examples of Behat tests, but Behat can do so much more than check simple forms. Try adding bundle-specific features with the Symfony 2 Behat bundle. Add some tests which check front-end functionality such as AJAX features. Tons of information can be found in the Behat docs at http://docs.behat.org .
Have an improvement for this post? What additional Behat functionality would you like included in this post? Please comment below.