You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Both the headers and body can be compared to create automated api tests using different types of checking schemes described in further sections. All the checking schemes can be used for a test case.
146
+
To define test units add them in `expect` object in the testCase.
147
+
148
+
```js
149
+
{
150
+
"test_case_1": {
151
+
"path":"/path_1",
152
+
"method":"POST",
153
+
"expect": { // automated tests are kept inside this object
154
+
"header": {
155
+
...
156
+
},
157
+
"body": {
158
+
...
159
+
}
160
+
}
161
+
}
162
+
}
163
+
```
164
+
165
+
There are 5 ways you can compare the result from the api response.
166
+
167
+
### 1. eq
168
+
169
+
The `eq` check compares every element in an object irrespective of the order of object keys and array elements. Every element in compared object should match as the object defined in `eq` block.
170
+
171
+
#### Syntax
172
+
173
+
```js
174
+
{
175
+
...
176
+
"expect": {
177
+
"body": {
178
+
"eq": {
179
+
"key":"value"
180
+
}
181
+
}
182
+
}
183
+
}
184
+
185
+
```
186
+
187
+
Example:
188
+
The api has following response.
189
+
190
+
```js
191
+
{
192
+
"name":"ram",
193
+
"age":20
194
+
}
195
+
```
196
+
197
+
To test using `eq` check:
198
+
199
+
```js
200
+
{
201
+
...
202
+
"expect": {
203
+
"body": {
204
+
"eq": {
205
+
"name":"ram",
206
+
"age":20
207
+
}
208
+
}
209
+
}
210
+
}
211
+
```
212
+
213
+
The check will pass for the above response. If any of the value or key is different it will throw error.
214
+
215
+
### 2. contains
216
+
217
+
The `contains` check compares the expected value with all the possible subset of the compared object irrespective of the order of object keys and array elements. It will pass if the value matches any subset.
218
+
219
+
#### Syntax
220
+
221
+
```js
222
+
{
223
+
...
224
+
"expect": {
225
+
"body": {
226
+
"contains": {
227
+
"key":"value"
228
+
}
229
+
}
230
+
}
231
+
}
232
+
233
+
```
234
+
235
+
Example:
236
+
The api has following response.
237
+
238
+
```js
239
+
{
240
+
"name":"ram",
241
+
"age":20
242
+
}
243
+
```
244
+
245
+
To test using `contains` check:
246
+
247
+
```js
248
+
{
249
+
...
250
+
"expect": {
251
+
"body": {
252
+
"contains": {
253
+
"age":20
254
+
}
255
+
}
256
+
}
257
+
}
258
+
```
259
+
260
+
The check will pass for the above response as `"age": 20` is the subset of response.
261
+
262
+
### 3. hasKeys
263
+
264
+
The `hasKeys` will check if the provided keys in array are present in the response or not.
All the above keys are valid in the response. We can compare the key at any depth. While accessing arrays, be sure to use the index without brackets. The key accessing pattern contradicts with the next two checking schemes where bracket is used to access array properties.
312
+
313
+
### 4. path_eq
314
+
315
+
The `path_eq` does the same check as `eq` but allows the check to be made inside JSON object path at any depth. The path accessing pattern follows javascript object accessing patterns.
316
+
317
+
#### Syntax
318
+
319
+
```js
320
+
{
321
+
...
322
+
"expect": {
323
+
"path_eq": {
324
+
"path": {"key":"value:"},
325
+
"path.key1.key":1
326
+
}
327
+
}
328
+
}
329
+
330
+
```
331
+
332
+
Example:
333
+
The api has following response.
334
+
335
+
```js
336
+
{
337
+
"people": [
338
+
{
339
+
"name":"ram",
340
+
"age":20
341
+
},
342
+
{
343
+
"name":"Shyam",
344
+
"age":21
345
+
}
346
+
]
347
+
}
348
+
```
349
+
350
+
To test using `path_eq` check:
351
+
352
+
```js
353
+
{
354
+
...
355
+
"expect": {
356
+
"body": {
357
+
"path_eq": {
358
+
"people[0]": {
359
+
"name":"ram",
360
+
"age":20
361
+
},
362
+
"people[1].name":"Shyam"
363
+
}
364
+
}
365
+
}
366
+
}
367
+
```
368
+
The above example shows how to access an object path to compare and check the values at any depths.
369
+
370
+
371
+
### 5. path_contains
372
+
373
+
The `path_contains` does the same check as `contains` but allows the check to be made inside JSON object path at any depth. The path accessing pattern follows javascript object accessing patterns.
374
+
375
+
#### Syntax
376
+
377
+
```js
378
+
{
379
+
...
380
+
"expect": {
381
+
"path_contains": {
382
+
"path":"value",
383
+
"path.key1.key":"value"
384
+
}
385
+
}
386
+
}
387
+
388
+
```
389
+
390
+
Example:
391
+
The api has following response.
392
+
393
+
```js
394
+
{
395
+
"people": [
396
+
{
397
+
"name":"ram",
398
+
"age":20
399
+
},
400
+
{
401
+
"name":"Shyam",
402
+
"age":21
403
+
}
404
+
]
405
+
}
406
+
```
407
+
408
+
To test using `path_contains` check:
409
+
410
+
```js
411
+
{
412
+
...
413
+
"expect": {
414
+
"body": {
415
+
"path_contains": {
416
+
"people[0]": {
417
+
"name":"ram",
418
+
},
419
+
"people[1].name":"Shyam",
420
+
"people": []
421
+
}
422
+
}
423
+
}
424
+
}
128
425
```
426
+
The above example shows how to access an object path to compare and check the values at any depths. All the above comparison are a subset of response and will pass the check.
0 commit comments