These are the things you should generally test your APIs for.
🤓 It is recommended to check only invariable parts of responses. Check for required fields and only values you control. For instance, it is not recommended to check id fields, date fields, as they can be frequently changed.
Ensure that inside codecept.conf.js in helpers section REST or GraphQL helpers are enabled.
If you use REST helper add JSONResponse helper below with no extra config:
// inside codecept.conf.js
// ...
helpers:{
REST:{
endpoint:'http://localhost:3000/api'
},
// .. add JSONResponse helper here
JSONResponse:{}
}
If you use GraphQL helper add JSONResponse helper, configuring it to use GraphQL for requests:
helpers:{
GraphQL:{
endpoint:'http://localhost:3000/graphql'
},
// .. add JSONResponse helper here
JSONResponse:{
requestHelper:'GraphQL',
}
}
Originally, REST and GraphQL helpers were not designed for API testing.
They were used to perform API requests for browser tests. As so, they lack assertion methods to API responses.
Generate TypeScript definitions to get auto-completions for JSONResponse:
npx codeceptjs def
After helpers were configured and typings were generated, you can start writing first API test. By default, CodeceptJS saves tests in tests directory and uses *_test.js suffix. The init command created the first test for you to start.
REST or GraphQL helpers implement methods for making API requests.
Both helpers send requests via HTTP protocol from CodeceptJS process.
For most cases, you will need to have authentication. It can be passed via headers, which can be added to helper’s configuration in codecept.conf.js.
helpers:{
REST:{
defaultHeaders:{
// use Bearer Authorization
'Authorization': 'Bearer 11111',
'Content-Type': 'application/json',
'Accept': 'application/json',
},
}
}
Or you can use the browser cookies if you are running browser session.
In this case use setSharedCookies() from @codeceptjs/configure package:
GraphQL have request format different then in REST API, but the response format is the same.
It’s plain old JSON. This why JSONResponse helper works for both API types.
Configure authorization headers in codecept.conf.js and make your first query:
Feature('Users endpoint')
Scenario('get user by query',({I})=>{
// make GraphQL query or mutation
const resp =awaitI.sendQuery('{ user(id: 0) { id name email }}');
I.seeResponseCodeIsSuccessful();
// GraphQL always returns key data as part of response
JSONResponse provides set of assertions for responses in JSON format. These assertions were designed to check only invariable parts of responses. So instead of checking that response equals to the one provided, we will check for data inclusion and structure matching.
For most of cases, you won’t need to perform assertions by accessing response object directly. All assretions are performed under hood inside JSONResponse module. It is recommended to keep it that way, to keep tests readable and make test log to contain all assertions.
Response status codes can be checked to be equal to some value or to be in a specific range.
To check that response code is 200 call I.seeResponseCodeIs:
I.seeResponseCodeIs(200);
But because other response codes in 2xx range are also valid responses, you can use seeResponseCodeIsSuccessful() which will match 200 (OK), 201 (Created), 206 (Partial Content) and others. Methods to check 3xx, 4xx, 5xx response statuses also available.
ℹ️ If response is an array, it will check that every element in array have provided keys
However, this is a very naive approach. It won’t work for arrays or nested objects.
To check complex JSON structures JSONResponse helper uses Zod library.
It has rich API to validate JSON by the schema defined using JavaScript.
ℹ️ If response is an array, it will check that at least one element in array matches JSON
To perform arbitrary assertions on a response object use seeResponseValidByCallback.
It allows you to do any kind of assertions by using expect from chai library.
You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.