Skip to content

Commit faf6bdc

Browse files
authored
Merge pull request #15 from subeshb1/feature/support_external_scripts
Add external script integration
2 parents fffaa8f + 325c5ab commit faf6bdc

File tree

2 files changed

+247
-42
lines changed

2 files changed

+247
-42
lines changed

README.md

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ OPTIONS:
6565
COMMANDS:
6666
run Run test cases specified in the test file.
6767
test Run automated test in the test file.
68+
describe List test cases or describe the contents in a test case.
6869

6970
Run 'api-test COMMAND --help' for more information on a command.
7071
```
@@ -135,6 +136,7 @@ api-test -v -f test.json run test_case_1 # To run in verbose mode use `-v`
135136
## Automated testing
136137

137138
To run an automated test run,
139+
138140
```sh
139141
api-test -f test.json test test_case_1
140142
api-test -f test.json test all # To run all tests
@@ -359,14 +361,14 @@ To test using `path_eq` check:
359361
"name": "ram",
360362
"age": 20
361363
},
362-
"people[1].name": "Shyam"
364+
"people[1].name": "Shyam"
363365
}
364366
}
365367
}
366368
}
367369
```
368-
The above example shows how to access an object path to compare and check the values at any depths.
369370

371+
The above example shows how to access an object path to compare and check the values at any depths.
370372

371373
### 5. path_contains
372374

@@ -378,10 +380,12 @@ The `path_contains` does the same check as `contains` but allows the check to be
378380
{
379381
...
380382
"expect": {
381-
"path_contains": {
382-
"path": "value",
383-
"path.key1.key": "value"
384-
}
383+
"body": {
384+
"path_contains": {
385+
"path": "value",
386+
"path.key1.key": "value"
387+
}
388+
}
385389
}
386390
}
387391

@@ -423,6 +427,67 @@ To test using `path_contains` check:
423427
}
424428
}
425429
```
430+
431+
### 6. External scripts or program
432+
433+
If none of the above checks work for you, there is a way to inject **any language** to compare and test an api response. To do so, provide the command name or script in the `external` key in the `expect` block. If a test case passes return an `exit code 0` and if a test fails `exit code > 0` to communicate with the `api-test` program.
434+
435+
#### Syntax
436+
437+
```js
438+
{
439+
...
440+
"expect": {
441+
"body": {...},
442+
"header": {...},
443+
"external": "<your program>"
444+
}
445+
}
446+
```
447+
448+
Example:
449+
450+
test.json
451+
452+
```js
453+
{
454+
...
455+
"expect": {
456+
"body": {...},
457+
"header": {...},
458+
"external": "node test.js"
459+
}
460+
}
461+
462+
```
463+
464+
test.js
465+
466+
```js
467+
let testCase = process.argv[2]; // First arg will be test case key
468+
let body = process.argv[3]; // Second arg will be body
469+
let header = process.argv[4]; // Third arg will be header
470+
471+
let success = true;
472+
switch (testCase) {
473+
case "get_api":
474+
if (success) {
475+
process.exit(0); // For success case
476+
} else {
477+
process.exit(1); // For failure case
478+
}
479+
break;
480+
case "invalid_post_api":
481+
...
482+
break;
483+
484+
default:
485+
break;
486+
}
487+
```
488+
489+
The `test case key`, `body` and `header` are passed respectively to the supplied program. You can use any language as long as you are sending out the correct exit code for failure and success.
490+
426491
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.
427492

428493
## Uninstalling

0 commit comments

Comments
 (0)