This is a summary of the knowledge I have gained after some research and first tests.
Behat is a tool for writing feature requirement criteria in Gherkin syntax, and integrating this criteria with PHP. In order to work with Behat (a tool) and Gherkin (a syntax or way of writing feature requirements), you will need to understand how to get Behat setup, the role it plays, and how to write using Gherkin.
The following readme should work: https://github.com/Behat/Behat/blob/master/README.md For Laravel, the following video outlines the process: https://laracasts.com/lessons/laravel-5-and-behat-bffs
composer require behat/behat behat/mink behat/mink-extension laracasts/behat-laravel-extension --dev
behat --init
behat
behat -dl
behat features/example.feature
You may need to run vendor/bin/behat
instead of behat
from your project if the path is not set for behat
.
- For Laravel, you'll follow this process:
- Write your Gherkin requirements in
./features/myFeatureNameHere.feature
. Example:
@authentication
Feature: User Login
As a user
I want to be able to login
So that I can use features that require login
Background:
Given I am not logged in
Scenario: User logs in
And I go to a login form
And I submit valid credentials
Then I am logged in
Scenario: User attempts login with bad credentials
And I go to a login form
And I submit invalid credentials
Then I am notified that the credentials are invalid
And I am prompted to login again
- From your project directory, run behat which will give you the option of [0] or [1]:
- Screen Shot 2017-06-14 at 4.26.57 PM.png
- Select [1] to have Behat process the Gherkin you wrote:
- Screen Shot 2017-06-14 at 4.27.09 PM.png
- Using the functions printed in the terminal, copy them into
./features/bootstrap/FeatureContext.php
- Screen Shot 2017-06-14 at 4.30.39 PM.png
- Run behat again and it will now match up the Gherkin you've written to the functions you've just copied in. You should see some passing tests.
- Screen Shot 2017-06-14 at 4.32.09 PM.png
- From here, you can write your actual tests into the functions you've copied into your code and see whether or not they pass whenever you run behat. Check out resources such as the provided Behat Laracast video for more ideas on what to do from here.
- That's it!
Syntax docs: http://docs.behat.org/en/v2.5/guides/1.gherkin.html
You need to know the syntax for the following keywords: Feature, Background, Scenario, Given, When, Then, And, But. However, knowing more of the syntax will always be useful.
Defining feature requirements in Gherkin can be written at either a high level or a low level.
Writing feature requirements at a high level means they can easily be understood by anyone (from client to developer) and the requirements are agnostic to how the feature is implemented by UX, a developer, a designer, etc.
Writing feature requirements at a low level means they will be very specific about how a feature will be implemented and the criteria will likely only be understood by developers, and possibly UX and designers.
Both of these levels of granularity are perfectly possible in Gherkin. However, one may be more advantageous depending on your needs:
- Help stakeholders express what they want via executable specifications
- Help programmers write better software by making TDD/BDD easy
- Reduce misinterpretations through a ubiquitous language
- Requires specific details about execution and testing be written in code, unit tests, etc. (do not write implementation details in Gherkin)
- Unlikely to require refactoring when a feature's code, UX, or visual design changes
Because of these reasons, writing high level requirements could be highly advantageous over low level requirements
- Interfaces directly with tests and allows Behat+Gherkin to work as testing tools
- Couples tightly with code or UI
- Will have to be refactored when fields change, or possibly when UI changes
Feature: User Register
As a user
I want to be able to register
In order to login
Background:
Given I am not logged in
Scenario: A user registers with valid information
And I go to a registration form
And I submit valid credentials
Then I am registered
And I am logged in
Scenario: A user registers with invalid information
And I go to a registration form
And I submit invalid credentials
Then I am notified that the credentials are invalid
And I am prompted to register again
Feature: User Register
As a user
I want to be able to register
In order to login
Scenario Outline: A user registers with valid information
Given I am not logged in
And I go to a registration form <email> <password> <password confirmation>
And I submit valid credentials
Then I am registered
And I am logged in
Examples:
| email | password | password confirmation |
| [email protected] | password123 | password123 |
| [email protected] | password456 | password456 |
Scenario: A user registers with invalid information
Given I am not logged in
And I go to a registration form
And I enter the email <email>
And I enter the password <password>
And I enter the password confirmation <password confirmation>
And I submit invalid credentials
Then I am notified that the credentials are invalid
And I am shown an error message <result>
And I am prompted to register again
Examples: of common registration problems
| email | password | password confirmation | result |
| [email protected] | | | password missing |
| [email protected] | password123 | password456 | password mismatch |
| [email protected] | password123 | | password confirmation missing |
| test@example | password123 | password123 | invalid email |
| [email protected] | pass | pass | short password |
# etc.
Examples: of less common registration problems
| email | password | password confirmation | result |
| | | | missing email, missing password |
| [email protected] | pass | pass1 | password mismatch, short password |
# etc.
Scenario Outline: I receive messages about invalid credentials
Given I encounter this <problem>
Then I see this advice <advice>
Examples: of problems and advice
| problem | advice |
| this is | incomplete and the end of this example |
Article from the founder of Cucumber on why you should use Cucumber [Behat] to write BDD and not tests: http://aslakhellesoy.com/post/11055981222/the-training-wheels-came-off Video on using Gherkin and the difference between declarative and imperative: https://skillsmatter.com/skillscasts/3141-user-centred-scenarios-describing-capabilities-not-solutions