REST API Testing with Cucumber and Javascript

Ancient Knowledge

This article is getting old. It was written in the ancient times and the world of software development has changed a lot since then. I'm keeping it here for historical purposes, but I recommend you check out the newer articles on my site.

There are a great many tools available for testing REST Api's by various vendors, open source plugins, browser plugins, and the old classic curl. If you're looking for a really simple way to test REST Api's that is easy to maintain and entirely automated then you should consider using Cucumber with Javascript.

If you're not familiar with Cucumber, its an adapter that handles gherkin, a domain specific language for writing ATDD tests. Here is what a simple automated test looks like using gherkin DSL:

Scenario: Making a simple GET request
    When I make a GET request to "/posts/1"
    Then The response property "userId" should be "1"
    And The response property "id" should be "1"

Any member of your team, whether it's a developer, tester, or analyst can read and understand the scripts, which really makes it easier to collaborate as a team and eliminate confusion. A simple POST request might look like this

Scenario: Making a POST request using tabular data
    Given The request data
    | title | body | userId |
    | foo   | bar  | 1      |
    When I make a POST request to "/posts"
    Then The response property "userId" should be "1"
    And The response property "id" should be "101"

Or if you prefer to use json (I certainly do), you can use this alternate format

Scenario: Making a POST request with json data
    Given The json request data
    """json
    {
        "title": "foo",
        "body": "bar",
        "userId": 1
    }
    """
    When I make a POST request to "/posts"
    Then The response property "userId" should be "1"
    And The response property "id" should be "101"

I have written a small framework as a starting point that you can clone from Github and start writing tests today. The framework can be customized as you go along and expanded to cover any testing need you have. https://github.com/carbonrobot/relish