Skip to content

Commit fffaa8f

Browse files
authored
Update readme and add version (#14)
Add docs for automated tests
1 parent de9765a commit fffaa8f

File tree

3 files changed

+336
-9
lines changed

3 files changed

+336
-9
lines changed

README.md

Lines changed: 304 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,21 @@ Since it is a small bash file, you can copy the content in https://raw.githubuse
5252
```sh
5353
$ api-test.sh -h
5454

55-
USAGE: api-test [-hv] [-f file_name] [CMD] [ARGS]
55+
A simple program to test JSON APIs.
56+
57+
USAGE: api-test [-hv] -f file_name [CMD] [ARGS]
5658

5759
OPTIONS:
58-
-h (--help) print this message
5960
-h (--help) print this message
6061
-v (--verbose) verbose logging
6162
-f (--file) file to test
63+
--version print the version of the program
6264

6365
COMMANDS:
6466
run Run test cases specified in the test file.
65-
Example: 'api-test -f test.json run test_case_1 test_case_2', 'api-test -f test.json run all'
67+
test Run automated test in the test file.
68+
69+
Run 'api-test COMMAND --help' for more information on a command.
6670
```
6771

6872
### Test file
@@ -72,7 +76,7 @@ The test file will contain test cases in json format.
7276
Example:
7377
`test.json`
7478

75-
```json
79+
```js
7680
{
7781
"name": "My API test",
7882
"testCases": {
@@ -115,17 +119,311 @@ The test cases are present in the `testCases` object. The main url for the api i
115119

116120
To pull the `template.json`
117121

118-
```
122+
```sh
119123
curl -LJO https://raw.githubusercontent.com/subeshb1/api-test/master/template.json
120124
```
121125

122126
### Running test case
123127

124-
```
128+
```sh
125129
api-test -f test.json run test_case_1 # running single test case
126130
api-test -f test.json run test_case_1 test_case_2 # running multiple test case
127131
api-test -f test.json run all # running all test case. WARNING: Don't name a test case `all`
132+
api-test -v -f test.json run test_case_1 # To run in verbose mode use `-v`
133+
```
134+
135+
## Automated testing
136+
137+
To run an automated test run,
138+
```sh
139+
api-test -f test.json test test_case_1
140+
api-test -f test.json test all # To run all tests
141+
```
142+
143+
![test](https://user-images.githubusercontent.com/27361350/82819405-0e5b5f00-9ec0-11ea-959b-211704ea4c1a.gif)
144+
145+
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.
265+
266+
#### Syntax
267+
268+
```js
269+
{
270+
...
271+
"expect": {
272+
"body": {
273+
"hasKeys": []
274+
}
275+
}
276+
}
277+
278+
```
279+
280+
Example:
281+
The api has following response.
282+
283+
```js
284+
{
285+
"people": [
286+
{
287+
"name": "ram",
288+
"age": 20
289+
},
290+
{
291+
"name": "Shyam",
292+
"age": 21
293+
}
294+
]
295+
}
296+
```
297+
298+
To test using `hasKey` check:
299+
300+
```js
301+
{
302+
...
303+
"expect": {
304+
"body": {
305+
"hasKeys": ["people", "people.0", "people.1", "people.0.name", "people.1.name"]
306+
}
307+
}
308+
}
309+
```
310+
311+
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+
}
128425
```
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.
129427

130428
## Uninstalling
131429

0 commit comments

Comments
 (0)