I’ve been coaching Behat and relevant tech’s for some time now. Think it be good to share the information out to you start to finish.
My previous article covered the installation of Behat. If you’d like to have a look at that, click here.
lets continue on, after installation we need to setup our basic scaffold to hold our feature files and our context files. Run this command to set yourself up with the basic structure.
$_ vendor/bin/behat --init
This will create:
Don’t worry about this file just yet.
Next you need to setup your behat.yml file which holds all Behat related configuration such as which website to target etc.
Create your behat.yml file with the content below:
default: suites: default: contexts: - FeatureContext - Behat\MinkExtension\Context\MinkContext extensions: Behat\MinkExtension: base_url: http://en.wikipedia.org/ sessions: default: goutte: guzzle_parameters: curl.options: CURLOPT_SSL_VERIFYPEER: false CURLOPT_FOLLOWLOCATION: false CURLOPT_CERTINFO: false CURLOPT_TIMEOUT: 120 ssl.certificate_authority: false
This section of the file references all your context files that hold step definitions to be loaded when Behat kicks off. In the above example we’ve got our FeatureContext file which is an empty file you created before hand when scaffolding the structure, and the MinkExtension which gives us loads of step definitions to get started out of the box.
This section loads up any custom extensions designed for Behat. Extensions give you powerful API’s to work with on the PHP side. We will interact with them in time!
This above file contains a bit more than your standard basic examples out there. Basically what you’ve got in the guzzle_parameters is a way to ignore ssl certificate validation which usually holds up testing if you visit a website in https mode.
Now then we’re all set. All we need to do now is create our first feature file.
Feature files live in the features directory, outside of the bootstrap folder. Any files you create that you want behat to consider as your feature files need to have the extension of “.feature”. Lets create one.
$_ touch features/my-first-test.feature
If you fancy just creating these files with a text editor go ahead! Its fine. Lets write our first feature then.
In terms of what the syntax is and how to format your file please have a look here http://behat.org/en/latest/user_guide/features_scenarios.html. I find the documentation limiting in terms of what the idea behind the syntax is, but if we do that we’d be stepping into the BDD world. Right now we’re just having our Behat’s on.
To get a list of all step definitions available to us, you can run the command
$_ vendor/bin/behat -dl
To search for a particular step definition by partial match you can run
$_ vendor/bin/behat -d "<your search term>"
Feature: In order to try out some basic testing tactics As a developer I want to try out an automated test against wikipedia Scenario: Homepage has wikipedia on it. Given I am on "/wiki/Main_Page" Then I should see "Wikipedia"
As you can see, the above is written in a very particular language. Its called “Gherkin”. You can find more information here http://behat.org/en/latest/user_guide/gherkin.html.
Since we’ve got the MinkContext loaded up thanks to the setup we’ve done in our behat.yml file, these step definitions will be recognised by Behat and will execute as we wanted them to. Now if you run Behat you should get a passing test.