Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Testing REST and GraphQL Services With Just-API

DZone's Guide to

Testing REST and GraphQL Services With Just-API

Learn how to use Just-API, a specification-based framework for testing REST and GraphQL APIs, to create test suites for services.

· DevOps Zone ·
Free Resource

DevOps involves integrating development, testing, deployment and release cycles into a collaborative process. Learn more about the 4 steps to an effective DevSecOps infrastructure.

Just-API is an open-source test framework for APIs. There are several tools and companies available that offer nice UI to test APIs, but none of them really handle complex API automation flows. Often, people go back to programming to test complex flows. Just-API solves these problems by allowing users to test complex API flows with minimal coding, parallel execution, and built-in response validation constructs with nice reporting.

What Is Just-API?

Just-API is a robust, specification-based framework to test REST and GraphQL (or any HTTP-based) APIs. It takes API suite specification from YAML files and runs them either in serial mode or in parallel mode as instructed by the user.

What’s Special About Just-API?

It’s declarative, and allows the user to test APIs with no code. But you can always tap into code when you need to.

How Does It Work?

In simple terms, users build suites by providing request and response validation specification in a YAML suite. Just-API builds the request, makes a call to the ,server and validates response as per the specification. You can choose to validate any or all of

  • Response Status code
  • Response Headers
  • Response JSON body
  • Response JSON schema

or provide your own custom JavaScript validator function.

A Sample REST API Test Suite

This suite tests the people/1 endpoint of a Star Wars service which returns Luke Skywalker details.

meta:
  name: Star Wars suite
configuration:
  scheme: https
  host: swapi.co
  base_path: /api
specs:
  - name: get "Luke Skywalker" info
    request:
      path: /people/1/
      method: get
    response:
      status_code: 200
      headers:
        - name: content-type
          value: !!js/regexp application/json     
      json_data:
        - path: $.name
          value: Luke Skywalker

As you can see above, It’s quite simple to write tests.

Here we are checking Response.

  • Status code is 200
  • Header value of content-type header matches with Regular expression pattern application/json
  • JSON body object’s name field’s value isLuke Skywalker

A Sample GraphQL Test Suite

The following example tests a GraphQL API that returns the location for a given IP address.

meta:
  name: GraphQL location service
configuration:
  host: api.graphloc.com
  scheme: https
specs:
  - name: Get Location of a an ip address
    request:
      method: post
      path: /graphql
      headers:
        - name: content-type
          value: application/json
      payload:
        body:
          type: json
          content:
            query: >
                   {
                    getLocation(ip: "8.8.8.8") {
                      country {
                        iso_code
                      }
                     }
                    }
            variables: null
            operationName: null
    response:
      status_code: 200
      json_data:
        - path: $.data.getLocation.country.iso_code
          value: US

Sample suite snippets you see here show just the basic stuff we can do with Just-API. It’s packed with tons of useful features.

Features:

  • Runs test suites in parallel/serial mode
  • Supports all widely used HTTP methods
  • Supports Form, Multipart requests, File uploads
  • Response Validation (Headers, Status code, JSON body, JSON schema, Custom Response Validation function)
  • Runs imported custom functions from modules
  • Custom inline and module Javascript sync/async functions
  • Hooks (Before all, After all, Before each, After each, Before test, After test)
  • Custom suite configuration
  • Chained Request flows
  • Ability to define/override Request path, query params, path params, headers, body at runtime
  • Storing suite & test data in context for reuse
  • Importing specs from multiple suites
  • Intrasuite and Intersuite spec dependency execution
  • Reusing test specification
  • Retry failed tests
  • Looping: Generate ’n’ number of tests with a list
  • Built-in HTML, JSON reporters
  • Ability to generate reports in multiple formats for the same run
  • Logging HTTP request/response data for failed tests
  • Proper error reporting
  • Runs only tests matching with a given pattern/string
  • Skipping tests with specification
  • Disable or Enable redirections
  • Reports test duration
  • Allows user to plug-in custom reporters

Check it out and feel free to leave feedback, request enhancements, or file issues.

Resources

Read the 4-part DevOps testing eBook to learn how to detect problems earlier in your DevOps testing processes.

Topics:
devops ,rest ,graphql ,qa ,api ,test automation ,web services ,automated testing ,tutorial ,open source

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}