RESTful API Testing with Behat and Guzzle

Behat and Guzzle

Behat is a behaviour driven development framework for the PHP application. Behat can be used for acceptance testing and api testing.

Guzzle is PHP HTTP client to work with HTTP and web services. You can read more about Guzzle on the official website . We can use combination of the Behat and Guzzle for the testing of the REST API. In this post, we will see how to use Guzzle http  for testing GitHub web services.

Behat in Action

Let’s do some action now. We will install behat, create feature simple file and implement step definitions.

Installation & Config

Let’s create our composer.json to install all the dependencies with composer.

Now, we need to download composer and install all these dependencies

This will install all the dependencies in the ‘vendor’ directory. Now we will create config file for behat.

Note, we have created basic config for the behat. Now initialise Behat so that, Behat will create ‘bootstrap’ directory and ‘FeatureContext.php’ file.

Create Feature file

In order to test the GitHub’s REST API with Behat, we need to create a feature file in the plain english. Our feature file should look like this:

As we read the feature file, we can understand that we are checking response format, response code and properties of the response a.k.a response body. At this point, we need to run ‘behat’ to get undefined steps. We need to implement these steps in the ‘FeatureContext.php’ file.

Step Definitions

Now, it’s time to implement step definitions using all the methods of the Guzzle Http library.

We have implemented all the steps to test GitHub REST API. At this point, we will see all the steps has been passing




The source code for this demo is available on the GitHub at ‘RESTful-Behat‘ repo. You can try this by yourself


Watch video demo of this demo on youtube


  • laechoppe

    Thanks for your tutorial. It really helped my to learn some basics an try it myself.

  • laechoppe

    I have done everything like in the tutorial. Test is running but I see that getBody and getStatuscode have errors: Method not found in class.
    Please advise what is wrong

  • Pablo Ignacio de la Vega

    I don’t want the data persist so I add “use DatabaseTransactions” but when I made the request the, I don’t get that data, how can I solve that?

  • Jher

    Thanks for the tutorial.
    What if i wanted to have a timestamp as one of the parameters in the outbound request.
    e.g. each request has a unique time stamp value in the request.