REST API Test Example
The example REST API test uses:
- JSONPlaceholder: "Fake Online REST API for Testing and Prototyping."
The REST API client used in this testing is:
It has methods corresponding to some HTTP methods:
It also has method
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.
The testing here uses data classes that correspond to JSONPlaceholder data structures.
Each data class is derived from:
which has these methods:
Resource Data Classes
The testing here uses data classes that correspond to the JSONPlaceholder resources:
Each resource data class has these instance methods:
verdict_valid?: validates the form (but not the correctness) of its data.
Each resource data class is derived from:
which has these existence methods:
along with CRUD methods:
and a couple of other convenience methods:
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:
self.call: 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 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
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: