Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
144 lines (87 sloc) 4.56 KB

REST API Test Example

The example REST API test uses:

The Client

The REST API client used in this testing is:

It has methods corresponding to some HTTP methods:

  • delete
  • get
  • post
  • put

It also has method

  • self.with

which should be used instead of method new. The method takes a block, and yields with an instance of ExampleRestClient, which the block can then use for testing.


Data Classes

The testing here uses data classes that correspond to JSONPlaceholder data structures.

Each data class is derived from:

which has these methods:

  • self.verdict_equal?
  • log
  • to_hash

Resource Data Classes

The testing here uses data classes that correspond to the JSONPlaceholder resources:

Each resource data class has these instance methods:

  • initialize
  • verdict_valid?: validates the form (but not the correctness) of its data.

Each resource data class is derived from:

which has these existence methods:

  • self.exist?
  • self.verdict_exist?
  • self.verdict_not_exist?

along with CRUD methods:

  • self.create
  • self.update
  • self.delete

and a couple of other convenience methods:

  • self.get_all
  • self.get_first

More Data Classes

Additional data classes correspond to data structures that are not themselves resources, but instead are embedded in resource data:


Each endpoint found in the target REST API is encapsulated by an endpoint class in this test example.

These endpoint classes implement an endpoint object pattern that corresponds directly to the well-known page object pattern.

Each endpoint class has three methods:

  • sends request and returns data object(s) created from response.
  • self.call_and_return_payload: sends request and returns both data object(s) and parsed JSON.
  • self.verdict_call_and_verify_success: sends request and verifies response.

The endpoint URLs and their behaviors are very consistent, which makes it easy to implement base classes that do most of the work:

Each of these base classes implements methods self.call_and_return_payload and self.verdict_call_and_verify_success.

Method is implemented in a higher-level base class:


Base Class for Test

If you've read this far, you won't be surprised that there's a:

The class is derived from class Minitest::Test, in Ruby gem

Its only method, prelude accepts a block, and yields with instances of Log and ExampleRestClient for use in the block.

Test Classes

Each test class performs testing for one of the REST API resources:

Each test method uses Log's nested sections to give the test structure and improve readability. The sections are reflected in the created XML log.

If you have cloned this project, you can run the tests and produce logs by typing command:

  • rake examples:rest_api