Skip to content

Instantly share code, notes, and snippets.

@Lemmings19
Last active June 15, 2017 17:48
Show Gist options
  • Save Lemmings19/f13e211d7a3a127a8a0cacce18c4560e to your computer and use it in GitHub Desktop.
Save Lemmings19/f13e211d7a3a127a8a0cacce18c4560e to your computer and use it in GitHub Desktop.
Using Behat and Mink Together

Preface

These are notes from my initial research into what Behat and Mink are as well as how they interact with one another.

The short of it is that these two tools should not go together. They are two different tools for two different purposes.

Behat is for writing BDD and moving that BDD over to testing the domain (via PHPUnit or a similar tool).

Mink is for testing the front-end user interface of web pages. The logic involved here is typically not domain logic; but rather UI logic.

While it is possible to make these two tools work together, it is not a good idea.

A lot of online sources say that you should use these two tools together and encourage it. This is wrong.

What is preferrable is to write your BDD style criteria using Gherkin and Behat, and then save the tests for inside your actual code.

Sources

(2014) Why you shouldn't be writing tests in the BDD, article notes that this was brought up in 2011, questions why people are still doing it: http://elnur.pro/behat-and-mink-are-not-meant-to-be-together

(2011) Author of Cucumber (Behat's parent project) explains why writing tests in your BDD documentation is bad: http://aslakhellesoy.com/post/11055981222/the-training-wheels-came-off

Example of bad usage: https://gist.github.com/patrick99e99/1265118

Example of good usage: https://gist.github.com/aslakhellesoy/1265136

(2017) Some Behat docs showing you how to write tests using Gherkin, which runs entirely contrary to the purpose of BDD but makes no note of this for beginners (none of the documentation anywhere seems to state that this might be bad practice): http://docs.behat.org/en/v2.5/cookbook/behat_and_mink.html#writing-your-first-web-feature

(2012) A video of some people teaching basic Cucumber+Gherkin usage in a presentation: (use bugmenot to find a login password) https://skillsmatter.com/skillscasts/3141-user-centred-scenarios-describing-capabilities-not-solutions

Key quotes from these sources

Behat is the official Cucumber implementation for PHP.

https://cucumber.io/docs/reference/behat

Cucumber is not a testing tool, it's a tool to describe the users' goals.

  • Aslak Hellesøy, Cucumber co-founder

http://aslakhellesoy.com/post/11055981222/the-training-wheels-came-off#comment-329562726

Cucumber was designed around the core principles of BDD, and one of these principles is to improve stakeholder collaboration through a ubiquitous language. This essentially means that both code and executable specifications (Cucumber scenarios) should be written in the language of the domain.

Clicking links and buttons or filling in text fileds has nothing to do with the domain.

The scenarios [BDD] express what a user should be able to do, and not how.

  • Aslak Hellesøy, Cucumber co-founder

http://aslakhellesoy.com/post/11055981222/the-training-wheels-came-off

It’s [using Behat and Mink together to write Gherkin] awful because it fails as a communication medium between stakeholders and developers. And if it was written just for developers, you could as well write a test with PHPUnit driving Mink and avoid the overhead of text-to-code translation.

  • Elnur Abdurrakhimov

http://elnur.pro/behat-and-mink-are-not-meant-to-be-together

Notes

BDD can be verified against domain code directly.

Verifying it against a UI is problematic. It leads to writing tests in the BDD; not behaviours.

PHPUnit to drive Mink allows for writing tests in a developer friendly manner. Behat to drive Mink means writing text to drive code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment