placeholder

Behavior driven development with Behat

Behat is a testing method that enables behavior-driven development: an Agile development method that considers the operation of an application from a user's perspective. But just as importantly, Behat also allows non-technical team members to test functionality. In this article we share our experiences and give tips on how to use Behat to work together on a more stable and better functioning website.

You probably recognize the situation

You are in the kick-off phase of a new project and everyone is excited about what you are going to create. There is a clear vision, budget to hire a decent agency and a list of all the functionality you think you need for a successful website. Let's go!

Things are worked out in a functional design and the development team gets to work. During unit testing, some minor bugs are found but are quickly fixed. A success, right? Not quite because at the first delivery, the functionality built turns out not to do exactly what the client had in mind. The wonders if it was not described precisely enough, if he left too much room for a different interpretation. Or does the team not understand the context and end user enough?

Behavior-driven development

Situations like this are exactly why behavior-driven development (BDD) has become so popular: it improves communication and collaboration between developers and non-technical stakeholders. User stories are of course a good way to avoid this kind of "noise": everyone understands the concrete description of the actions a user should be able to perform and the result. But as a client, how do you ensure that user stories are correctly converted to automated tests?

Behat: normal people can also participate

So the premise of behavioral driven development is that software development is driven by human behavior rather than an abstract functional design. But to test whether the technology actually supports this behavior, it makes sense to write the test in ordinary human language as well. That is exactly what Behat makes possible.

The great benefit of Behat is thus that user stories can be used almost one-to-one for automated testing. You quickly agree on what needs to be tested and especially: in what way. The test is visualized in a browser; you can watch as it were 'live' while the tests are being performed. It is therefore no longer only developers who can quickly test the developed functionality; with Behat, clients can also easily test them against the user stories themselves.

An additional advantage is that in case of security updates or deployment of new functionality to an existing platform, you can continue to use existing Behat tests in your regression tests. Browser testing also becomes a lot easier with Behat: by using a plugin, all tests can be run in Browserstack, allowing all tests to be run automatically in different browsers on different systems.

Sample scenario

Scenario: Checking search FAQ + link to FAQ
  When I am on "/search"
  And I fill in "drinkpak" for "search term"
  And I press the "Search" button
  Then I should see "In which bin do drink cartons belong?"
  When I click "In which bin do drink cartons belong?"
  Then I should be on "/self-measures/many-questions"
  And I should see "Drinking cartons may go with plastic."

BDD vs. Unit testing

So testing with Behat is a fundamentally different method than Unit testing, for example. With Unit testing, you test isolated pieces of source code (units): if you put A in, B should come out. The results of a unit test are abstract and isolated, making it difficult for a client to judge whether the platform serves the users as you had envisioned. With BDD, however, testing is done from the user perspective.

Test different scenarios (and avoid hassles)

Of course, you don't just want to test the scenarios in which the user acts exactly as imagined; deviant behavior is also interesting. By identifying these borderline cases together and describing the desired results in user stories, you prevent these borderline cases from being handled incorrectly or incompletely in the development process. In addition, in this way you create a more complete picture of the functioning of your application meeting specifications.

Behat: better collaboration and better technology

So our experiences are very positive: faster communication, clear expectations, better performance and a more stable system. But perhaps more important is that as a client you get more control over the development process. Many don't have in-depth technical knowledge; after all, that's what they hire an agency for. But because anyone can write a user story and use it to write a test in Behat, everyone can contribute. This not only increases the quality of the tests, it also creates more participation, support and ownership within the organization.

Talking through Behat and Behavior Driven Development?

Send me a message!